Solved

average for numbers inputed

Posted on 2011-02-25
6
307 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 72
tripleUp challenge 7 74
countClumps  challenge 10 111
Python - ImportError: No module named 'urllib2' 2 154
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

910 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

17 Experts available now in Live!

Get 1:1 Help Now