### Mean

We have a code that calculates the mean of two numbers but we need to modify it so that it calculates the mean of all numbers between it two. Ex. Input 3 and 10. We want it to calculate the mean od 3,4,5,6,7,8,9, and 10 to get 6.5.

#include <iostream>
using namespace std;

int main()
{
// value is some positive number n
double total = 0; // total holds the sum of the first n positive numbers
int number; // the amount of numbers
double mean,x; // the average of the first n positive numbers

cout << "Please enter a positive integer" << endl;
while ( cin >> x)
{total+= x;
++number;
cout << "Enter
cin >> value;

if (value > 0)
{
for (number = 1; number <= value; number++)
{
total = total + number;
} // curly braces are optional since there is only one statement

mean = float(total) / value; // note the use of the typecast
// operator here
cout << "The mean average of the first " << value
<< " positive integers is " << mean << endl;
}
else
cout << "Invalid input - integer must be positive" << endl;

return 0;
}
seems like your number variable is not initialized.

try

 `` `` ``int number = 0;``

We have this and it calculates the mean starting at 1 and we want to be able to input two values and calculate the mean from the first value to the second.

#include <iostream>
using namespace std;

int main()
{
int value; // value is some positive number n
int total = 0; // total holds the sum of the first n positive numbers
int number; // the amount of numbers
float mean; // the average of the first n positive numbers

cout << "Please enter a positive integer" << endl;
cin >> value;

if (value > 0)
{
for (number = 1; number <= value; number++)
{
total = total + number;
} // curly braces are optional since there is only one statement

mean = float(total) / value; // note the use of the typecast
// operator here
cout << "The mean average of the first " << value
<< " positive integers is " << mean << endl;
}
else
cout << "Invalid input - integer must be positive" << endl;

return 0;
}
If someone could please help us, we would greatly appreciate it!!!!!
 ``123456789`` ``````double meanbetween(unsigned low, unsigned high) { unsigned num; if(low > high) num = low - high + 1; else num = high - low + 1; return num / 2 + low - (num % 2 ? 0 : .5); }``````

--> mik2718.

*facepalm*
m(
Last edited on
ok so you actually want:

 ``12345678910111213141516`` ``````double meanbetween(unsigned low, unsigned high) { if (low == high) return 0; if (low > high) swap(low, high); unsigned num = high - low; unsigned total = 0; for (unsigned i=low; i<=high; i++) { total += i; } double mean = (double)total/num; return mean; }``````

This method may be more intuitive but is far less effecient than calculating the total without a for loop by using sum of arithmatic series, where a = low and d=1 and n being the number of terms ...
closed account (o1vk4iN6)
Why have 2 parameters named "low" and "high" if you are just going to check and swap them anyways ? The user will see the function and would naturally think that they are responsible for passing them in the proper order, adding a redundant comparison.

 `` `` ``double MeanFromRange( unsigned start, unsigned count );``
Last edited on
whats wrong with

double meanbetween(int oneend,int otherend)
{
return double(oneend+otherend)/2.0
}

?
 Why have 2 parameters named "low" and "high" ...

I agree that the function MeanFromRange will force the users hand in a particular way - but that may not be best for users situation.

If user were to receive data relating to a 3d spatial grid, consisting of low and high value pairs describing particular ranges then these may not be ordered as <low, high> pair depending on relative fix point of observation/reference .... then instead of ordering these values yourself first and caluculating the number of elements, it may be more convenient to just pass them to a function like meanbetween to calculate the absolute value of the mean.

closed account (o1vk4iN6)
 I agree that the function MeanFromRange will force the users hand in a particular way - but that may not be best for users situation.

Either way naming of the variables is terrible for what it does.

The user will order them to match the naming. So they would have to check the source code to make sure it does the swap, although they wouldn't think to do that because the parameters are named high and low.

Can change it if you want to take a signed int instead and depending on the sign it'll choose the direction to go accordingly.

Last edited on
Topic archived. No new replies allowed.