I am currently doing a task in a book which asks me to calculate the mathematical constant e using the while loop. I managed that fairly easily, however I am having troubles calculating e^x, whereas the user inputs x and the degree of accuracy. The code I used for computing e is:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
while (x <= degreeOfAccuracy)
{
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
e += (1/number);
x++;
}
cout << endl << "The mathematical constantr e is: "
<< setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
However, when I tried e^x the following code returned a completely wrong value:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int degreeOfAccuracy, x = 1, exponent;
long double e = 1;
cout << "Enter degree of accuracy of mathimatical constant e: ";
cin >> degreeOfAccuracy;
cout << "Enter the number of which you wish to raise to e: ";
cin >> exponent;
int temp = exponent;
while (x <= degreeOfAccuracy)
{
exponent = temp;
int conter = x;
int intial = x;
long double number = x;
int counter = 1;
while (conter > 1)
{
number = number*(intial-counter);
counter++;
conter--;
}
int counterr = 1;
while (counterr < x)
{
exponent *= exponent;
counterr++;
}
e += (exponent/number);
x++;
}
cout << endl << "The mathematical constantr e is: " << setprecision(degreeOfAccuracy) << fixed << e << endl;
system("pause");
return 0;
}
Any ideas where the calculations went wrong?
This line:
exponent *= exponent;
is wrong. It should be:
exponent *= temp;
Related
#include <iostream>
using namespace std;
// on input n returns the value a_n as a double
double term(int n) {
double val = 1.0 / (n+1.0) / (n+1.0);
if (n%2 != 0) val = -val;
return val;
}
/* computes the sum for i from k to k+n-1 of term(i) by the
* direct upwards method */
double direct_up(int k,int n) {
double sum = 0.0;
for (int i=0; i<n; i++) {
sum += term(k+i);
}
return sum;
}
int main() {
cout.precision(16);
int nterms = 0;
int ft = 0;
cout << "Enter first term, number of terms" << endl;
cin >> ft >> nterms;
cout << "The sum of the " << nterms << " terms starting at " << ft << endl;
cout << "direct_up: " << direct_up(ft, nterms) << endl;
return 0;
}
I have created a program which takes a formula and adds term by term starting from the kth term to the (n-1)th term. However I am unable to work out how to calculate the roundoff error after each term?
Would you be able help me with this please?
I wrote the following code but only works when the first number is symmetry:
symmetry is like this number: 4554 (reading from both ends is the same number)
My question is why the break only works for the first number? It happens when I run it.
#include <iostream>
using namespace std;
int main()
{
int n=0, z=0, r=0, x=0, m;
for (;;) {
cout << "Enter number: ";
cin >> x;
m = x;
while(x!=0) {
r = x % 10;
z = z * 10 + r;
x = x / 10;
}
if(m==z)
break;
else
n++;
}
cout << n;
return 0;
}
Move int z=0, r=0; inside for loop.
Why not using this code to know if the number is symmetry?
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int x;
for (;;) {
cout << "Enter number: ";
cin >> x;
m = x;
std::stringstream str;
str << x;
std::string number = str.str();
if ( number == std::string( number.rbegin(), number.rend() )
break;
else
n++;
}
cout << n;
return 0;
}
Simpler, leads to the same result and is definitely more error prone ;-)
It would have been much easier to reason if you have written like this:
#include <iostream>
bool isPalindrome(int x)
{
int y = x;
int z;
while (x) {
z = z * 10 + x % 10;
x /= 10;
}
return y == z;
}
int main()
{
int n = 0;
int x;
for (;;) {
std::cout << "Enter number: ";
std::cin >> x;
if (isPalindrome(x))
break;
else
++n;
}
std::out << "Number of non-palindromes: " << n << std::endl;
return 0;
}
functions with meaningful names always helpful!
I'm working on a project to print out a table of exponential numbers using nested for-loops. Users specify the number of rows to print and the number of powers. For example, if the users specifies 2 rows and 3 powers, the program should print 1,1,1 and 2,4,9 (2^1,2,3 etc). I should note this is for class and we aren't allowed to use cmath, otherwise I would use pow(). I can't seem to figure out the correct function in a nested for loop that can change both values of the base and the exponent. Here's what I have so far. Thanks for your help!
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int r, p, a;
cout << "The program prints a table of exponential powers.\nEnter the number of rows to print: ";
cin >> r;
cout << "Enter the number of powers to print: " ;
cin >> p;
cout << endl;
for (int i = 1 ; i <= r; i++)
{
cout << setw(2) << i;
for (int q = 1; q <= i; q++)
{
a = (q * q); //This only works for static numbers...
cout << setw(8) << a;
}
cout << endl;
}
}
for (int i = 1 ; i <= r; i++)
{
cout << setw(2) << i;
int a = 1;
for (int q = 1; q <= r; q++)
{
a = (a * i);
cout << setw(8) << a;
}
cout << endl;
}
Several things to note. First, you can compute the powers by maintaining the variable a and multiplying it by i for each power. Also, I think you want the upper bound on your second loop to be r and not i.
You need couple to change the way accumulate the values of raising a number to a power.
Also, you are using the wrong variable to end the loop in the inner for-loop.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int r, p, a;
cout << "The program prints a table of exponential powers.\nEnter the number of rows to print: ";
cin >> r;
cout << "Enter the number of powers to print: " ;
cin >> p;
cout << endl;
for (int i = 1 ; i <= r; i++)
{
cout << setw(2) << i;
a = 1; // Start with 1
for (int q = 1; q <= p; q++) // That needs to <= p, not <= i
{
a *= i; // Multiply it by i get the value of i^q
cout << setw(8) << a;
}
cout << endl;
}
}
I am a beginner in c++. I was solving problems in CodeChef and came across this prooblem: http://www.codechef.com/problems/FCTRL2
The problem asks you to find the factorial of large numbers. To achieve this I am trying to perform digit by digit multiplication. However, in my code in line 39 the sum of "b[i] * c[j] * pow(10,(i+j)) + x" is being reduced by 1 from 3rd iteration of the loop. I am not able to figure out the reason for sum being reduced by 1.
Kingly help me out with this problem.
Here is the code:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
// to find the number of digits
int num_digits(int val) {
int digits = 0;
while(val) {
val /= 10;
digits++;
}
return digits;
}
int main() {
std::ios_base::sync_with_stdio(false);
int b[10], c[10], num1, num2, x=0, num_inputs;
std::cin >> num_inputs;
while(num_inputs) {
std::cin >> num1;
num2 = num1 - 1;
int num1_digits = num_digits(num1);
int num2_digits = num_digits(num2);
int temp1 = num1;
int temp2 = num2;
while(num2>0) {
std::cout << temp2 << " " << temp1 << " " << num1_digits << " " << num2_digits<< endl;
for(int i=0; i<num2_digits; i++) {
b[i] = temp2 % 10;
temp2 /= 10;
for(int j=0; j<num1_digits; j++) {
c[j] = temp1 % 10;
temp1 /= 10;
std::cout << b[i] << " " << c[j] << " " << pow(10, (i+j)) << " ";
x = b[i] * c[j] * pow(10,(i+j)) + x; // the sum is getting reduced by 1
std::cout << x << endl;
}
temp1 = num1;
}
num2--;
temp2 = num2;
temp1 = x;
x = 0;
num1_digits = num_digits(temp1);
num2_digits = num_digits(temp2);
}
std::cout << temp1;
num_inputs--;
}
return 0;
}
pow is floating-point exponentiation, not integer exponentiation. You should only use it when you want approximate, floating point results, not exact, integer results.
I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}