Chat on IRC |
need help with a base case form recursive function
Posted by matheo917 [send private reply] at November 08, 2001, 07:37:06 PM
i seem to have a huge problem with finding a proper base case for my recursive program.... can anyone help.... i just can't get it...
here's some code.... // Task: Implement (MaxArray), discussed in the section "Finding the // Largest Item in an Array," as a C++ function. What other // recursive definitions of (MaxArray) can you describe? // // Preconditions: Every recursive call must make the original program // smaller by at least a half. There must be a base case, // which in fact has to be reached by one of the recursive // calls to the function. // // Postconditions: At the end the result has to come out victorious and // the program will point to the largest number. // //////////////////////////////////////////////////////////////////////////// //--------------------------------------------------------------------------- // Preprocessor directives... #include <iostream> #include <cstdlib> using namespace std; //--------------------------------------------------------------------------- // Global constant variables... const int MaxIntegers = 10; // int Highest; //--------------------------------------------------------------------------- // Functions declarations... int MaxArray(int Array[], int First, int Last); int Compare(int Largest); //--------------------------------------------------------------------------- int main() { cout << "This program will ask you to input 10 integers. \n" << "After you have done so, the computer will display all \n" << "of these integers and display the one that is the largest \n" << "out of these 10 integers. \n\n\n"; int Array[MaxIntegers]; for (int i = 0; i < MaxIntegers; i++) { cout << "Please enter integer # " << i + 1 << endl; cin >> Array[i]; cout << endl; // flushes the buffer } cout << "These are the integers the you have entered: \n\n"; for (int j = 0; j < MaxIntegers; j++) { cout << Array[j] << ", "; } cout << "\n\n" << endl; // flushes the buffer int First = 0; int Last = (MaxIntegers - 1); int Highest = MaxArray(Array, First, Last); // function call... cout << "The largest integer is " << Highest << endl; system("pause"); return 0; } //--------------------------------------------------------------------------- // Function definition (implementation)... int MaxArray(int Array[], int First, int Last) { int Largest; int Biggest; int Mid = (Last + First) / 2; // finding midpoint if (Last = Last) { Largest = Array[Last]; // not necessarily true...check it.!!! Biggest = Compare(Largest); // have to send it here...somewhere... } // need to have a different base case else { MaxArray(Array, First, Mid); // recursive call... MaxArray(Array, (Mid + 1), Last); } // i have to send the results somewhere to somehow compare // then in term of size...... hmmm... ? and return the biggest one. return Biggest; } //--------------------------------------------------------------------------- // Function definition (implementation)... int Compare(int Largest) { int Biggest; if (Largest > Biggest) { Biggest = Largest; } return Biggest; } //--------------------------------------------------------------------------- i think the problem is in the function MaxArray in the "if" statement ... if(Last == First) seems to be a wrong base case or maybe something else is wrong can anyone give me a hand....???? thanx matheo917
Posted by taubz [send private reply] at November 09, 2001, 08:12:02 AM
Well the statement "if (Last = Last)" surely appears to be a mistake. Also you have various "Biggest" variables, none of which refer to the same thing, so your Compare function will behave completely randomly. I'm also not sure what you are trying to do, or why you're doing this recursively.
- taubz
Register as a new user | ||||