Calculation error

i have the following codes. it prompts u to enter lst number and 2nd number. then it output the sum and average range from the lst number and 2nd number. but seems that it only capture from 1-10. would anyone help on correcting it ? thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
float sum(int num1);
int main () {
float n=0, num1=0, num2=0,sum=0,avg;
cout<<"Enter first number: ";
cin >> n;
num1=n;
cout<<"Enter second number: ";
cin>> num2;
for ( n!=0 ; n!=num2;n++)
{
n++;
sum=n*(n+1)/2;
n--;
}
avg=sum/num2;
cout<<"The total sum of range " << num1 << " to " <<num2<< " is: " <<sum<<endl;
cout<<"The average is: " <<avg;
cin.ignore();
cin.get();
}
The 'for' loop at line 11 perplexes me. Considering you're using Gaussian's formula for summing a sequence of integers, you don't need to iterate (i.e. you don't need a 'for' loop). Perhaps you don't fully understand what a 'for' loop does. In this case you should read up on it.

You're reading the two numbers ok. Assuming that we trust the user to enter appropriate values, you can then use num1 and num2 to find your answers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
float sum(int num1);
int main () {
float n=0, num1=0, num2=0,sum=0,avg;
cout<<"Enter first number: ";
cin >> num1;
cout<<"Enter second number: ";
cin>> num2;

// Sum = (sum of integers from 1 to num2) - (sum of integers from 1 to (num1-1))
sum = (num2 * (num2+1) / 2) - (num1 * (num1-1) / 2);

// Avg = (sum divided by the number of values between the two endpoints, inclusive)
avg = sum / (num2 - num1 + 1);

cout<<"The total sum of range " << num1 << " to " <<num2<< " is: " <<sum<<endl;
cout<<"The average is: " <<avg;
cin.ignore();
cin.get();
}


This can be improved in a number of ways. For one, you should validate the user input to make sure that num2>=num1. Secondly, why are you using floats?


Oops I wrote 'Gaussian', I meant 'Gauss' :). Actually I'm not even sure if the sum of a sequence of integers is attributed to him...I heard a story about him finding it out or something...
Here are your problems:
1
2
3
n++;
sum=n*(n+1)/2;
n--;
I'm not sure what are you doing here, but you're doing it in a for loop, so sum gets recalculated every time. To find the sum of numbers in range [num1; num2] you could either use for loopfor(n = num1; n <= num2; n++) sum+=n; or simply use a formula sum = (num2 + num1)*(num2 - num1)/2; or something like that...
Also avg=sum/num2; is not a way to get the average. You could either use avg = sum/(num2-num1); or avg = (num1+num2)/2;.
The other flaws:
You are not using function sum(int num1), so why did you declare it?
n!=0 on line 11 is put where declarations should be, it never evaluated and in generally not needed here.
n++ and n-- on lines 13, 15 are odd. You know you could have written sum=(n+1)*(n+2)/2 instead, and the result would have been the same...
Lastly, note that your code and the code that I posted is only going to work if num2 > num1. You may want to swap them otherwise.
Last edited on
Topic archived. No new replies allowed.