### Help with Summation program!

Hello!

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!

Thank you.
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.
Okay, sorry about not using the source code button!
That helps! Thank you!

Since I do not use the parameter for the calculation, what would it look like then? sorry visuals help.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` `````` #include #include #include 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.
Once I switch "X" to "x" my compiler then tells me I have a declaration of int x shadows a parameter so I had to give it a new letter and I tried X.

In the for statement I have for (int x=1; x<=n; x++), here is where I believe I have a value assigned to x.

And I am initializing sum=0 twice because sum is in both functions and needs to be declared or else it will not compile.

Thank you for your help! I just cant seem to get this program to stop printing the same numbers over and over again.
 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.
Last edited on
I understand that. How do I get the program to work then?
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?
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` `````` #include #include #include 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.
These are my instructions.

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) { … }
Thank you so much aperio! I see how to use the void function now and it makes it a lot more simple. Thank you for everyone help.

I fixed and compiled the code and it works perfect.
Thank you!
Aperio's implementation should get you in the ballpark. I'm going to present a slightly different implementation just for the sake of variety.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include // the mathematical function: int f(int x) { return pow(x,5) + 10 ; } // summation of f from f(1) to f(max) int summation_of_f(int max) { int sum = 0 ; for ( int x=1; x<=max ; ++x ) sum += f(x) ; return sum ; } int main() { bool getting_summation = true ; while(getting_summation) { std::cout << "Enter a number to get the summation: " ; int input ; if ( std::cin >> input ) { std::cout << "Summation is : " << summation_of_f(input) << '\n' ; std::cout << "Get another summation? (Y/N)\n" ; char choice ; std::cin >> choice ; if ( std::tolower(choice) == 'n' ) getting_summation = false ; } else { std::cout << "Invalid input encountered. Quitting!\n" ; getting_summation = false ; } } std::cout << "Bye!\n" }``````
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.
Topic archived. No new replies allowed.