Solved

average for numbers inputed

Posted on 2011-02-25
6
306 Views
Last Modified: 2012-05-11
The average kept coming out wrong.
int add = 0;
float total = 0;
for(i = 0; i < 9; i++) {
       total += array[i ];
       add++;
}

float average = total/9;

why does the average not add up right, im using the basic numbers 1, 2,3, 4, 5, ,6 ,7, 8, 9 so i know the correct average.
0
Comment
Question by:HelpMeMaggi
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 34986162
You should write it as follows:

int add = 0;
float total = 0;
for(i = 0; i <= 9; i++) {
       total += array[i ];
       add++;
}

float average = total/add;
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986166
Sorry that was incorrect, it should be:

int add = 0;
float total = 0;
for(i = 0; i < 9; i++) {
       total += array[i ];
       add++;
}

float average = total/add;
0
 

Author Comment

by:HelpMeMaggi
ID: 34986173
I have, the average comes out wrong still.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 34986186
>  im using the basic numbers 1, 2,3, 4, 5, ,6 ,7, 8, 9
the ,, between the 5 and 6 would be a syntax error
float average = total/9.0;
or
 float average = (float)total/add;
will do a floating  division rather than an integer division
0
 
LVL 24

Expert Comment

by:jimyX
ID: 34986219
What is the result that you get?
0
 

Author Comment

by:HelpMeMaggi
ID: 34986227
That worked, Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
centeredAverage challenge 8 123
How to creat good software interface ? 1 57
FizzBuzz challenge 9 73
scoresSpecial  challenge 13 42
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now