Writing a program with an array that averages 10 numbers

Reply

Join Date: Mar 2008
Posts: 3
Reputation: brittany435 is an unknown quantity at this point 
Solved Threads: 0
brittany435 brittany435 is offline Offline
Newbie Poster

Writing a program with an array that averages 10 numbers

 
0
  #1
Mar 26th, 2008
Hello,

I am working on a small program that reads ten numbers, computes their average, and finds out how many numbers are above average. This is the set up I have so far:

#include <iostream>
using namespace std;

int main()
{
double myList[10];

myList[0] = 89
myList[1] = 56
myList[2] = 34
myList[3] = 98
myList[4] = 87
myList[5] = 94
myList[6] = 100
myList[7] = 69
myList[8] = 52
myList[9] = 73

return 0;
}


.... Now I just need to know how to use it!

Thanks for all the help!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 11
Reputation: Miyuki is an unknown quantity at this point 
Solved Threads: 0
Miyuki Miyuki is offline Offline
Newbie Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #2
Mar 26th, 2008
For average, try

double average;
average = ( myList[0] + myList[1] ... ) / 10;

A for loop is really better, but I will let you try it first.

For number above average

int i,numabove;
for ( you do this part )
if ( myList[i] > average ) numabove++

Finally output numabove
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: brittany435 is an unknown quantity at this point 
Solved Threads: 0
brittany435 brittany435 is offline Offline
Newbie Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #3
Mar 26th, 2008
#include <iostream>
using namespace std;

int main()
{
// Declare Variables
double myList[10];
double average;


// Define Array
myList[0] = 89;
myList[1] = 56;
myList[2] = 34;
myList[3] = 98;
myList[4] = 87;
myList[5] = 94;
myList[6] = 100;
myList[7] = 69;
myList[8] = 52;
myList[9] = 73;

//calculate average
average = ( myList[0] + myList[1] + myList[2]+ myList[3]+ myList[4]+ myList[5]
+ myList[6]+ myList[7]+ myList[8]+ myList[9]) / 10;

// Display average
cout <<"The average score is " << average <<"\n\n";


//Find out how many numbers are above average
int i;
for (i = 0; i > average; i++)
int numabove;
if (myList[i]>average)
numabove++;

cout << "The numbers above average are" << numabove;



return 0;
}

This is what I have so far but I can't get the for loop wrong. I'm not initializing numabove and I'm not sure how.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 11
Reputation: Miyuki is an unknown quantity at this point 
Solved Threads: 0
Miyuki Miyuki is offline Offline
Newbie Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #4
Mar 26th, 2008
You want the for loop to count 0 to 9.

In
for (i = 0; i > average; i++)
set the middle part to i < 10
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Compton11 is an unknown quantity at this point 
Solved Threads: 0
Compton11 Compton11 is offline Offline
Light Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #5
Mar 26th, 2008
Brittany, you shouldn't declare variables within a loop...Here's what you should do:


#include <iostream>
using namespace std;

int main()
{

// Declare Variables
double myList[10];
double average;
double sum = 0;
int numabove = 0;

// Define Array
myList[0] = 89;
myList[1] = 56;
myList[2] = 34;
myList[3] = 98;
myList[4] = 87;
myList[5] = 94;
myList[6] = 100;
myList[7] = 69;
myList[8] = 52;
myList[9] = 73;

//calculate average

//Make a loops traversing all of the 10 values in the myList array 
//and add each one to a variable named sum
for (int i = 0; i < 10; i++)
      sum += myList[i];

//calculate average by getting that sum and dividing it by 10
average = (sum/10);

// Display average
cout <<"The average score is " << average <<"\n" << endl;


//Find out how many numbers are above average by using a loop

for (int i = 0; i < 10; i++)
{
    if (myList[i]>average)
          numabove++;
}

cout << "The numbers above average are: " << numabove << endl;



return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 3
Reputation: brittany435 is an unknown quantity at this point 
Solved Threads: 0
brittany435 brittany435 is offline Offline
Newbie Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #6
Mar 26th, 2008
Originally Posted by Miyuki View Post
You want the for loop to count 0 to 9.

In
for (i = 0; i > average; i++)
set the middle part to i < 10
I was able to get that working, now I need the user to input the scores. I have it all set up, but I'm getting some crazy average.


#include <iostream>
using namespace std;

int main()
{
// Declare Variables
const int TOTAL_NUMBERS = 10;
int numbers[TOTAL_NUMBERS];
double average;

//Read all numbers
for (int i = 0; i < TOTAL_NUMBERS; i++)
{
cout << "Enter a score: ";
cin >> numbers[i];
}

//Find the average
average = ( numbers[TOTAL_NUMBERS] + numbers[TOTAL_NUMBERS]) /10;

// Display average
cout <<"The average score is " << average <<"\n\n";


return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Compton11 is an unknown quantity at this point 
Solved Threads: 0
Compton11 Compton11 is offline Offline
Light Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #7
Mar 26th, 2008
Brittany, you can't do cin >> number[i]..That will not read a value and store it in the array...Do something like this: ...Remember to add to my reputation!

#include <iostream>
using namespace std;

int main()
{
// Declare Variables
const int TOTAL_NUMBERS = 10;
int numbers[TOTAL_NUMBERS];
double average;
double sum = 0.0;
int num;

	//Read all numbers
	for (int i = 0; i < TOTAL_NUMBERS; i++)
	{
		cout << "Enter a score: ";
		cin >> num;
		numbers[i] = num;
		sum += numbers[i];
	}

	//Find the average
	average =  sum/10;

	// Display average
	cout <<"\nThe average score is " << average <<"\n\n";


return 0;
}
Last edited by Compton11; Mar 26th, 2008 at 5:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Writing a program with an array that averages 10 numbers

 
0
  #8
Mar 26th, 2008
Originally Posted by Compton11 View Post
Brittany, you can't do cin >> number[i]..That will not read a value and store it in the array...Do something like this: ...Remember to add to my reputation!

  1. cin >> num;
  2. numbers[i] = num;
  1. cin >> numbers[i];
works fine. The way you did it works too, but involves an extra step. You don't have to read from cin into an integer, then assign that integer to the array. Reading it directly from cin into the array works too.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Writing a program with an array that averages 10 numbers

 
0
  #9
Mar 26th, 2008
Originally Posted by Compton11 View Post
Brittany, you can't do cin >> number[i]..That will not read a value and store it in the array...Do something like this: ...Remember to add to my reputation!
That is exactly how to read in a value into the array. Don't know where you get the idea it can't.


  1. //Find the average
  2. average = ( numbers[TOTAL_NUMBERS] + numbers[TOTAL_NUMBERS]) /10;
Since when is adding two numbers together then dividing by 10 giving an average?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC