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.

2
  • 2
    your forloop in broken - check out any book or online tutorial on C for the correct syntax Commented Nov 6, 2009 at 21:05
  • @hollerTrain: Did you get my code from stackoverflow.com/questions/32920644/c-compare-numbers/… and twisted it somehow ?! Commented Oct 3, 2015 at 12:12

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

8 Comments

this is perfect! i assume i just do the opposite if i want to find the lowest number in an array?
Note that this assumes your array has at least one element: gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Zero-Length.html
Why not i++ or ++i instead of i += 1?
@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.
@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.
|
5

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)

2 Comments

what you said about the three parts is very information. thanks for this tip!
@R Samuel: I'd change "should-continue" to "termination condition", as that's what it is; it's the condition that terminates the loop.
1

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

normally it should be something like

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

Comments

1

include:

void main()
{
  int a[50], size, v, bigv;
  printf("\nEnter %d elements in to the array: ");

  for (v=0; v<10; v++)
    scanf("%d", &a[v]);

  bigv = a[0];

  for (v=1; v<10; v++)
  {
    if(bigv < a[v])
      bigv = a[v];
  }

  printf("\nBiggest: %d", bigv);
  getch();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.