This little program demonstrates the recursion technique in three different settings. It includes recursive algorithms to search an array, reverse a number, and find the greatest common divisor of two numbers.
// Boyd Turley #include <iostream> #include <string> #include <fstream> #include <iomanip> //required to use setw() #include <cstdio> using namespace System; using namespace std; ///////////////////////////////////// Globals const int TABLE_ROWS = 30; const int TABLE_COLS = 20; int table[TABLE_COLS][TABLE_ROWS]; int grid[599]; //////////////////////////////////// FXN Defs. bool posWhole(int checkme); void NumReverse(int reverseme); void setOrder(int num1, int num2); int GCD(int num1, int num2); void gridSearch(); ///////////////////////////////////////////////////////////////////////////////////////// MAIN int main(array<System::String ^> ^args) { char thechoice = 0; while(thechoice != '!') { system("cls"); cout << "Recursion Menu:"<<endl << "S. Search an array for a number." << endl << "M. Mirror a number." << endl << "G. Greatest Common Devisor." << endl << "Q. Quit Program." << endl; cin >> thechoice; switch(thechoice) { int num; case 'S': case 's': { system("cls"); gridSearch(); cout << endl; system("pause"); break; } case 'M': case 'm': { system("cls"); cout << "Enter the number to be mirrored" << endl << "nt"; // validate positive and whole if(!(cin >> num)) { cout << "thats not a int, try again."; thechoice = '#'; // {// flush cin buffer somehow...} system("pause"); break; } NumReverse(num); break; } case 'G': case 'g': { system("cls"); unsigned int num1,num2; cout << "Enter two intigers to find their GCD"<<endl; cin >> num1 >>num2; cout << GCD(num1,num2) << endl; system("pause"); break; } case 'Q': case 'q': { thechoice = '!'; break; } default: { cout << "Enter S, M, G, or Q followed by the enter key.nn"; cin.clear(); system("pause"); break; } }} cout << "ntend Of progn"; system("pause"); return 0; } ///////////////////////////////////////////////////////////////////////////////////////// FXN METHODS bool posWhole(int checkme) { return true; } void NumReverse(int reverseme) { if(reverseme<=0) { cout << "Bad Input"; system("pause"); return; } else{ //base case: 1 digit. if((reverseme/10) < 1) { cout << reverseme << "...is the mirrored number." << endl; system("pause"); system("cls"); return ; } //recursive step: cout << (reverseme%10); NumReverse(reverseme/10); } } void setOrder(int num1, int num2) { if(num1>num2) { int tempswap = num1; num1 = num2; num2 = tempswap; } } int GCD(int num1, int num2) { setOrder(num1,num2); while(num1<=0 || num2<=0) { cout << "No negative numbers"<<endl; cin >> num1 >> num2; } if(num1%num2 ==0) { return num2; } return (GCD(num2,(num1%num2))); } void gridSearch() { int findme=0; ifstream in; in.open("values.txt"); if(!in) //crap on invalid input { cout << "Wrong file, Ya Hippyn"; system("pause"); return; } int i,tmp,gridctr = 0; cout<<"n "; for(i=0;i<TABLE_COLS;i++)//print top row { cout <<setw(4)<< i; } cout << endl; while(!in.eof()) // File reading begins here { for(i=0;i<TABLE_ROWS;i++)// FOR EACH ROW { for(int j=0;j<TABLE_COLS;j++) //FILL ITS COL VALUES { //char tmp; if(j==0) //insert a left margin cout<<endl<<setw(3)<<i; in >> tmp; cout << setw(4)<< tmp; //grid[TABLE_ROWS][TABLE_COLS] = tmp; //write tmp to array //the above doest nothing at all????!!! Driving me friggin crazy!!!! //I cant even attempt to search the grid without properly loading it. // im punking out and using a regular array grid[gridctr] = tmp; gridctr++; } } } // failed recursion attempt //int low = 0; //int high = 599; //cout << "nEnter a number to Locate"; //cin >> findme; //if(findme == grid[(high-low)/2]) //{ // cout << "nnumber found at row:" << (i%20) << " col:" << (grid[(high-low)/2]%20); //} //else{ // if(findme < grid[(high-low)/2]) // { // return; //cant figure out how to modularize and still be able to lookup the grid // } //} //iterative punkout... gotta turn in something that runs.. while(findme>=0) { cout << "nEnter a number to Locate. Any negative entry will exit"; cin >> findme; i=0; while(grid[i] <= findme) { if(grid[i]==findme) cout << "nnumber found at row:" << (i/20) << " col:" << (i%20); i++; } }findme=0; }