1,105,333 Community Members

Find Largest Number

Member Avatar
cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
iamthwee
Posting Sage
7,066 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 595 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

Hint: Is index correct?

Member Avatar
cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
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;
Member Avatar
cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
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
Member Avatar
cassyjack
Light Poster
36 posts since Jan 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
rosellejane
Newbie Poster
1 post since Feb 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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