### Using Nested Loops for Sigma Summation

Hello!

I am attempting to write a program that takes the following sigma function

n
∑ (ax - ib)^c
i=1

and outputs the sum of the equation given user-input values for n, x, a, b, and c. After displaying the sum, the program will ask the user if he/she would like to input a new set of values into the equation; after summation, the question will be posed again and loop repeated until user decides otherwise (i.e. outputs anything other than "Y").

I have created a program with a do-while loop that is working perfectly as far as iterating the program as many times as indicated by the user, but I think there must be error in my for loop. The sums that are output are completely off. I am not sure if this is due to a mistake due to the conditions of my for loop or if there is a mistake in my assignment of "sum" [pow(((a * x) - (i * b)), c)]. Help with this would be much appreciated!

 ``123456789101112131415161718192021222324252627282930`` ``````#include #include using namespace std; int main() { double n, x, a, b, c; double sum = 0; int i = 0; char userInput; do { cout << "This program will calculate the sum (ax - ib)^c, where i goes from 1 to n." << endl << "Enter the values for n, x, a, b, and c separated by spaces then press Enter." << endl; cin >> n >> x >> a >> b >> c; for (i = 1; i <= n; i++) { sum += i; } sum = pow(((a * x) - (i * b)), c); cout << "The sum of (" << a << "*" << x << " - i*" << b << ")^" << c << " where i goes from 1 to " << n << " is " << sum << endl; cout << "Would you like to do another calculation (Y/N)?"; cin >> userInput; cout << endl; } while (userInput == 'Y'); system("pause"); return 0; }``````
 ``123456`` ``````for (i = 1; i <= n; i++) { sum += i; } sum = pow(((a * x) - (i * b)), c);``````

This makes no sense. You calculate the sum of 1 + 2 +3 + 4 +... +n , and then you ignore that completely and calculate a single `pow(((a * x) - (i * b)), c);`

So, would:

 ``1234`` ``````for (i = 1; i <= n; i++) { sum = pow(((a*x) - (i*b)), c); sum += i; }``````

be the proper form? I know this doesn't work to accomplish my code because I tried it, but maybe I need to adjust conditions?
New idea: I just learned about function calling, would this be useful in my problem above? I could have the function for the equation executed, then printed in main. Does this work in the context of a loop, however? I may be totally off base, but here is what I have with the function call included thus far. I am unsure how to approach the for loop (and i) now, however my overall programs still executes iterations until user inputs "N." Now, the output or sum is showing up as 0.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include using namespace std; double SigmaFunction(double n, double x, double a, double b, double c, double i) { double sum; sum = pow(((a * x) - (i * b)), c); return sum; } int main() { double n, x, a, b, c; double sum = 0.0; //int i = 0; char userInput; do { cout << "This program will calculate the sum (ax - ib)^c, where i goes from 1 to n." << endl << "Enter the values for n, x, a, b, and c separated by spaces then press Enter." << endl; cin >> n >> x >> a >> b >> c; // for (i = 0; i <= n; i++) { // sum = pow(((a * x) - (i * b)), c); // sum += i; // } cout << "The sum of (" << a << "*" << x << " - i*" << b << ")^" << c << " where i goes from 1 to " << n << " is " << sum << endl; cout << endl << "Would you like to do another calculation (Y/N)?"; cin >> userInput; cout << endl; } while (userInput == 'Y'); system("pause"); return 0; }``````
 I just learned about function calling

That's great. The code you showed above, though, doesn't actually call the function you wrote. That function you wrote named `SigmaFunction`. To use it, you have to call it. Your loop doesn't make any use of it.
Topic archived. No new replies allowed.