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:
"
#include <iostream>
#include <cmath>
#include <math.h>
usingnamespace 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');
}
#include <iostream>
usingnamespace 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');
}
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.
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 :)
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."
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 :/)