Results 1 to 9 of 9
Thread: help with this code?
- 11-20-2009, 09:19 AM #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:
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?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); } }
- 11-20-2009, 09:50 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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, 05:44 PM #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:
Also, you forgot to print total.Java Code:total += (double)num[i] / (double)totalnum;
- 11-20-2009, 11:12 PM #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.
-
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, 02:12 AM #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); } }
-
If you continue with a defeatist attitude you may as well give up now and quit this course. Serious.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++; } }
- 11-22-2009, 03:51 AM #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.
- 11-22-2009, 09:31 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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 ;-)
Similar Threads
-
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Why doesn't this code accept my code?
By PeterFeng in forum New To JavaReplies: 5Last Post: 02-03-2009, 01:39 PM -
I need help fixing my code.. or non code?
By MrHuggykins in forum New To JavaReplies: 1Last Post: 03-19-2008, 10:12 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks