RSS Forums RSS

Average Program using an Array

Please support our C++ advertiser: Programming Forums
Reply
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

  #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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 13,862
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: 1230
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Average Program using an Array

  #2  
Dec 2nd, 2008
sum them all up and divide by the size of the array. For example
int a[3] = {2,4,6};

The sum is 2+4+6 = 12
The average is 12/3 = 4
Reply With Quote  
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

  #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  
Posts: 302
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 
Solved Threads: 56
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Average Program using an Array

  #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  
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

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

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 775 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:34 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC