Results 1 to 11 of 11
Thread: DisplayAboveAverage Pls help me
- 08-27-2011, 06:46 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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
- 08-27-2011, 07:13 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
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.
- 08-27-2011, 07:16 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
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); } } } }
- 08-27-2011, 07:19 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
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.
- 08-27-2011, 07:23 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
- 08-27-2011, 07:30 AM #6
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0
yes i tried it. this is reason for nothing happend in for loop.
- 08-27-2011, 07:35 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
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
- 08-27-2011, 08:48 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,377
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-27-2011, 11:00 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
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
- 08-27-2011, 11:59 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,538
- Rep Power
- 11
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.
- 08-28-2011, 03:49 PM #11
Member
- Join Date
- Aug 2011
- Posts
- 4
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks