Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Largest Index in Array ( title edited ), using a for loop

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Here is what I have so far.

My Prof. said this :
>> Your following code finds the index of the largest coefficient in the array.
>> It should find the largest index i with a non-zero coefficients[i] value.
>> You should change the logic to do that. (Start with degree = 0.)

i changed it to degree = 0 as you can see.
Is that all I had to change? My code is looping through the coefficients array and assigned "degree" to the largest value of the coefficients array index right?
Also, is the check of the non-zero coefficient correct? Thank you.

Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
First, come up with some test inputs that you already know the answers to. Be sure to test the corner cases also:

Input
//your requirements are actually silent as to how these cases should be treated
[] // the empty array
[0]
[0, 0]

//These are well defined, but what should they return?
[1]
[-1]
[1, 0]
[0, 1]
[-1, 1]
[1, -1]
[0, 1, 0, 1]
[1, 0, 1, 0]

Coming up with good test cases can be just as difficult as writing correct code. But it is just as important.

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
Oh, and the topic of this thread is at odds with the requirements you state in your posting. You should not be looking for the largest value in the array at all.

dante cass
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
What do you mean? I am looking for the largest value in the array and assigning it to "degree". How is it at odds?

Ranch Hand
Posts: 952
• Number of slices to send:
Optional 'thank-you' note:

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:

dante cass wrote:What do you mean? I am looking for the largest value in the array and assigning it to "degree". How is it at odds?

>> It should find the largest index i with a non-zero coefficients[i] value.

Those are not the same things.

dante cass
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
Ok, so I am now lost. Do you think I should be finding the largest index? or the largest value at an index.

Also with the testing you suggested, I don't really know how to test it seeing I cant compile it because the program isn't finished.

If I am finding the largest index i with a non zero coefficient why would he want me to do a for loop? Why wouldn't I just use an if else statement with (coefficients.length-1)?

And isnt that why this code is doing?

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
You wrote that your professor asked you to find the largest index not the largest value. That is why I wrote those test cases. You have to understand the requirements and know what the answers should be before you write the code.

dante cass
Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
I changed the title for you, happy? obviously I misunderstood what he said.

I understand that you need to understand the assignment before writing the code. That goes for anything in life. I obviously thought I understood the assignment and didn't.

I appreciate your help but if your going to be snide about it then I'd rather you not respond at all. Good day.

Garrett Rowe
Ranch Hand
Posts: 1296
• Number of slices to send:
Optional 'thank-you' note:
I was trying to help you without giving you the answer. I was also trying to help you come up with a strategy for solving the problem. I didn't mean that you should feed the inputs I gave into your uncompilable program. What I meant was that you should start with inputs for which you know the answer, and then write the code and make sure what the program does is what you expect.

Marshal
Posts: 28271
95
• Number of slices to send:
Optional 'thank-you' note:

dante cass wrote:Also, is the check of the non-zero coefficient correct?

Ask yourself: what does it make the code do, that it wouldn't do without that check?

 So it takes a day for light to pass through this glass? So this was yesterday's tiny ad? We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser