Seems like I have over-engineered my program

For an assignment, I wrote a program that does something like this:

This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n
What should n be in the final term (2 - 10)? 5
1/2 + 1/4 + 1/8 + 1/16 + 1/32 = .96875

I am happy to say that I was successful, but I just feel that I severely over-engineered the code to it, lol. Any quick pointers on what I could do in the future to perhaps tone it down?

Also, if somebody would be so kind to offer up an example of my code with the proper indentation and bracket spacing (for easier reading/debugging in the future), that would be fantastic.

Unfortunately, my instructor is not an expert in C++... when asking any type of question, I get referred back to my book (which I obviously read, as that's my only source if information...)

Here's my code: (I know, I need to work on commenting it)
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
44
45
46
47
48
49
50
51
52
// Lab 5.5 - summation.cpp
// This program displays a series of terms and computes its sum.

#include "stdafx.h" 
#include <iostream>
#include <cmath>
using namespace std;

int main()
{     
   int nTerm;            
   double denom, number;
   double sum = 0.0;     
   char again;

   do
   {
   cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n";
   cout << "\nPlease enter the nth term (2-10):  ";
   cin >> nTerm;
   
   while (nTerm < 2 || nTerm > 10)
	   {
		cout << "\nError!  Please enter an integer between 2 and 10:  ";
        cin >> nTerm;
       }  
   cout << endl;
      
   for (int counter = 1; counter <= nTerm; counter++)
   {
    denom = (pow(2.0, counter));
	number = 1.0 / denom;
	sum += number;
	cout << "1/" << denom;
	
	if (static_cast<double>(counter) / nTerm == 1)
	{ cout << " = " << sum << "\n\n";}
	
	else
	{ cout << " + ";}
   }
   
   sum = 0.0;
   cout << "Would you like to run this program again? (y/n):  ";
   cin >> again;
   cout << "\n\n\n\n";   
   }
   while (again == 'Y' || again == 'y');

   return 0;
}


Thanks,
-jumpinmp
In my opinion your code is pretty good for the most part. A couple of changes I would make. In summing a series, there are very often much more efficient ways to generate each term than by using the pow() function. So I removed the <cmath> completely here.

Line 36 made me ponder a little:
 
    if (static_cast<double>(counter) / nTerm == 1)

Unless I'm missing something (always possible), that can be replaced by
 
    if (counter == nTerm)


It's generally preferred to keep the scope of variables limited to where they are used, thus I moved the declaration of a couple of variables. And although your setting of sum = 0.0 towards the end of the loop is logically correct, it is easier for the reader to understand (and feel satisfied with its correctness) if that is done at the start of each pass through the loop, rather than at the end.

My modified version (apologies, I changed the text which prompts the user for input slightly, which you may disagree with, it was just my opinion):
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
44
45
46
#include <iostream>

using namespace std;

int main()
{
    char again;

    do
    {
        cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n";
        cout << "\nPlease enter the number of terms (2-10):  ";
        int nTerm;
        cin >> nTerm;

        while (nTerm < 2 || nTerm > 10)
        {
            cout << "\nError!  Please enter an integer between 2 and 10:  ";
            cin >> nTerm;
        }
        cout << endl;

        double denom = 1.0;
        double sum   = 0.0;

        for (int counter = 1; counter <= nTerm; counter++)
        {
            denom *= 2.0;
            sum   += 1.0 / denom;;

            cout << "1/" << denom;

            if (counter == nTerm)
                cout << " = " << sum << "\n\n";
            else
                cout << " + ";
        }

        cout << "Would you like to run this program again? (y/n):  ";
        cin >> again;
        cout << "\n\n\n\n";

    } while (again == 'Y' || again == 'y');

    return 0;
}
For completeness, here is the original code with no changes apart from indentation / spacing. Though it should be mentioned that there are several different opinions and preference over styles of indentation.

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
44
45
46
47
48
49
50
51
52
53
54
// Lab 5.5 - summation.cpp
// This program displays a series of terms and computes its sum.

#include "stdafx.h" 
#include <iostream>
#include <cmath>
using namespace std;

int main()
{     
    int nTerm;            
    double denom, number;
    double sum = 0.0;     
    char again;

    do
    {
        cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n";
        cout << "\nPlease enter the nth term (2-10):  ";
        cin >> nTerm;

        while (nTerm < 2 || nTerm > 10)
        {
            cout << "\nError!  Please enter an integer between 2 and 10:  ";
            cin >> nTerm;
        }  
        cout << endl;

        for (int counter = 1; counter <= nTerm; counter++)
        {
            denom = (pow(2.0, counter));
            number = 1.0 / denom;
            sum += number;
            cout << "1/" << denom;

            if (static_cast<double>(counter) / nTerm == 1)
            { 
                cout << " = " << sum << "\n\n";
            }
            else
            {
                cout << " + ";
            }
        }

        sum = 0.0;
        cout << "Would you like to run this program again? (y/n):  ";
        cin >> again;
        cout << "\n\n\n\n";   
        
    } while (again == 'Y' || again == 'y');

    return 0;
}
> Also, if somebody would be so kind to offer up an example of my code with the proper indentation
> and bracket spacing (for easier reading/debugging in the future), that would be fantastic.

Just pick on a brace and indentation style that appeals to you; one that makes you feel that your code is most easily readable and maintainable if written in this style. Just be consistent with whatever style you choose.

Once you start reading real-life code, once you are past the amateur stage, you would have to read, write and maintain code written using many different styles - K&R, Allman, and so on. http://en.wikipedia.org/wiki/Indent_style

Very soon, you would realize that code written in a consistent style is both readable and understandable; it doesn't really matter what the style is as long as it is consistent.

If you modify or extend a program that you didn't write, preserve the style that is already present in the code - the program's overall consistency of style is more important than your own predilections. And don't waste time over sterile arguments int* p vs int *p, which brace and indentation style to use etc.

If I were to write this program, it may end up look something like this; but then that is just me:

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
#include <iostream>

int main()
{
    std::cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n\n" ;

    char again ;
    do
    {
        std::cout << "What should n be in the final term (2 - 10)? " ;
        int nterms = 0 ;
        std::cin >> nterms ;

        if( nterms > 1 && nterms < 11 )
        {
            double sum = 0.0 ;
            double term = 1.0 ;
            const double multiplier = 1.0 / 2.0 ;

            for( int n = 1 ; n <= nterms ; ++n )
            {
                term *= multiplier ;
                sum += term ;

                std::cout << "1/2^" << n << ' ' ;
                if( n < nterms ) std::cout << "+ " ;
                else std::cout << "== " ;
            }

            std::cout << sum << '\n' ;
        }

        else std::cout << "n must be in the range (2-10)\n" ;

        std::cout << "Would you like to run this program again? (y/n):  " ;
        std::cin >> again ;

    } while( again == 'Y' || again == 'y' ) ;

}
Last edited on
Ah, Ok. I knew I overthought some things.
if (counter == nTerm)
cout << " = " << sum << "\n\n";
else
cout << " + ";

Is certainly waaaay more simplistic than what I came up with, haha.

I really appreciate your input, Chevril. I have tired eyes, but I'm going to look over the differences a little more closely tomorrow and try to file them away for future endeavors.

Thanks again!
-jumpinmp
Thanks to both of you, Chevril and JLBorges.
Topic archived. No new replies allowed.