0

I have defined 16 variables and assigned them with different kind of values.

int block0 = 5;
int block1 = 6;
int block2 = 8;
int block3 = 25;
int block4 = 8;
int block5 = 23;
int block6 = 2;
int block7 = 1;
int block8 = 6;
int block9 = 4;
int block10 = 5;
int block11 = 7;
int block12 = 15;
int block13 = 4;
int block14 = 5;
int block15 = 8;

How can i find the biggest variable among all using while or for loop?

Joseph
  • 117,725
  • 30
  • 181
  • 234
Michael Gamage
  • 57
  • 1
  • 1
  • 5

4 Answers4

3

first you have to make them into an array

int[] intArr = new int[16];

int[0] = 5;
int[1] = 6;
...

and then you can iterate

int max = 0;
int maxIndex = 0;

for(int i=0; i<intArr.length; i++)
{
    if(max < intArr[i])
        maxIndex = i;
}
3

You should, but if you can't use arrays, you can use Math.max(), like this beautiful code:

int max = Math.max(block0, Math.max(block1, Math.max(block2, Math.max(block3, Math.max(block4, Math.max(block5, Math.max(block6, Math.max(block7, Math.max(block8, Math.max(block9, Math.max(block10, Math.max(block11, Math.max(block12, Math.max(block13, Math.max(block14, block15)))))))))))))));

Unless they are fields in a class, you cannot iterate (using a for or while, as you say) through those variables (local variables). You only option other than Math.max() is a huge if statement. Compared to it, though, Math.max() looks pretty.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
2

If you really can't use arrays, make your own method:

public static int maxValue(int... values) {
        int maximum = Integer.MIN_VALUE;

        for(int x : values)
            maximum = (x > maximum) ? x : maximum;

        return maximum;
    }

That way you can call it with multiple arguments, eg. by saying

int max = maxValue(block0, block1, /*etc*/, block15);

@acdcjunior 's solution with multiple Math.max methods works of course, too.

felix fritz
  • 462
  • 2
  • 5
  • 21
0

it's rather easy..

Just do a simple sort..

function findlarge()
{
   var largest = 0;
   for (i = 0; i < 15; ++i)
   {
     largest = ((block+i) > block+(i+1)) ? block+i : block+i+1; 
   }
   return largest;
}

please note you'd need to address the incorrect variable names I used..but this is how I'd sort out the largest..

Good luck :)

Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
  • 1
    You can't do what you seem to think you can do. If `i` is 1, `block+i` just adds 1 to `block`, it does not get you the value of `block1`. – James Montagne May 17 '13 at 00:11
  • 1
    @Dory In java you can write this code and you will not have an error, but it will not produce what you think – Ricardo Cacheira May 17 '13 at 00:16
  • Guys I know this..The tag also said javascript :) and besides please see the note I had bellow..I was just suggesting the logic, not more than that. – Dory Zidon May 17 '13 at 00:19
  • 1
    But you can't "suggest the logic" if that involves tools that do not exist in the entourage of the solution (as you cannot say "just use eval()"). Now that it is clear that it is only Java related, you should remove this answer. – SJuan76 May 17 '13 at 00:20