Arithmetic Series Summation

I am writing a program for a class project. The program is to receive input from the user: first term of the series, the difference between terms, and the number of terms in the series. While I have the majority of the program completed, I have having trouble displaying the sign (+ or -) both before and after a value of 0 is returned.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    double firstTerm, difference, numTerms;
    
    cout << "What is the first term of the series?";
    cin >> firstTerm;
    cout << endl;
    cout << "What is the difference between adjacent terms?";
    cin >> difference;
    cout << endl;
    cout << "How many terms does the series have?";
    cin >> numTerms;
    cout << endl;
    cout << endl;
    
    if (numTerms <= 0)
    {
       cout << "  Error  ->  The number of terms must be a positive integer.";
       cout << endl;
       cout << endl;
                 
       system("PAUSE");
                 
       return EXIT_SUCCESS;
    }
    else
    {
    double sum, maxValue, i, d, n;
    
    maxValue = (firstTerm) + (difference * (numTerms - 1));
       
    cout << "s(" << firstTerm << "," << difference << "," << numTerms << ") = ";
    
    i = firstTerm;
    d = difference;
    n = numTerms;
    
    sum = 0;
    
    for (i = firstTerm; abs (i) <= abs (maxValue); i = i + d)
    {
        sum = sum + i;
        
        if ((i) >= 0)
        {
             if (i != abs (maxValue))
             {
                   cout << i << " + ";
             }
             else if (i = maxValue)
             {
                   cout << i << " = " << sum;
             }
        }
        else if ((i) <= 0)
        {
             if (i != maxValue)
             {
                   cout << " - " << abs (i);
             }
             else if (i = maxValue)
             {
                  cout << " - " << abs (i) << " = " << sum;
             }
        }
        }
    }
    cout << endl;
    cout << endl;

        
        
    system("PAUSE");
    
    return EXIT_SUCCESS;

}


See sample run:

What is the first term of the series?5

What is the difference between adjacent terms?-5

How many terms does the series have?10


s(5,-5,10) = 5 + 0 + - 5 - 10 - 15 - 20 - 25 - 30 - 35 - 40 = -175

Press any key to continue . . .
Sorry, quick bump. The assignment is due soon and this is the only error I can foresee. Thanks!!!
I think it has to do with how when you are writing a positive number, you write a + after it, and if it is negative you write a - before it; so when going from 0 to a negative number, you write a + after and a - before the negative number after it. Can you see how to fix it?
I understand what you're saying, I've been stuck on it for a few hours now. Since you said that, could/should I insert another conditional [i]if[i] loop for lines 53 and 64? Something like
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
for (i = firstTerm; abs (i) <= abs (maxValue); i = i + d)
    {
        sum = sum + i;
        
        if ((i) >= 0)
        {
             if (i != abs (maxValue))
             {
                   cout << setw(2);
                   if (i  < 0)
                           cout << i << " + ";
                   else if (i > 0)
                           cout << " + " << i;
             }
             else if (i = maxValue)
             {
                   cout << i << " = " << sum;
             }
        }
        else if ((i) <= 0)
        {
             if (i != maxValue)
             {
                   cout << " - " << abs (i);
             }
             else if (i = maxValue)
             {
                  cout << " - " << abs (i) << " = " << sum;
             }
        }
        }
If I may also add, this current semester is the first time I've been exposed to programming. If you see something that could make my coding more efficient or concise, please let me know. I've come to enjoy it and would like to continue to improve. Thanks guys (AND GALS)
Quick tip: since the number of terms must be a positive integer, "numTerms" should be an int, rather than a double. (unless you can have 5.356 terms...)

(You could use an unsigned int, but funky things will happen if the user enters a negative number -- specifically, you'll end up with a super huge positive number.)
One idea is to make both positive numbers and negative numbers add the sign in front except for the first number if it is positive. Another option would be to "look ahead to the next term and don't place a + sign after your current term if the next is negative.
Thank you, I got it! Or at least I'm pretty sure I did. Thanks again.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
    double firstTerm, difference, numTerms;
    
    cout << "What is the first term of the series?";  
    cin >> firstTerm;
    cout << endl;
    cout << "What is the difference between adjacent terms?";  
    cin >> difference;
    cout << endl;
    cout << "How many terms does the series have?";  
    cin >> numTerms;
    cout << endl;
    cout << endl;
    
    if (numTerms <= 0)
    {
       cout << "  Error  ->  The number of terms must be a positive integer.";
       cout << endl;
       cout << endl;
                 
       system("PAUSE");
                 
       return EXIT_SUCCESS;
    }
    else
    { 
    
    double sum, maxValue, i, d, n;
    
    maxValue = (firstTerm) + (difference * (numTerms - 1));
       
    cout << "s(" << firstTerm << "," << difference << "," << numTerms << ") = ";
    
    sum = 0;
    
    i = firstTerm;
    d = difference;
    n = numTerms;
    
    
    
    for (i = firstTerm; abs (i) <= abs (maxValue); i = i + (d))
    {
        sum = sum + i;
        
       
        
        if ((i) >= 0)
        {
             if (i != abs (maxValue))
             {
                   cout << i << " + ";
             }
             else if (i = maxValue)
             {
                   cout << i << " = " << sum;
             }
           
        }
        else if ((i) <= 0)
        {
             if (i != maxValue)
             {
                   cout << " - " << abs (i);
             }
             else if (i = maxValue)
             {
                  cout << " - " << abs (i) << " = " << sum;
             }
           
        }
        }
       
    }
    cout << endl;
    cout << endl;
     
        
    system("PAUSE");
    
    return EXIT_SUCCESS;

}
my error, wrong file...

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
    
    for (i = firstTerm; abs (i) <= abs (maxValue); i = i + (d))
    {
        sum = sum + i;
        
        if ((i) >= 0)
        {
             if (i != abs (maxValue))
             {
                   cout << setw(2);
                   if (i <= 0)
                   {
                         cout << " + " << i;
                   }
                   else if (i >= 0)
                   {
                        cout << " - " << i;
                   }
             }
             else if (i = maxValue)
             {
                   cout << i << " = " << sum;
             }
        }
        else if ((i) <= 0)
        {
             if (i != maxValue)
             {
                   cout << setw(2);
                   if (i <= 0)
                   {
                         cout << " - " << abs (i);
                   }
                   else if (i >= 0)
                   {
                        cout << " + " << abs (i);
                   }
             }
             else if (i = maxValue)
             {
                  cout << " - " << abs (i) << " = " << sum;
             }
        }
        }
Topic archived. No new replies allowed.