Results 1 to 9 of 9
Thread: help with this code?
 11202009, 10:19 AM #1Member
 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); } }
 11202009, 10:50 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,233
 Blog Entries
 7
 Rep Power
 24
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
 11202009, 06:44 PM #3Member
 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;
 11212009, 12:12 AM #4Member
 Join Date
 Nov 2009
 Posts
 7
 Rep Power
 0
well i reedited 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.

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!
 11222009, 03:12 AM #6Member
 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); } }
 alright, i just can't figure it out. i'm all out of ideas.
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++; } }
 11222009, 04:51 AM #8Member
 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; 11222009 at 04:54 AM.
 11222009, 10:31 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,233
 Blog Entries
 7
 Rep Power
 24
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 (nonnested) 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 ;)
Similar Threads

Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08122009, 11:14 AM 
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08092009, 01:21 PM 
Why doesn't this code accept my code?
By PeterFeng in forum New To JavaReplies: 5Last Post: 02032009, 02:39 PM 
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03192008, 11:12 PM 
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04262007, 03:52 PM
Bookmarks