# function returns 0

• 11-02-2008
alyeska
function returns 0
Hello,

I am trying to make a function which will return the maximum value in an array. This function returns 0. I figured the for loop will iterate over all the elements of the array. It looks like its not. Please help.

Thanks..

Code:

```float Maximum(float* item, int el) {         float max = 0.0f;         for (int i = 0; i < el; ++i)         {                 if (item[i] > item[i]+1)                         max = item[i];                 ++item[i];         }         return max; }```
• 11-02-2008
tabstop
Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; it's just never going to happen.)

Also, you don't care whether the number is larger than its neighbor; you care whether it's the largest number of all of them.
• 11-02-2008
alyeska
Code:

```Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; it's just never going to happen.)```
Thanks for pointing that out, tabstop.. I think I've fixed this part. Anyway, this time, it always returns the last element of the array,
whatever that is. Shouldn't the iterator move from element to element?

Code:

```float FindMax(float* item, int el) {         float max = 0.0f;         for (int i = 0; i < el; ++i)  //<-- iterator working?         {                 if (item[i] > item[i+1])                         max = item[i];                 else                         max = item[i+1];                 //++item[i];         }         return max; }```
• 11-02-2008
innuendo
alyeska,
is your array ordered in increasing order?
• 11-02-2008
tabstop
Quote:

Originally Posted by alyeska
Code:

```Well. How often is a number going to be greater than one larger than itself? (I mean you're testing whether 8 > 9, or -4 > -3; it's just never going to happen.)```
Thanks for pointing that out, tabstop.. I think I've fixed this part. Anyway, this time, it always returns the last element of the array,
whatever that is. Shouldn't the iterator move from element to element?

Yes. But that doesn't mean that you want to check the number next to it. Consider the array
1 3 7 5 4.
5 is bigger than 4 (the number after it), but that doesn't mean it's the maximum of the whole list.
• 11-02-2008
innuendo
Sorry alyeska,
I see now what the problem is.
Every time runtime exits the for loop the max variable is newly assigned. A possible solution'd be to create a tmpmax variable, assign the value to this tmpmax variable and transfer tmpmax value to max if it's bigger.
• 11-02-2008
CornedBee
No, that's not the problem. tabstop's post explains the problem.
• 11-02-2008
innuendo
Yes, it is actually the problem. The for loop is running through all members of the array, so you check one number and then the next after it.
tabstop is right saying that 5 is not the maximum of the whole list, hence the suggestion of creating a temporary variable.
Code:

```float FindMax(float* item, int el) {         float max = 0.0f;         for (int i = 0; i < el; i++)  //<-- iterator working?         {                 float tmpmax = max;                 if (item[i] > item[i+1])                 {                         cout << item[i] << " > " << item[i+1];                         tmpmax = item[i];                         cout << " max: " << max << endl;                                        }                                 else                 {                         cout << item[i] << " < " << item[i+1];                         tmpmax = item[i+1];                         cout << " max:" << max << endl;                 }                 if (tmpmax > max)                 {                         max= tmpmax;                 }         }         return max; }```
• 11-02-2008
cpjust
Code:

`if (item[i] > item[i+1])`
In addition to testing the wrong thing, your last test is always going to be testing the element beyond the end of the array, which is a huge bug.
• 11-03-2008
innuendo
Oops, sorry tabstop... I was being thick. I couldn't see the obvious.
• 11-03-2008
pobri19
Code:

```for (int i = 0; i < ARRAY_SIZE-1; i++) {         maxval = array[i];         if (maxval < array[i+1])                 maxval = array[i+1] }```
• 11-04-2008
matsp
Code:

```for (int i = 0; i < ARRAY_SIZE-1; i++) {         maxval = array[i];         if (maxval < array[i+1])                 maxval = array[i+1] }```
This will definitely fail if you have a sequence of 1 3 7 4 2, as the result will be 4. Think about it.

--
Mats