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

pbrockway2

What does the code output?

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

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

pbrockway2

Quote:

Originally Posted by know.how

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.

08-27-2011, 07:30 AM

know.how

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

08-27-2011, 07:35 AM

pbrockway2

Quote:

Originally Posted by know.how

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:

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

08-27-2011, 11:00 AM

JavaMan13

1 Attachment(s)

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

pbrockway2

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.