c++ - Recursively find the largest number in an integer array -
i required find largest number in integer array recursively. function crashed cannot find bug. here's c++ code:
void numbers::compare(int size) { if(size == 0) cout << "you must enter number!" << endl; if(size ==1) cout << "the max number " << numarray[0] << endl; if(size >1) { if(numarray[size-1] > numarray[size-2]) { int temp = 0; numarray[size-1] = temp; temp = numarray[size-2]; numarray[size-2]= numarray[size-1]; size --; } size = size -1 ; compare(size); } }
here problem:
int temp = 0; numarray[size-1] = temp; // set numarray[size-1]=0 temp = numarray[size-2]; numarray[size-2]= numarray[size-1];// set numarray[size-2]= 0 size --;
you set entire array 0.
what meant do:
int temp = numarray[size-2]; numarray[size-2] = numarray[size-1]; numarray[size-1]= temp size --;
note change array in process, not sure if meant do.
Comments
Post a Comment