Using Nested Loops and Call Functions 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 sigmaFunction call. My overall programs executes iterations until user inputs "N," however, the output or sum is showing up as 0. 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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
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;
}
Hello juliabrushett,

In short you define and initialize "sum" to 0.0, but never change it. You never call the "SigmaFunction" from main that I can see. So, by the time you reach line 27 "sum" still has a value of 0.0.

A suggestion, in place of line 31 put userInput = std::toupper(userInput); // <--- Requires header file "cctype". This way it will not matter if "y" or "Y" is typed.

I would most likely follow that with an if statement. If "Y" set all the variables to zero. Not really necessary, but it makes me feel better.

As far as the math goes it is a bit beyond my understanding, but the syntax of the "pow" function looks OK.

Aster the input on line 20 and before line 27 you need to call the "SigmaFunction". If you need the for loop you might want to move line 27 inside the for loop.

Hope that helps,

Andy
Andy, thank you so much for your help and suggestions! Totally did not see that I failed to call my SigmaFunction. I removed the potential of the for loop, because the more I think about it, the more I'm thinking I don't have a need for it. However, I am having trouble still with my SigmaFunction call. Am I still calling the function incorrectly? When I try to run the program, the following error message occurs: "too few arguments in function call" and "SigmaFunction: function does not take 1 arguments"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
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;
	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;

		
		cout << "The sum of (" << a << "*" << x << " - i*" << b << ")^" << c << " where i goes from 1 to " << n << " is " << SigmaFunction(sum) << endl;

		cout << endl << "Would you like to do another calculation (Y/N)?";
		cin >> userInput;
		cout << endl;

	} while (userInput == 'Y');

	system("pause");
	return 0;
}
Whoops, adjusted my SigmaFunction arguments. Now the program runs, but the numbers are way off. For example, userInput of 1 2 3 4 5 should return a sum of 32, but I always get 7776. Even if I change the userInput, the sum answer is always 7776. New code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <cmath>
using namespace std;

double SigmaFunction(double n, double x, double a, double b, double c) {
	int i = 0;
	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 " << SigmaFunction(n, x, a, b, c) << endl;
		}
		
		cout << endl << "Would you like to do another calculation (Y/N)?";
		cin >> userInput;
		cout << endl;

	} while (userInput == 'Y');

	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>

double term( int i, double x, double a, double b, double c ) {

    return std::pow( a*x - i*b , c ) ;
}

double SigmaFunction( int n, double x, double a, double b, double c ) {

    double sum = 0.0 ;
    for( int i = 1 ; i <= n ; ++i ) sum += term( i, a, x, b, c ) ;
    return sum ;
}

bool do_another_calculation() {

    char another ;
    std::cout << "\nWould you like to do another calculation (Y/N)? ";
    std::cin >> another ;
    return another == 'Y' || another == 'y' ;
}

int main() {

    char another;

    do {

        std::cout << "\nThis program will calculate the sum (ax - ib)^c, where i goes from 1 to n.\n"
                  << "Enter the values for n, x, a, b, and c separated by spaces then press Enter.\n" ;

        int n ;
        double x, a, b, c;
        std::cin >> n >> x >> a >> b >> c;

        std::cout << "The sum of (" << a << "*" << x << " - i*" << b << ")^" << c
                  << " where i goes from 1 to " << n << " is "
                  << SigmaFunction(n, x, a, b, c) << '\n' ;


    } while( do_another_calculation() );
}
Try n=5, x=1, a=2, b=3, c=0.5

This summation is going to fail rather a lot unless c is a positive integer (assumed below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

double ipow( double x, unsigned int p )
{ 
   if ( p == 0 ) return 1;
   return x * ipow( x, p - 1 );
}


int main()
{
   char ans;
   unsigned int n, c;
   double x, a, b, sum;

   do
   {
      cout << "Enter n, x, a, b, c (n and c are positive integers): ";
      cin >> n >> x >> a >> b >> c;

      sum = 0;
      for ( int i = 1; i <= n; i++ ) sum += ipow( a * x - i * b, c );

      cout << "Sum is " << sum << "\n\n";

      cout << "Another calculation? (y/n): ";
      cin >> ans;
   } while ( ans == 'Y' || ans == 'y' );
}
Last edited on
Hello juliabrushett,

Refer to JLBorges's example for a more proper fix, but before you finish plug this for loop in place of your for loop in main and you should have a better idea of what your for loop was doing and hopefully an indication of what needs to be done.

The for loop needs to be in the Sigma function not in main, but you are on the right track. I am still not sure why or what the use of sum += i; is for?

If you take what you have done and JLBorges's suggestions you will have a good program.

Hope that helps,

Andy
Topic archived. No new replies allowed.