C++ program for school

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?

J
lol
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int random(){
return 4;//guaranteed to be random
}

int main(){
for (int temp = 0; temp <= 10; temp++)
std::cout << "\nNumber : " << temp << " = " << random();
std::cout << "\nNumbers Above Average : 0";
}


edit: double forgot the average
Last edited on
He determined the 4 by dice roll. It's guaranteed to be random.
Still... awake... somehow...

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?

-Albatross
Oh albatross, you're right..but it's so fun.. I apologize jonsto, and second albatross's question.
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.


#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


void main()
{
int countNumber;
const int 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;
}
cout << "The average is: " << average << endl;

cin.get();

return;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


int main()
{
	int countNumber;
	const int 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
Here it is mate...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

int main ()
{
	const int 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;
	}
Thanks a lot guys.
Topic archived. No new replies allowed.