Arrays

#include <iostream>
using namespace std;
int main ()
{
int score[5];
int total=0;
for(int a=0; a<=4; a++)
{
score[a]=0;
cout<<"Enter score for" <<a<< ":";
cin>> score[a];
total += score[a];
}
double x= total/5.0;
cout<<"The Average of x is "<<x<<endl;
return 0;
}

//I need the code on how to compute y as the average of the inputs that are greater or equal to x and print y.
x = 5
numbers = { -1, 12, 20, 4, 3, 25 }
What is the average of the numbers greater then x?

numbers_greater_then_x = { 12, 20, 25 }
average = (12 + 20 + 25) / 3 = 19

How did I find all the numbers greater then x?
Where did that last 3 come from?
If you can answer these questions, all you need to do is translate it into code!
Last edited on
see thats the thing, i understand it like that but i dont quite know how to put it into a code format, ive tried but cant quite do it
Show me what you tryed. I'll see if I can fix it up.
if(score[a] <= x)
double y=x/5.0;
cout<<" The average is "<<y<<endl;

its not right i know
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

#include <iostream>

using namespace std;

int main() {
	const int MIN = 10; //This is our "x", are minimum value. All numbers must be >= 10 in this example.
	const int N = 5; //This is the maximum number of times we will ask for a number, I'll ask for up to 5 numbers in this example
	
	int total = 0; //this is the running totle, so far it's 0
	int count = 0; //this is the number of inputs that were actually >= MIN, so far no inputs have been given
	
	for( int a = 0; a < N; a++ ) {
		//get the score from input, notice I only store it in a local variable, no need to fill an array here (but you could) 
		int score;
		cout << "Enter score for " << a << ":";
		cin >> score;
		
		//this part stops the loop if there is an error in the standard input stream,
		//	like if the user enters an End Of File (EOF) character (Ctrl-Z in windows, Ctrl-D in *nix)
		if( !cin.good() )
			break; //stop the loop, i.e. stop asking for input
		
		//check to see if the input is greater >= MIN, if it's not then do nothing
		if( score >= MIN ) {
			//if it is, then increase the totle, and increase the count
			total += score; //add the score to the totle (and store back into totle)
			count++; //add 1 to the count (and store back in count)
		}
	}
	
	double avg = (double)total / count; //we divide by count (not by N), also we must cast either the numerator or denominator to a double so our average isn't truncated to an int
	cout << "The Average of the numbers greater then or equal to " << MIN << " is " << avg << endl; //print the average
	
	return 0;
}


Please ask a question if you don't understand something.
I tryed to stay close to what you had to start with.
Also I removed the array, but you could add it back if you really needed it for something later in the program.
Last edited on
ok the thing is there should be two averages, first one as the question reads 1.Compute x as the average of the array. Print x. that i did no problem, second says Compute y as the average of those entries that are greater than or equal to x. Print y. Here is where im having the problem, and the question states that it has to be in an array.

Any assistance here please?
So try to modify the program I gave you. First just create the array, along with an extra pair of (total, count) variables to compute the average of all the entries. How you modify the for loop should be self evident, give it a shot! I'll help you out if it doesn't work right.
i dont understand the extra total and extra count, i already got one average just need to compute another, dont know how to go about it, did u think u could add whats needed after my program as i have it above
Although this could be done in a single for loop, it would probably help you understand it if you did this in three separate for loops.

1
2
3
4
5
6
7
8
9
10
11
for( int a = 0; a < N; a++ )
  { /* prompt for input, store in array */
  }
for( int a = 0; a < N; a++ )
  { /* pass the array counting and adding all the numbers */
  }
/* print x */
for( int a = 0; a < N; a++ )
  { /* pass the array counting and adding numbers > x */
  }
/* print y */
int score[5];
int total=0;
for(int a=0; a<=4; a++)
{ cout<<" Enter score for" <<a<< ":";
cin>> score[a];
}
for(int a=0; a<=4; a++)
{
total += score[a];
}
double x= total/5.0;
cout<<"The Average is "<<x<<endl;
for(int a=0; a<=4; a++)
{
i dont know how to get those that are greater or equal to x thats my question
How would you compute an average?
Think about it, my program was well commented, did you understand it?
i computed the average of x there but i dont know how to do that of y since y is all the entries greater than or equal to x
Run my program though an array, instead of getting input from cin. And assign MIN = previous average found.
Topic archived. No new replies allowed.