I have written a summation.cpp that calculates the summation of a function.
I am suppose to edit this code so that when I write a procedure/function for f(x)=x^5 + 10, which takes the value of x and returns the evaluation of the function. This is what I have:
1 #include <iostream>
2 #include <cmath>
3 #include <cstdlib>
4
5 using std::endl;
6 using std::cout;
7 using std::cin;
8
9 int check_this(int X) {
10 int x;
11 int n;
12 int sum = 0;
13
14 for (int x=1; x<=n; x++){
15 sum += pow (n, 5) + 10;
16 cout << " " << endl;
17 cout << sum << " is the summation for the function." << endl;
18 }
19
20 return X;
21 }
22
23 using std::endl;
24 using std::cout;
25 using std::cin;
26
27 int main() {
28 int x;
29 int n;
30 char y_n;
31 int sum = 0;
32
33 do {
34
35 cout << "Enter a number to get the summation:" << endl;
36 cin >> n;
37
38 int x = check_this(x);
39
40 sum = 0;
41 cout << " " << endl;
42 cout << "Want to enter another number for summation?" << endl;
43 cin >> y_n;
44
45 } while (y_n == 'y' || y_n == 'Y');
46
47
48 cout << "Thank you for finding some summation's!" << endl;
49
50
51 return 0;
52
53 }
I can compile the code just fine but I get incorrect numbers. I keep getting:
"-2147483648 is the summation for the function.
-2147483648 is the summation for the function.
-2147483648 is the summation for the function.
-2147483648 is the summation for the function.
Want to enter another number for summation?"
I don't see what is wrong but I was hoping someone could see my error. Any help please!
In check_this you don't actually use the parameter for the calculation. And you don't set the local variable n to anything, but you do use it as if it had some meaningful value. (It doesn't. It contains junk.)
Please use code tags (the <> button) and avoid supplying line numbers -- they make it impossible to cut and paste your code without editing.
#include <iostream>
#include <cmath>
#include <cstdlib>
using std::endl;
using std::cout;
using std::cin;
int check_this(int X) {
int x;
int n;
int sum = 0;
for (int x=1; x<=n; x++){
sum += pow (n, 5) + 10;
cout << " " << endl;
cout << sum << " is the summation for the function." << endl;
}
return X;
}
using std::endl;
using std::cout;
using std::cin;
int main() {
int x;
int n;
char y_n;
int sum = 0;
do {
cout << "Enter a number to get the summation:" << endl;
cin >> n;
int x = check_this(x);
sum = 0;
cout << " " << endl;
cout << "Want to enter another number for summation?" << endl; cin >> y_n;
} while (y_n == 'y' || y_n == 'Y');
cout << "Thank you for finding some summation's!" << endl;
return 0;
}
I notice two things.. you're initializing sum=0; twice, and calling check_this(x) with no value assigned to x. I think this usually means that the value at the specified memory address of 'x' is calculated other than the value of what you want, but maybe some other experienced programmers can comment on that? I've done the same thing i the past.
Also, in check_this(int X) you never use int X. You're using 'int x' but never 'int X', which is bound to cause some problems. What are you trying to do with this source code? Even line 9 throws me an error because again, 'int X' isn't declared, but 'int x' is. C++ is a case-sensitive language, which is extremely important to consider moving forward.
For me, this source won't even compile so I can't replicate your errors. But perhaps you have a more lax compiler.
In the for statement I have for (int x=1; x<=n; x++)
The x in the function check_this has no relation to the (two) x variables in main. They are, all three, separate in the same way that 3 people with the same name are entirely separate individuals.
It's hard to tell you how to make it work, when it's not entirely clear what you're trying to do. What is the "summation" of a (single) number supposed to be?
#include <iostream>
#include <cmath>
#include <cstdlib>
using std::cout;
using std::cin;
using std::endl;
void check_this(int x)//make this a lower case x. This will hold the value
//passed to it from main() when you get the user input
//from cin. it is also void because it does not return a value
{
int n = x; //this is how many times your for loop will iterate (until
//x<=n)
int sum = 0;
for (int x=1; x<=n; x++)
{
sum += pow (n, 5) + 10;
cout << " " << endl;
cout << sum << " is the summation for the function." << endl;
}
//return x; //no need to return a value since this is where
//sum is being printed to the console window
}
int main()
{
//int x; //this is an unused variable - don't need it
int n;
char y_n;
//int sum = 0; //you don't need this
do
{
cout << "Enter a number to get the summation:" << endl;
cin >> n; //this takes in the value from the user input
//int x = check_this(x); //used check_this(n) instead
check_this(n); //this will pass the user input value to check_this()
//sum = 0; --> you don't need this
cout << " " << endl;
cout << "Want to enter another number for summation?" << endl;
cin >> y_n;
}
while (y_n == 'y' || y_n == 'Y');
cout << "Thank you for finding some summation's!" << endl;
return 0;
}
This should get you closer to what you want. Read the comments in the code to see what changes were made.
You will write a program, summation.cpp, that calculates the summation of a function, e.g. f(x)=x5+10, where x=1 to n.
You will continually ask the user if he/she wants to find the summation for the function, until he/she answers enters no.
If the user wants to find the summation, then prompt the user for the n value.
Edit summation.cpp, and write a procedure/function for f(x)=x5+10, which takes the value of x and returns the evaluation of the function. Hint: int f(int x) { … }
Both are really good codes! Thank you both for your help! Cire, this code works really good, the bool program is new to me but I looked it up and I will use that in the future for sure. Thank you for you help.