//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
// :mrgreen: commented out on previous line. Please help finsh with recursion. I'm lost.

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<conio.h>
 
usingnamespace std;

char ProgramName[] = "Project 1.cpp"; // Used to hold the program name
int Option, *oPtr = &Option;
constint ARRAY_SIZE = 10;
char fname[ARRAY_SIZE][25]={"none","none","none","none",
"none","none","none","none","none","none"};
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]);
//:mrgreen: 
void Search(char [ARRAY_SIZE][25]);

int main (void)
{
StartUp();
system("cls");
Menu(fname);
return 0;
} // End of function main
 
 
void StartUp(void){
} // End of function StartUp
 
void call(void (*opt)(char arr[ARRAY_SIZE][25]))
{
(*opt)(fname);
}
 
 
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"
//:mrgreen: 
<< "\n7. Search"
<< "\n\n\tChoose and Option: ";
cin >> Option;
//:mrgreen: //6-7
if(Option < 1 || Option > 7)
{
cout << "\nPlease re-enter your selection." << endl;
 
_getch();
}
//:mrgreen: //6-7
} while(Option <1 || Option > 7);
call(f[Option-1]);
}
 
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);
}
}
}
 
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;
}
}
 
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);
}
void Print(char arrNames[ARRAY_SIZE][25])
{
for(int i = 0; i < 10; i++)
{
cout << "\tarrName[" << i << "] = " << arrNames[i] << endl;
}
}
 
 
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[i] << endl;
}
cout << "\n\n\t";
system("pause");
system("cls");
Menu(fname);
}
 
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.
//*********************
//:mrgreen: 
void Search(char arr[10][25])
{
 
cout << "Which element do you want to search for:(0-9) " << endl ;
 
cin >> "Enter choice" >> endl;
return;
}
 
void WrapUp(void)
{
cout << "\n\tProgram "
<< ProgramName
<< " ended successfully.\n\t";
} // End of function WrapUp.
/*

O

Member Avatar for iamthwee

1. Learn to use code tags properly

You should say what the code is about, what is it supposed to do? What is there to sort and where to search in? I'm not gonna spend much time looking through code finding what the real problem actually is. You should describe the problem better.

And you really should indent it. I personally think it's just so much easier to understand for outsiders of your code.

Here's a better explantion. Thanks!
This part"A" is already finished:

A). Create a menu with the following options, (1) Add, (2) Update, (3) Delete, (4) Sort, (5) Print, and (6) Quit.
Create an array of 10 strings up to 25 characters long and initialize each one to "none". Each item will represent a first name.
Create an array of function pointers to each of the above menu options. When the user selects an option call the appropriate function using this array.
Pass the string array to each of the functions as a parameter.
Use a bubble sort to sort the items.
Display the array when the user selects a menu item (except Sort, Print, and Quit) and then prompt the user for the nmnber (use the array subscript) of the item they want to operate on.
When deleting a name set it back to "none".
When deleting a name first confirm that the user wants to continue with the delete.

This is part"B" which has been added:
B). //I need to modify this code by changing the(existing) 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
// :?: commented out on previous line. Please help finsh with recursion. I'm lost.
//:mrgreen: denotes sort section
The sort is set up now to sort the array in alphabetacle order according the fletter of fname. The search would need to search the sorted array.

#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<conio.h>
 
usingnamespace std;
 
char ProgramName[] = "Project 1.cpp"; // Used to hold the program name
int Option, *oPtr = &Option;
constint ARRAY_SIZE = 10;
char fname[ARRAY_SIZE][25]={"none","none","none","none",
"none","none","none","none","none","none"};
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]);
//::?: : 
void Search(char [ARRAY_SIZE][25]);
 
int main (void)
{
StartUp();
system("cls");
Menu(fname);
return 0;
} // End of function main
 
 
void StartUp(void){
} // End of function StartUp
 
void call(void (*opt)(char arr[ARRAY_SIZE][25]))
{
(*opt)(fname);
}
 
 
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"
//::?: : 
<< "\n\t7. Search"
<< "\n\n\tChoose and Option: ";
cin >> Option;
//::?:  //6-7
if(Option < 1 || Option > 7)
{
cout << "\nPlease re-enter your selection." << endl;
 
_getch();
}
//::?: : //6-7
} while(Option <1 || Option > 7);
call(f[Option-1]);
}
 
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);
}
}
}
 
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;
}
}
 
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);
}
// :mrgreen: **************************
// 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);
}
void Print(char arrNames[ARRAY_SIZE][25])
{
for(int i = 0; i < 10; i++)
{
cout << "\tarrName[" << i << "] = " << arrNames[i] << endl;
}
}
 
 
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[i] << endl;
}
cout << "\n\n\t";
system("pause");
system("cls");
Menu(fname);
}
 
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.
//*********************
//::?: : 
void Search(char arr[10][25])
{
//:?: 
cout << "Which element do you want to search for:(0-9) " << endl ;
//:?: 
cin >> "Enter choice" >> endl;
return;
}
 
void WrapUp(void)
{
cout << "\n\tProgram "
<< ProgramName
<< " ended successfully.\n\t";
} // End of function WrapUp.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.