tags:

views:

360

answers:

3

I am trying to find the max number in an array. I have created a function and I am using the following code:

int maxValue( int myArray [], int size)
{
    int i, maxValue;
    maxValue=myArray[0];

    //find the largest no
    for (i=0;i)
     {
     if (myArray[i]>maxValue)
     maxValue=myArray[i];
     } 
     return maxValue;
}

However I get a syntax error before ) token. What am I doing wrong and am I even doing this right? Any help would be greatly appreciated.

+6  A: 

You must pass a valid array with at least one member to this function:

#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int
maxValue(int myArray[], size_t size) {
    /* enforce the contract */
    assert(myArray && size);
    size_t i;
    int maxValue = myArray[0];

    for (i = 1; i < size; ++i) {
        if ( myArray[i] > maxValue ) {
            maxValue = myArray[i];
        }
    }
    return maxValue;
}

int
main(void) {
    int i;
    int x[] = {1, 2, 3, 4, 5};
    int *y = malloc(10 * sizeof(*y));

    srand(time(NULL));

    for (i = 0; i < 10; ++i) {
        y[i] = rand();
    }

    printf("Max of x is %d\n", maxValue(x, sizeof(x)/sizeof(x[0])));
    printf("Max of y is %d\n", maxValue(y, 10));

    return 0;
}

By definition, the size of an array cannot be negative. The appropriate variable for array sizes in C is size_t, use it.

Your for loop can start with the second element of the array, because you have already initialized maxValue with the first element.

Sinan Ünür
this is perfect! i assume i just do the opposite if i want to find the lowest number in an array?
HollerTrain
@HollerTrain: `if( myArray[i] < minValue )`
Sinan Ünür
Note that this assumes your array has at least one element: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Zero-Length.html
Nate Kohl
Why does this answer have no upvotes? +1
Chris Lutz
Why not i++ or ++i instead of i += 1?
Jonathan Leffler
@Nate Kohl - That's in GNU C, not ISO/ANSI C (even C99). It's a nonstandard extension and therefore Sinan's code doesn't need to account for it.
Chris Lutz
thank you so much!
HollerTrain
@Chris Lutz: Thank you ;-) @Jonathan Leffler: No particular reason.
Sinan Ünür
@Nate Kohl, Chris Lutz: In an argument list, `int myArray[]` is effectively the same as `int * myArray`. The only useful question is whether to test if a null pointer is passed in.
David Thornley
@David - I know, but @Nate was saying that you could pass an `int Array[0]` variable as a parameter, which would break this code because this code assumes (as it should) that any array(/pointer) passed has at least one element.
Chris Lutz
+5  A: 

A for loop has three parts:

for (initializer; should-continue; next-step)

A for loop is equivalent to:

initializer;
while (should-continue)
{
    /* body of the for */
    next-step;
}

So the correct code is:

for (i = 0; i < size; ++i)
R Samuel Klatchko
what you said about the three parts is very information. thanks for this tip!
HollerTrain
@R Samuel: I'd change "should-continue" to "termination condition", as that's what it is; it's the condition that terminates the loop.
Ken White
+1  A: 

the paren after the for seems to be missing some contents.

normally it should be something like

for (i=0; i<size; i++)
Carl Smotricz