#13 [IMG]http://www.gidforums.com/images/buttons/reputation.gif[/IMG]
[IMG]http://www.gidforums.com/images/statusicon/post_old.gif[/IMG] 10-allican57@yahoo [IMG]http://www.gidforums.com/images/statusicon/user_online.gif[/IMG] vbmenu_register("postmenu_52405", true);
New Member
[IMG]http://www.gidforums.com/images/gid/ranks/rank1_5.gif[/IMG][IMG]http://www.gidforums.com/images/gid/ranks/rank1_5.gif[/IMG]
Join Date: Sep 2006
Posts: 21
[IMG]http://www.gidforums.com/images/reputation/reputation_pos.gif[/IMG]
Nov-2006, 15:17
I need to modify this code by changing the sort function to make it recursive and add a recursive binary search that prompts the user to enter a search item.I have added some code for the search, which I will mark with// [IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]commented out on previous line. Please help finsh with recursion. I'm lost.
Code:
// ***********************************************************************// The include section// ***********************************************************************#include <iostream>#include <iomanip>#include <cstdlib>#include <ctime>#include <fstream>#include <conio.h>// ***********************************************************************// The namespace section// ***********************************************************************using namespace std;// ***********************************************************************// The global variable declaration section// ***********************************************************************char ProgramName[] = "Project 1.cpp"; // Used to hold the program nameint Option, *oPtr = &Option;const int ARRAY_SIZE = 10;char fname[ARRAY_SIZE][25]={"none","none","none","none", "none","none","none","none","none","none"};// ***********************************************************************// The function prototype section// ***********************************************************************void StartUp(void);void WrapUp(void);void call(void (*)(void));void Menu(char [ARRAY_SIZE][25]);void Add(char [ARRAY_SIZE][25]);void Update(char [ARRAY_SIZE][25]);void Delete(char [ARRAY_SIZE][25]);void Sort(char [ARRAY_SIZE][25]);void Print(char [ARRAY_SIZE][25]);void Printarr(char [ARRAY_SIZE][25]);void Quit(char [ARRAY_SIZE][25]);//:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: void Search(char [ARRAY_SIZE][25]);// ***********************************************************************// main// This is the entry point for this program. It controls// the flow of execution.// ***********************************************************************int main (void){ StartUp(); system("cls"); Menu(fname); return 0;} // End of function main// ***********************************************************************// StartUp// Currently this is an empty module or stub. It will be used to// perform any needed initialization.// ***********************************************************************void StartUp(void){} // End of function StartUp// ***********************************************************************// The Call function. This function is used to call the data.// ***********************************************************************void call(void (*opt)(char arr[ARRAY_SIZE][25])){ (*opt)(fname);}// ***********************************************************************// Menu function// Here the user is prompted for his/her choice.// ***********************************************************************void Menu(char arr[ARRAY_SIZE][25]){ void (*f[6])(char arr[ARRAY_SIZE][25]) = {Add, Update, Delete, Sort, Printarr, Quit}; do { system("cls"); cout << "\t\t Menu" << "\n\t1. Add" << "\n\t2. Update" << "\n\t3. Delete" << "\n\t4. Sort" << "\n\t5. Print" << "\n\t6. Quit" //:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: << "\n\t7. Search" << "\n\n\tChoose and Option: "; cin >> Option;//:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: if(Option < 1 || Option > 7) { cout << "\nPlease re-enter your selection." << endl; _getch(); } } while(Option <1 || Option > 6); call(f[Option-1]);}// ***********************************************************************// The Add function// This is where new data is added to the array.// ***********************************************************************void Add(char arr[ARRAY_SIZE][25]){ int Subscript = 0; system("cls"); cout << "\t\t Adding Name Page\n\n"; Print(arr); cout << "\n\tWhich element do you want to add a name to (0 - 9): "; cin >> Subscript; if (Subscript < 0 || Subscript > 9) { cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t"; system("pause"); system("cls"); Menu(fname); } else { if(strcmp(arr[Subscript], "none")== 0) { cout << "\n\tEnter a name: "; cin >> arr[Subscript]; system("cls"); Menu(fname); } else { cout << "\n\tA name is already in this array. If you want" << "\n\tto update please go back to the Main Menu and " << "\n\tchoose the update option.\n\t"; system("pause"); system("cls"); Menu(fname); } }}// ***********************************************************************// The Update function// This is where the user is able to alter existing data// in the array.// ***********************************************************************void Update(char arr[ARRAY_SIZE][25]){ int Subscript = 0; char choice = 'b'; system("cls"); cout << "\t\t Update Name Page\n\n"; Print(fname); cout << "\n\tWhich element do you want to Update(0 - 9): "; cin >> Subscript; if(Subscript < 0 || Subscript > 9) { cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t"; system("pause"); system("cls"); Menu(fname); return; } else { if(strcmp(arr[Subscript], "none")< 0||strcmp(arr[Subscript], "none")> 0) { cout << "\tA name is already entered into this array." << "\n\tAre you sure you want to update this name? (Y/N)"; cin >> choice; if(choice == 'Y' || choice == 'y') { cout << "\n\tEnter a name: "; cin >> arr[Subscript]; system("cls"); Menu(fname); return; } else if(choice == 'N' || choice == 'n') { system("cls"); Menu(fname); return; } else { cout << "\n\tSorry Invalid Selection\n\t"; system("pause"); system("cls"); Menu(fname); return; } } else { if(strcmp(arr[Subscript], "none")== 0) { cout << "\tYou chose an array that has none listed in it" << "\n\tplease go back to the Main Menu and " << "\n\tchoose the Add option.\n\t"; system("pause"); system("cls"); Menu(fname); return; } } system("cls"); Menu(fname); return; }}// ***********************************************************************// The Delete function// Here the user is able to remove data from the array.// ***********************************************************************void Delete(char arr[ARRAY_SIZE][25]){ int Subscript = 0; char choice = 'b'; system("cls"); cout << "\t\t Delete Name Page\n\n"; Print(fname); cout << "\n\tWhich element do you want to delete(0 - 9): "; cin >> Subscript; if(Subscript < 0 || Subscript > 9) { cout << "\n\tSorry Invalid Selection Hit Enter to Return to the Main Menu\n\t"; system("pause"); system("cls"); Menu(fname); } else { if(strcmp(arr[Subscript], "none")== 0) { cout << "\n\tThere is nothing to delete in this array." << "\n\tPlease go back to the Main Menu and " << "\n\tchoose another option.\n\t"; system("pause"); system("cls"); Menu(fname); } else { cout << "\n\tAre you sure that you want to Delete this array? (Y/N)"; cin >> choice; if(choice == 'Y' || choice == 'y') { strcpy (arr[Subscript], "none") ; } else if(choice == 'N' || choice == 'n') { cout << "\n\tNo deletions will occur.\n\t"; system("pause"); system("cls"); Menu(fname); } else { cout << "\n\tSorry Invalid Selection\n\t"; system("pause"); system("cls"); Menu(fname); } } } system("cls"); Menu(fname);}// ***********************************************************************// The Sort function// Here the program puts the data from the array into// alphabetical order.// ***********************************************************************void Sort(char arr[ARRAY_SIZE][25]){ char hold[25]; for (int Pass = 1; Pass < 10; Pass++) { for (int j = 0; j < 10 - 1; j++) { if(strcmp(arr[j], arr[j + 1]) > 0) { strcpy(hold, arr[j]); strcpy(arr[j], arr[j + 1]); strcpy(arr[j + 1], hold); } } } system("cls"); Menu(fname);}// ***********************************************************************// The Print function// This is where the data from the array is displayed// to the monitor.// ***********************************************************************void Print(char arrNames[ARRAY_SIZE][25]){ for(int i = 0; i < 10; i++) { cout << "\tarrName[" << i << "] = " << arrNames << endl; }}// ***********************************************************************// The Printarr function// This function displays the array.// ***********************************************************************void Printarr(char arrNames[ARRAY_SIZE][25]){ system ("cls"); cout << "\t\t Print Name Page\n\n"; for(int i = 0; i < 10; i++) { cout << "\tarrName[" << i << "] = " << arrNames << endl; } cout << "\n\n\t"; system("pause"); system("cls"); Menu(fname);}// ***********************************************************************// The Quit function// This function is used when the user chooses to end the program.// ***********************************************************************void Quit(char arr[ARRAY_SIZE][25]){ cout << "\n\tYou have choosen to Quit the program." << "\n\tPlease have a great day." << endl; WrapUp();}//***********************************************************************//// The Search function// This function is used when the user chooses to Search for a// particular file.//************************************************************************//:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: void Search(char arr[10][25]){//:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: cout << "Which element do you want to search for:(0-9) " << endl ;//:[IMG]http://www.gidforums.com/images/gid/smilies/silly_hair.gif[/IMG]: cin >> "Enter choice" >> endl; return;}// ***********************************************************************// WrapUp// ***********************************************************************void WrapUp(void){cout << "\n\tProgram " << ProgramName << " ended successfully.\n\t";} // End of function WrapUp./*Output:Program Project 1.cpp ended successfully.*/