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";
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 ()
{
StartUp();
system("cls");
Menu(fname);
return 0;
} // End of function main
 
void StartUp(void){
} 
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: 
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();
}
 
 
//:?: :?: :?: //3LINES
void Search(char arr[10][25])
{
 
cout << "Which element do you want to search for:(0-9) " << endl ;
 
cin >> "Enter choice" >> endl;
return;
}

Recommended Answers

All 7 Replies

Please Help

... does that really mean

Please write my program for me?

I don't see a question in your post.

... does that really mean
I don't see a question in your post.

No! That's not what I'm after. I'm just looking for some help on the rec. binary search and sort. Thank You.

Ok, what help do you want? what do you not understand about the requirements ?

Google has lots of help on binary search algorithms. The idea is quite simple -- start in the middle of the array and keep dividing the array in half until you either find what you are searching for or it does not exist. Most of those google links will explain the algorithms and provide examples.

As for sorting? I have never used recursion to sort an array. But there again you can get lots of help from google. if you just take the time to study them.

At row 378 I have started search function, but I'm not sure where to go with it from here as far as setting up the recursive alg. part of the code I don't need my exact program written, a genral snippet showing the code for showing this particular step is all I'm asking for. The directions were posted just so the existing code would be more easily understood as requested.

Have you tried searching google for binary search recursive? That should turn up many results, as Ancient Dragon said. For starters, try looking here or at any of these results.

You REALLY REALLY need to format your code. Indenting is extremely important.

At row 378 I have started search function, but I'm not sure where to go with it from here as far as setting up the recursive alg. part of the code

First thing is read in what you want to search for correctly.

At this point in the program do you know exactly how many entries are in arr? If the array is not full you should pass in the number of entries.

Then call your recursive seach function (the function called seach() cannot actually be the function) passing in the array and the index to the middle entry (I'm assuming you are doing a binary seach -- what did you use before?)

Then in the function, test the entry pointed to with the name looking for and call the recursive search function again using the binary seach algorithm. Keep calling the function until
1) you find the value -- return with the index of the value
2) you have exhaused the search -- return with -1

If you need the binary seach algorithm, check here

We got it Thanks for the tips Walt.

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.