• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
dante cass
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? I am looking for the largest value in the array and assigning it to "degree". How is it at odds?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

You said your professor said:
>> 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!