Results 1 to 9 of 9
  1. #1
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default 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?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    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.

  4. #4
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  6. #6
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

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

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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++;
    			}
    		  }

  8. #8
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default

    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 04:54 AM.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by glopez09 View Post
    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 ;-)

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 12:14 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 PM
  4. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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