Archived

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

GTI

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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites