Repetition help

Pages: 12
My Assignment is almost done but one part has been giving me trouble ever since I started. I don't know what I'm doing wrong but the summation and repetition method aren't matching like they should be. I know the problem is the repetition method but I don't know what I'm doing wrong. Here's what my output looks like:

"
This code will find the sum of integers from N1 to N2.

Please enter the values for N1 and N2: 75 100
Using the repetition method, the sum of values from 75 to 100 is: 2850
Using the summation method, the sum of values from 75 to 100 is: 2275
Would you like run the program again?
Enter y/Y for yes and n/N for no:
"

Here's my code

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
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;


int main()
{
	int sum2 = 0, b, N1, N2;
	char response;
	do
	{
		cout << "This code will find the sum of integers from N1 to N2." << endl << endl;

		cout << "Please enter the values for N1 and N2: ";
		cin >> N1 >> N2;

		while (N1 <= 1)
		{
			cout << "Value for number must be greater than 1, re-enter: ";
			cin >> N1;
		}

		while (N2 <= 1)
		{
			cout << "Value for number must be greater than 1, re-enter: ";
			cin >> N2;
		}

		while (N2 <= N1)
		{
			cout << "N2 must be greater than num1, reenter both numbers : ";
			cin >> N1 >> N2;
		}


		int sum1 = 0;
		for (int b = 1; b <= N1; b++)
			sum1 += b;
		
		sum2 = ((N2 * (N2 + 1) / 2) - ((N1 - 1) * ((N1 - 1) + 1) / 2));

		if (N1 >= 1)
		{
			cout << "Using the repetition method, the sum of values from " << N1 << " to " << N2 << " is: ";
			cout << sum1 << endl;

			cout << "Using the summation method, the sum of values from " << N1 << " to " << N2 << " is: ";
			cout << sum2 << endl;
		}

		cout << "Would you like run the program again?\n"
			<< "Enter y/Y for yes and n/N for no: ";
		cin >> response;

	} while (response == 'y' || response == 'Y');
}
If I understand what you're adding up, I think the issue is the number you start b with in the for loop, as well as the ending value.
what does int b do? my debugger says that it's doing nothing.
Last edited on
The b defined at line 9 isn't used, but the b defined in the for loop is.
oh, ok thanks
BTW, If you change the number that b is equal to the outcome changes.
I made some changes, Hope this helps!
code:
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
#include <iostream>
using namespace std;


int main()
{
	int sum1 = 0,sum2 = 0, N1, N2;
	char response;
	do
	{
		cout << "This code will find the sum of integers from N1 to N2." << endl<<endl;

		cout << "Please enter the values for N1 and N2:\n> ";
		cin >> N1;
		cout<<"> ";
        cin>>N2;

		while (N1 <= 1) /// Makes sure N1 is not 1
		{
			cout << "\nValue for number must be greater than 1, re-enter: ";
			cin >> N1;
		}
		while (N2 <= 1) /// Makes sure N2 is not 1
		{
			cout << "\nValue for number must be greater than 1, re-enter: ";
			cin >> N2;
		}
		while (N2 <= N1) /// Makes sure N1 is not less than N2
		{
			cout << "\nN2 must be greater than N1, please re-enter both numbers :\n> ";
			cin >> N1;
            cout<<"> ";
            cin>>N2;
		}
		for (int b = 1; b <= N1; b++) ///Repetition Method
			sum1 += b;

		sum2 = ((N2 * (N2 + 1) / 2) - ((N1 - 1) * ((N1 - 1) + 1) / 2)); ///Summation Method

		if (N1 >= 1)
		{
			cout << "\nUsing the repetition method, the sum of values from " << N1 << " to " << N2 << " is: ";
			cout << sum1 << endl<<endl;

			cout << "Using the summation method, the sum of values from " << N1 << " to " << N2 << " is: ";
			cout << sum2 << endl<<endl;
		}else{cout<<endl<<"N1 is Less Than One!";}

		cout << "Would you like run the program again?\n"
			<< "Enter y/Y for yes and anything else for no: ";
		cin >> response;

	} while (response == 'y' || response == 'Y');
}
Last edited on
I made some changes, Hope this helps!


Do you get the same number for both sums?
My PC just shut down, sorry! I didn't have time to test it. I'm using my desktop now. I can test it now if you want. Also, Two of the headers do not need to be there.
Last edited on
Well, I'd give the OP a chance to figure it out on their own :)
The outcome changes but none of the outcomes are equal to summation. I need the repetition and the summation to be equal every time.

Edit: Nevermind. responded to what I thought was the last post.
Edit again: Jason I tried your code and got the same results as the one I did. Thanks trying though :)
Last edited on
No problem, but seriously try messing with the value of int b
b represents the values you want to add up. Think about what number b should be initialized to.
The closest I get is 34 but even that isn't right.
It should depend on what the person enters? It's not going to be a fixed number you know ahead of time.

Right now you're adding up numbers from 1 to the first number entered.
Last edited on
I have the Algorithm my teacher gave me. I thought I followed every step exactly but apparently not:

"1) Declare two variables for number (e.g. num1, num2)
2) Perform input validation for numbers:
while (num1 <= 1)
{
See part 1a
}
while (num2 <= 1)
{
See part 1a
}
while (num2 <= num1)
{
output “num2 must be greater than num1, reenter both numbers: ”
input num1, num2
}
3) Declare two variables for finding the sum of integers using method 1 (repetition) and method 2
(formula). (e.g.: sum1, sum2). Set both sums to 0.
Tip: Make sure you initialize sum1 to zero right before the next step! sum2 can be initialized at
the beginning of the program.
4) Perform running sum on sum1 (while or for loop).
start loop at num1, end loop at num2
5) Output sum1
6) Use formula to calculate sum2 (see assignment for formula)
sum2 = [insert formula in proper syntax here]
7) Output sum2
8) If done correctly, sum1 should be equal to sum2."

I thought I followed every step :/
Last edited on
4) Perform running sum on sum1 (while or for loop).
start loop at num1, end loop at num2


Where are you starting and ending your loop? It doesn't match the assignment.
1
2
for (int b = 1; b <= N1; b++) ///Repetition Method
			sum1 += b;
i'm using 3 for N1 and 4 for N2, and that's still not correct.
Blue22 needs to change the for loop so the sum will be correct.
so my loop has to "Start at num1 and end at num2"....I don't even know what means

(I'm sorry if I sound like an idiot. This is the first time I've taken a class like this and I'm actually pretty shocked I made it as far into the code as I did :/)
Pages: 12