Skip to main content
gamedev.net
🔒 Locked

Help !!!!

Started by GTI 4 replies 350+ views
Original Post
GTI
GTI
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
dMDI
dMDI
What is that for loop about?
"I don''t know with what weapons the third world war will be fought, but I know the fourth will be fought with sticks and stones." Einstein
dMDI
dMDI
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.
"I don''t know with what weapons the third world war will be fought, but I know the fourth will be fought with sticks and stones." Einstein
Ready4Dis
Ready4Dis

  
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
GTI
GTI
Thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.