#### Archived

This topic is now archived and is closed to further replies.

# Help !!!!

## Recommended Posts

Hi I have just started learning to program and have a program with a bit of work I have to do.I''ve spent the last few days struggling with it so any help would be appreciated. The FindMax()Function given below is used to find the max. value in an array of integers which has a start address in memory defined by the pointer Vals. The number of elements in the array is defined by the parameter NumEls. THIS FUNCTION PROVIDES AN INCORRECT RESULT int FindMax(int *Vals, int NumEls) { int i, Max = *Vals++; for(i=1; i

##### Share on other sites
What is that for loop about?

##### Share on other sites
Sorry it didnt copy right, heres how it should be

int FindMax(int *Vals, int NumEls)
{
int i, Max = *Vals++;

for(i=1; i<NumEls; ++i)
{
if(Max < *Vals++) Max = *Vals;
}

return Max;
}

##### Share on other sites
Not got a compiler here at college, but off the top of my head:

int Max (int array[], int length) {    int max = array[0];    for (int i = 1; i < length; i++) {       if (length > max)           max = length[i];    }    return max;}

Try that.

##### Share on other sites

  int FindMax(int *Vals, int NumEls){int i, Max = *Vals++;for(i=1; i<NumEls; ++i){/* You''re incrementing Vals BEFORE you set Max = *Vals!!!if(Max < *Vals++) Max = *Vals;*/if(Max < *Vals) Max = *Vals;Vals++;}return Max;}

Other than that, it should work how I wrote it.

Billy - BillyB@mrsnj.com

Thanks

• ### Forum Statistics

• Total Topics
628333
• Total Posts
2982139

• 9
• 24
• 9
• 9
• 13