| | |
Average Program using an Array
![]() |
sum them all up and divide by the size of the array. For example
C++ Syntax (Toggle Plain Text)
int a[3] = {2,4,6}; The sum is 2+4+6 = 12 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.
•
•
Join Date: Mar 2007
Posts: 30
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2008
Posts: 372
Reputation:
Solved Threads: 69
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.
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.
C++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Writing a program with an array that averages 10 numbers (C++)
- Reading external data into array of struct (Visual Basic 4 / 5 / 6)
- Please help in this program array (C++)
- number of times each number in array occurs? (C)
- Array data not setting (C++)
- question about array (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Simple Craps Game
- Next Thread: Array Question
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ cdialogbar char class classes code coding compile console conversion convert count delete deploy desktop developer directshow dissertations dll double-linkedlist download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loan loop looping loops map math matrix memory multiple news node number online output pagerank pointer problem program programming project python random read recursion recursive reference risk rpg string strings superclass temperature template test text text-file tree tutorial url validator variable vector video win32 windows winsock wordfrequency wxwidgets






