C Programming Bubble Sort and Search Names (Using Pointers)Watch
BONUS: A bonus of 20 points if you create the program in such a way that there is no limit on the number of names to be handled.
1) Read in names to sort until the user types the “enter” key as the first character of a “C-type” string (the maximum number of names is 20, there is not a maximum length to a name).
2) After sorting and displaying the array of “C-type” strings, ask the user for a “Ctype” string to search for (again, no maximum length).
3) Do a binary search to find if the “C-type” string is in the array
4) If the “C-type” string is in the array, tell the user the index of the “C-type” string.
5) If the “C-type” string is not in the array, tell the user that it is missing.
6) Repeat items 3 thru 5 until the user enters the “enter” key as the first character of the “C-type” string, at which time the program will complete.
Write the code for C programming.
Submit the .c and .h files in the project folder as created by Visual Studio.
SORT HEADER FILE:
typedef int BOOL;
#define FALSE 0
#define TRUE 1
/* this is known as a "Binary" Search */
int Search (const char Letters , char C, size_t NumChars)
First = 0;
Last = NumChars - 1;
while (First <= Last)
Middle = (First + Last) / 2;
if (C == Letters [Middle])
if (C < Letters [Middle])
Last = Middle - 1;
First = Middle + 1;
/* this is known as a "Sinking" Sort or a "Bubble" Sort */
void Sort (char Letters , size_t NumChars)
Sorted = TRUE;
for (i = 0; i < NumChars; i++)
if (Letters[i] > Letters[i + 1])
Temp = Letters [i];
Letters [i] = Letters [i + 1];
Letters [i + 1] = Temp;
Sorted = FALSE;
} while (!Sorted);
int main ()
char * Names ;
char * Temp;
/* the bonus will do the array of Names as dynamic using pointers as */
char ** MoreNames;
MoreNames = (char **) malloc (20 * sizeof (char *));
use example from last week to read in names and put the addresses
into slots in the array
if (strcmp (Names [i], Names [i + 1]) > 0) /* means first name is greater than second */
Temp = Names [i];
Names [i] = Names [i + 1];
Names [i + 1] = Temp;
Sorted = FALSE;
/* remember to clean up the dynamic memory by using free */
I'm not sure where to start and how to use pointers for the bonus, to get the program to work. Any help is appreciated!
Otherwise, to continue using an array without a limit on the number of names, you could use the realloc() function to get another larger array and keep your existing data -- https://stackoverflow.com/questions/...06798#21006798
(It's impossible to resize an array, so what realloc actually does under-the-hood is to malloc() a new larger array, then do a simple memcpy() to preserve the data, before using free() with the old one - which should work fine for you in this case.