Results 1 to 11 of 11
  1. #1
    JavaMan13 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default DisplayAboveAverage Pls help me

    what's wrong with my code? why it does not display the above average code?
    I using a vista and I try it in jdk6.27 and even jdk 7
    plsss help me this is the code:

    Java Code:
    public class DisplayAboveAverage {
    	public static void main(String[]args)
    	{
    		int num[]={12,25,33,42,56,71,84,96};
    		int sum = 0;
    		
    		for(int i=0; i<num.length; i++)
    			sum = sum + num[i];
    		
    		double average = sum / num.length;
    		{
    			System.out.print("Average is: "+average);
    		}
    		if(average >= num.length)
    		{
    			for(double i=average; i<=num.length; i++)
    			{
    				System.out.println("Above average are: "+i);
    			}
    		}
    	}
    }
    Last edited by pbrockway2; 08-27-2011 at 07:12 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Hi, welcome to the forums.

    When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code is readable when it appears here. There is also a # button in the pane were you compose your post which will add the tags.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What does the code output?

    By the way you don't need the curly brackets around the first print statement:

    Java Code:
    public static void main(String[]args)
    	{
    		int num[]={12,25,33,42,56,71,84,96};
    		int sum = 0;
    		
    		for(int i=0; i<num.length; i++)
    			sum = sum + num[i];
    		
    		double average = sum / num.length;
    		System.out.print("Average is: "+average);
    
    		if(average >= num.length)
    		{
    			for(double i=average; i<=num.length; i++)
    			{
    				System.out.println("Above average are: "+i);
    			}
    		}
    	}
    }

  4. #4
    know.how is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    In this prog, in the last line in for loop, you set the value of (i=average;i<=num.length;i++), then how value of i will decrease bcoz i=average is maximum value and it will decrease to num.length, but i++ will increase the value. So change the i++ to i-- you will get your desired output.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by know.how View Post
    change the i++ to i-- you will get your desired output.
    Did you try this?

    If the output in the second for loop is never happening that cannot be because of anything in the increment step of that loop.
    Last edited by pbrockway2; 08-27-2011 at 07:26 AM.

  6. #6
    know.how is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    yes i tried it. this is reason for nothing happend in for loop.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by know.how View Post
    yes i tried it. this is reason for nothing happend in for loop.
    Then your java is broken. Changing i++ to i-- does not result in any of the data values being printed:

    Java Code:
    C:\>type DisplayAboveAverage.java
    public class DisplayAboveAverage {
            public static void main(String[]args)
            {
                    int num[]={12,25,33,42,56,71,84,96};
                    int sum = 0;
    
                    for(int i=0; i<num.length; i++)
                            sum = sum + num[i];
    
                    double average = sum / num.length;
                    {
                            System.out.print("Average is: "+average);
                    }
    
                    if(average >= num.length)
                    {
                            for(double i=average; i<=num.length; i--)
                            {
                                    System.out.println("Above average are: "+i);
                            }
                    }
            }
    }
    C:\>javac -cp . DisplayAboveAverage.java
    
    C:\>java -cp . DisplayAboveAverage
    Average is: 52.0
    C:\>
    Last edited by pbrockway2; 08-27-2011 at 07:37 AM. Reason: changed code, and reran

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose your array contains two numbers: 10 and 20; the average obviously is 15; now think: what does this value have to do with the length of the array? Answer: nothing; your code mixes up values and index values. i.o.w. the logic of your program is broken.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    JavaMan13 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Thnx to all who reply! and this is the pic of my code.
    can anyone suggest me on how can I display all the above average int?
    The output that I want to get is suppose to be like this:
    Average is: 52.0
    Above average are: 56
    Above average are: 71
    Above average are: 84
    Above average are: 96
    Attached Thumbnails Attached Thumbnails DisplayAboveAverage Pls help me-displayaboveaverage.jpg  

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    All you seem to have done is follow the bogus advice about changing i++ to i--. As already noted this will not result in any data values being displayed.

    Think about how you decided what the output should be. What thought process was involved? How did you examine the array and decide which values were greater than the average?

    Start with a very clear description of the process. And then write code.

    If you think about it the condition if(average>num.length) - =="if the average is bigger than the length of the array" - has nothing to do with how you located the data elements. Likewise the for loop, for(double i=average; i<=num.length; i--) - =="start from the average value and while it is less than the length of the array keep subtracting one". Basically that last bit of the code is wrong. But correcting it will start with forming a plan of attack: a precise complete description of the process you yourself used to decide what the output should be.

  11. #11
    know.how is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    check out this program. i am using java 1.7 and output is coming.

    [ spoonfeeding deleted ... ]
    Last edited by JosAH; 08-28-2011 at 03:53 PM.

Tags for this Thread

Posting Permissions

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