This code finds the sum & average of the range of numbers that were chosen. I have

int begin = 0, end = 0,sum = 0.0, avg = 0;

because I get an error saying that all of those need to be initialized, yet by doing so it gives me the results to be zero's instead of the answer. How do I fix this?

Also, I have a feeling that my math is incorrect & if so, I would appreciate some guidance.

Thank you.

``````#include <iostream>
using namespace std;

void GetData(int begin, int end);
void Calc(int begin, int end, int sum, int avg);
void Display(int begin, int end, int sum, int avg);

int main()
{
int begin = 0,
end = 0,
sum = 0.0,
avg = 0;

GetData(begin, end);
Calc(begin, end, sum, avg);
Display(begin, end, sum, avg);

return 0;
}
void GetData(int begin, int end)
{
cout << "number to start at: ";
cin >> begin;
cout << "Number to end at: ";
cin >> end;
}
void Calc(int begin, int end, int sum, int avg)
{

while(begin < end)
{
int nn = begin;
nn++;

sum += nn;
avg = sum/nn;
}

}
void Display(int begin, int end, int sum, int avg)
{
cout << "the sum between " << begin << " & " << end << " is " ;
<< sum << " & " << endl << " average"<< avg;
}``````

## All 15 Replies

So then it would be

``````void GetData(int& begin, int& end);
void Calc(int begin, int end, int& sum, int& avg);``````

&

``````void GetData(int& begin, int& end)
{
cout << "number to begin at: ";
cin >> begin;
cout << "Number to end at: ";
cin >> end;
}
void Calc(int begin, int end, int& sum, int& avg)
{

while(begin < end)
{
int nn = begin;
nn++;

sum += nn;
avg = sum/nn;
}

}``````

correct? My problem now is that

``````void Display(int begin, int end, int sum, int avg)
{
cout << "Sum of numbers between "<< begin << endl << " & " << end  << " is: " << sum
<< endl << "with the average of "<< avg;
}``````

will only run if I enter the biggest number first, & it's suppose to be smallest first. :[ Is my math wrong and am I missing something?

will only run if I enter the biggest number first, & it's suppose to be smallest first.

which variable are you talking about?
plus could you explain your operation

When I run the program, and enter 3 as the begin number & 9 as the end number, I only get this:

number to begin at: 3
number to end at: 9

and not the display, sum of numbers between 3 & 9 is: ...

but if I enter 9 as the begin and 3 as the end, it works & I get sum of numbers between 9 & 3 is: -858993460 with the wrong answers.

``int nn = begin;``

put this outside the loop and use nn in the loop condition instead

Are my references correct? :/ I moved int nn = begin; out of the loop but nothing changed. I don't know what to do. T.T

``````#include <iostream>
using namespace std;

void GetData(int& begin, int& end);
void Calc(int begin, int end, int& sum, int& avg);
void Display(int begin, int end, int sum, int avg);

int main()
{
int begin,
end,
sum,
avg;

GetData(begin, end);
Calc(begin, end, sum, avg);
Display(begin, end, sum, avg);

return 0;
}
void GetData(int& begin, int& end)
{
cout << "number to begin at: ";
cin >> begin;
cout << "Number to end at: ";
cin >> end;
}
void Calc(int begin, int end, int& sum, int& avg)
{
int nn = begin;

while(begin < end)
{
nn++;
sum += nn;
avg = sum/nn;
}
}
void Display(int begin, int end, int sum, int avg)
{
cout << "Sum of numbers between "<< begin << endl << " & " << end  << " is: " << sum
<< endl << "with the average of "<< avg;
}``````

you forgot the

and use nn in the loop condition instead

``while(nn < end)``

Thank you!

I am having trouble with:

``````int nn = begin;

while(nn < end)
{
nn++;
sum += nn;
avg = sum/nn;
}``````

is there something I am missing or have incorrect?

Edit: The sum of 3 through 9 should be 42 instead I get -85899342.
I also should be getting 6 for the average but instead I get -95443713. :/

Thank you!

I am having trouble with:

``````int nn = begin;

while(nn < end)
{
nn++;
sum += nn;
avg = sum/nn;
}``````

is there something I am missing or have incorrect?

could you explain the "trouble" with it
...if it's the average put avg = sum/nn; after the loop

The sum of 3 through 9 should be 42 instead I get -85899342.
I also should be getting 6 for the average but instead I get -95443713. :/

hmm..I tried running the program and I was able to successfully get the sum and average? did you compile and saved the changed code?

Then it must be my computer? :[
Yes I did save it, compile and I even created a new file but i still got the negative numbers.

Thank you for all your help Zeroliken. :]

Then it must be my computer? :[

Did you solve it?
in case not yet, try using using cout statements for all the variables at all parts of the code to know where the problem is by knowing which of these variables is giving garbage value.
try it before and after the initializations and at the end of the functions

Hmmm, I will have to try that out later today after my afternoon class. I will let you know. :]

Did you solve it?

I put cout statements before, at the end and inside the loop. I found the mistake and my code now runs correctly. :] Thank you very much for helping me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.