help with this code?

Printable View

• 11-20-2009, 10:19 AM
glopez09
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:

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?
• 11-20-2009, 10:50 AM
JosAH
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
• 11-20-2009, 06:44 PM
Arnold
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:
Code:

`total += (double)num[i] / (double)totalnum;`
Also, you forgot to print total.
• 11-21-2009, 12:12 AM
glopez09
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.
• 11-21-2009, 12:21 AM
Fubarable
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!
• 11-22-2009, 03:12 AM
glopez09
alright, i just can't figure it out. i'm all out of ideas. here is my code now.

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);                 }         }```
• 11-22-2009, 03:23 AM
Fubarable
Quote:

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.

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++;                         }                   }```
• 11-22-2009, 04:51 AM
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?

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);         } }```
• 11-22-2009, 10:31 AM
JosAH
Quote:

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 ;-)