I am working on projects in my book that aren't required and I've been trying to figure out the best way to write a program that outputs 10 random numbers between 1 and 100, outputs the average of the 10 numbers, and outputs how many of the random numbers were above the average... Can someone help please?
This is just silly. It's one thing to tell someone their question is mal-formed, but it's another to mock them. I mean really... have the recent trolls tainted us so much?
jonsto... what have you tried to get your program working so far?
This is my code so far. Please tell me what I'm doing wrong... Also, I don't know what to do to make the program output how many numbers are above the average of the 10 numbers.
for (countNumber = 0; countNumber < ARRAY_SIZE; countNumber++)
{
sum += randomNumber[countNumber];
average = sum / 10;
}
cout << "The average is: " << average << endl;
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int countNumber;
constint ARRAY_SIZE = 10;
double randomNumber[ARRAY_SIZE];
srand(time(0));
for (countNumber = 0; countNumber < ARRAY_SIZE/* && countNumber >= 0*/; countNumber++)
{
randomNumber[countNumber] = rand() %100 + 1;
cout << " " << randomNumber[countNumber] << endl;
}
int sum = 0;
double average = 0;
for (countNumber = 0; countNumber < ARRAY_SIZE; countNumber++)
{
sum += randomNumber[countNumber];
//average = sum / 10;
}
average = double(sum) / 10;
cout << "The average is: " << average << endl;
cin.get();
return 0;
}
You got the code right except where you made your average. You need to make it outside the for function. And it would print an integer the way you did it. You need to have at least a double where you placed sum / 10. I casted sum into a double. One more thing. You don't need to check && countNumber >= 0
in the 1st for. It can't go under 0. It will end because of the countNumber < ARRAY_SIZE
#include <iostream>
usingnamespace std;
int main ()
{
constint n = 10;
int i;
double sredina;
int chlen;
int v[10];
for (i=0; i<n; i++) {
cout<<"Input numbers with index: "<<i<<endl;
cin>>chlen;
v[i]=chlen;
}
int sum=0;
for (i=0; i<n; i++){
sum+=v[i];
sredina = sum / 10;
}
cout<<"\n\n";
cout<<"The average is: "<<sredina<<endl;
int pogolemi=0;
for (i=0; i<n; i++)
if (sredina < v[i])
pogolemi+=1;
cout<<pogolemi<<" numbers are greater then the average."<<endl;
cin.get(); cin.get();
return 0;
}