Solved

average for numbers inputed

Posted on 2011-02-25
6
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

635 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