# Thread: help with this code?

1. Member Join Date
Nov 2009
Posts
7
Rep Power
0

## help with this code?

hi guys, i'm just learning arrays, and i'm a bit stumped on this. i have to write a program that reads 10 numbers, computes their average, and then tells how many numbers are higher than the average. this is what i have right now:

Java Code:
``` import java.util.Scanner;
public class Prog17 {
public static void main (String[] args){
final int totalnum = 10;
int[] num = new int[totalnum];
Scanner input = new Scanner(System.in);

//Get Numbers
for (int i = 0; i <num.length; i++){
System.out.print("Enter a number: ");
num[i] = input.nextInt();
}
//Calculations for average
double total = 0;
int counter = 0;
for(int i = 0; i < num.length; i++){
total += (num[i])/10;
if ( num[i] > total)
counter++;
}
System.out.print(counter);

}

}```
i did the average of 1,2,3,4,5,6,7,8,9,10 and got the average of 1. but when i try to get the numbers bigger than the average, it says 10, but 1 > 1 is false, obviously, so it should be 9. what's wrong with it?  Reply With Quote

2. ## You're dealing with ints here and they have int divisions, i.e. 9/10 == 0 instead of 0.9. (the remainder is discarded); you have to add all your numbers first and divide the total by 10.0 at the end.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Oct 2009
Location
Rotterdam
Posts
52
Rep Power
0

## Like Jos said: you need to cast your integers to doubles. I recommend you replace your 10 in the division statement by the variable totalnum (or num.length).
To cast them both it needs to look something like this:
Java Code:
`total += (double)num[i] / (double)totalnum;`
Also, you forgot to print total.  Reply With Quote

4. Member Join Date
Nov 2009
Posts
7
Rep Power
0

## well i re-edited my code, and i am now getting the correct average. but i'm still stumped on how to get the elements of my array to compare it to the average so i can see how many numbers are larger than the average.  Reply With Quote

5. ## Calculate the average, and then loop again through the array, with each loop, checking to see if the the current number is higher or lower than the average.

Give it your best try and then post your attempt up here and we can take a look at it. Much luck!  Reply With Quote

6. Member Join Date
Nov 2009
Posts
7
Rep Power
0

## alright, i just can't figure it out. i'm all out of ideas. here is my code now.

Java Code:
``` import java.util.Scanner;
public class Prog17 {
public static void main (String[] args){
final int totalnum = 10;
double[] num = new double[totalnum];
Scanner input = new Scanner(System.in);

//Get Numbers
for (int i = 0; i <num.length; i++){
System.out.print("Enter a number: ");
num[i] = input.nextDouble();
}
//Calculations for average
double total = 0;
double average = total /num.length;
int counter = 0; //numbers above average

for(int i = 0; i < num.length; i++){
total += (num[i]);
while(num[i] > average){
counter++;
}
}
System.out.print("Numbers above average is: " +counter);
}
}```  Reply With Quote

7. ## alright, i just can't figure it out. i'm all out of ideas.
If you continue with a defeatist attitude you may as well give up now and quit this course. Serious.

Java Code:
```		for(int i = 0; i < num.length; i++){

total += (num[i]); // what is this line supposed to be doing?

while(num[i] > average){ // would a while loop or an if block be better here?
counter++;
}
}```  Reply With Quote

8. Member Join Date
Nov 2009
Posts
7
Rep Power
0

## ok. well, i spent more time on it, and i guess i just need to calculate things seperately, and it worked a few times. are there any flaws?

Java Code:
``` import java.util.Scanner;
public class Prog17 {
public static void main (String[] args){
final int totalnum = 10;
double[] numList = new double[totalnum]; //Array
Scanner input = new Scanner(System.in);

//Get numbers
for (int i = 0; i <numList.length; i++){
System.out.print("Enter a number: ");
numList[i] = input.nextDouble();
}
//Summing the elements
double total = 0;
for(int i = 0; i < numList.length; i++){
total += numList[i];
}
//Average Calculation
double average = total / numList.length;
int counter = 0; 	//numbers above average

for(int x = 0; x < numList.length; x ++){
if(numList[x] > average)
counter++;
}
System.out.print("Numbers above average: " + counter);
}
}```
Last edited by glopez09; 11-22-2009 at 03:54 AM.  Reply With Quote

9. ##  Originally Posted by glopez09 ok. well, i spent more time on it, and i guess i just need to calculate things seperately, and it worked a few times. are there any flaws?
I didn't see any flaws; that version does what it has to do; a (very) minor nitpick: use one variable name consistently when you have a few (non-nested) loops; first you use variable name 'i' when you calculate the total of all numbers and next you use 'x' when you want to find numbers larger than the average; use 'i' in both cases. This doesn't influence the working of your program but using 'i' and 'x' makes the eyebrows of an old sod (like me) fronze. It's not very important though ...

kind regards,

Jos (<--- old nitpicker ;-)  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•