1.11M Members

Find Largest Number

 
1
 

I need a little help. Here are the specs:

Create a method called findLargest, which takes an array of integers as an input parameter and returns the array index of the largest integer in the array. If there are positions that are tied for largest, the method should return the first index with a largest value.
Examples:
data:{25,11,15,25}
==> 0
data:{2,11,15,25}
==> 3

Here is the line of code given:

public int findLargest(int[] data){ }

This is what I have so far.

int index = 0;
int largest;
largest = data[0];
for(int i =0; i < index; i++)
if(largest < data[i])
{
largest = data[i];
return data[i];
}

Not sure where to go from here.

 
0
 

Hint: Is index correct?

 
0
 
int i;
int index = 0;
int largest;
largest = data[0];
for(i =0; i < index++; i++)
if(largest < data[i])
{
largest = data[i];
}
return index;

This is what I came up with but it is not producing the correct code. Its not looping correctly. It returns the index of the second array if it is larger than the first.

 
0
 

what about this

int i;
int index = 0;
int largest;
largest = data[0];
for(i =0; i < data.lenght(); i++)
if(largest < data[i])
{
largest = data[i];
index = i;
}
return index;
 
0
 

Thanks It worked but I had to take the () out. What is the difference in using the data.lentgth and the index. I not quite sure what the .lenght references.

 
0
 

When do I finaly learn where to use this bracklets ;) ?
data.length will return size of your array so you do not have to exactly say loop only 4 times then stop, you just ask for size of array and loop till you get last element in array

your index is just variable which you declared and then you tried to increase its value with each loop. To do this with index = 1, you get infinitive loop.

Plus you don't have to declare "i" outside of for statement

Nice job cassyjack, you doing well. Nice to see some mebers which do not only ask for code but actualy do their workload

Question Answered as of 7 Years Ago by peter_budo and iamthwee
 
0
 

Thanks for the info. Im taking Java II as a prereq and It's killing me. I had Java I about 4 years ago. I really appreciate your help and the explanation.

 
-1
 

i dont know,....

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article