Average Program using an Array

Reply

Join Date: Sep 2008
Posts: 6
Reputation: csaund1 is an unknown quantity at this point 
Solved Threads: 0
csaund1 csaund1 is offline Offline
Newbie Poster

Average Program using an Array

 
0
  #1
Dec 2nd, 2008
How do I code a C++ program that used an array to find an average of positive numbers. Then outputs the result
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Average Program using an Array

 
0
  #2
Dec 2nd, 2008
sum them all up and divide by the size of the array. For example
  1. int a[3] = {2,4,6};
  2.  
  3. The sum is 2+4+6 = 12
  4. The average is 12/3 = 4
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 30
Reputation: Ψmon is an unknown quantity at this point 
Solved Threads: 0
Ψmon Ψmon is offline Offline
Light Poster

Re: Average Program using an Array

 
0
  #3
Dec 2nd, 2008
What is not clear to me is: does the array have only positive numbers or are you finding the average of only positive numbers?

If the first, then Ancient Dragon has the con
If the second, you'd need to pull out only the positive numbers and divide by their count.

Perhaps ...
int a[6] = {2,-3, 4, -5, 6, -7};

int b[6] = new int[];
loop through a, placing positive values in b
then ...
The sum is 2+4+6 = 12
The average is 12/3 = 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 372
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 69
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Average Program using an Array

 
0
  #4
Dec 3rd, 2008
Think about how you add up. Suppose I ask you to add the
number in the series 2,6,9. you don't go let me put the numbers into
an array. you simply keep a running total,

So in your case

start a total
total=0
then IF the number is >0 add to total
divide total by the number of >0 numbers (or 42 ;-)

Just because it is programming it doesn't mean that you can forget you kindergarden maths.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: Average Program using an Array

 
0
  #5
Dec 3rd, 2008
  1. define total = 0
  2. define array[n] = {n1, n2, n3, n4, ...}
  3. for 0 to n - 1
  4. total = total + array[n]
  5. print 'Average: '
  6. print total / n
  7. end
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC