Need some help with sums

New here in c++. I need some help with designing a C++ code using a for loop. Prof stated that I cannot use anything else other than a for loop to design this program so I was wondering what to do from here. Here is the question
Get the number of terms from the user and calculate the following sum using a for loop:

sum = 4/5 – 8/25 + 12/125 - …

	Example:  	numterms = 1,   sum = 4/5 or 0.8
			numterms = 2, sum = 12/25  or 0.48
			numterms = 0, sum = 0

Display the result in a complete sentence which includes the number of terms and the sum at the end.


If anyone can help me with this I will be grateful.
If to name initial numerator as numerator then it is calculated as

numerator += 4;

denominator is calculated as

denominator *= 5;

Initial value of sum will be 0.0.

So the function could look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double series_sum( unsigned int number )
{
   const double initial_numerator = 4.0;
   const double initial_denominator = 5.0;

   double sum = 0.0;
   double numerator = 0.0;
   double denominator = 1.0;

   for ( unsigned int i = 0; i < number; i++ )
   {
      numerator += initial_numerator;
      denominator *= initial_denominator;
      if ( i % 2 == 0 ) sum += numerator / denominator;
      else sum -= numerator / denominator;
   }

   return ( sum );
}


For testing the function you can use the following code in the main


1
2
3
4
for ( unsigned int i = 0; i < 10; i++ )
{
	std::cout << "series_sum( " << i << " ) = " << series_sum( i ) << std::endl;
}


As for me I have gotten the following results

1
2
3
4
5
6
7
8
9
10
series_sum( 0 ) = 0
series_sum( 1 ) = 0.8
series_sum( 2 ) = 0.48
series_sum( 3 ) = 0.576
series_sum( 4 ) = 0.5504
series_sum( 5 ) = 0.5568
series_sum( 6 ) = 0.555264
series_sum( 7 ) = 0.555622
series_sum( 8 ) = 0.55554
series_sum( 9 ) = 0.555559
Last edited on
Alternatively, you can multiply the denominator by -5.0, removing the need for the if check.
You are right. In this case the initial value of denominator shall be set to -1. Do you have updated already? :)

So the code after its update as Gaminic has pointed out will look as


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double series_sum( unsigned int number )
{
   const double initial_numerator = 4.0;
   const double initial_denominator = -5.0;

   double sum = 0.0;
   double numerator = 0.0;
   double denominator = -1.0;

   for ( unsigned int i = 0; i < number; i++ )
   {
      numerator += initial_numerator;
      denominator *= initial_denominator;
      sum += numerator / denominator;
   }

   return ( sum );
}
Last edited on
I know we like being helpful, but we need to be careful about doing somebody's homework for him (or her). Now Tepples has a mostly functioning program (although not all of the requirements have been met), and has done none of the work.

We like to have the poster try first and then give assistance at the point where he or she is stuck. That way the poster learns something rather than just being given a solution.

This thread has not followed that model.
Agreed doug4, except for the fact that the OP can only use a for loop so i suspect functions haven't been taught yet so aren't allowed. Plus it doesn't output a complete sentence or handle the user input part so the OP still has some work to do.
@,oug4, @cnoeval

do not worry, be happy.:)
Topic archived. No new replies allowed.