Last active
December 16, 2015 10:08
-
-
Save ereidland/5417381 to your computer and use it in GitHub Desktop.
Dynamic memory allocation, name sorting, and searching.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <iostream> | |
using namespace std; | |
#define LONG_NAME_SIZE 512 | |
void main() | |
{ | |
int numNames = 0; | |
char input[LONG_NAME_SIZE]; //Not infinite, but larger than anything the user will enter. If an "infinite" one is needed, look up std::string. | |
char ** names = NULL; //Set array of pointers to NULL. | |
while (true) | |
{ | |
cout << "Enter name: "; | |
cin.getline(input, LONG_NAME_SIZE); //Read entire line to input. | |
if (strlen(input) == 0) //If empty. | |
break; //Break out of while loop. | |
char ** newNames = new char*[numNames + 1]; //Initialize new array of char pointers. | |
for (int i = 0; i < numNames; i++) //Point all the old names to the new names. (strcpy and such is not needed because this is just using pointers.) | |
newNames[i] = names[i]; | |
if (names != NULL) //If names are set yet. | |
delete [] names; //Delete ALL the names. | |
names = newNames; //Point names to newNames. | |
//Point the newest name to a new character array of the length of the input + 1 (to include null terminator). | |
names[numNames] = new char[strlen(input) + 1]; | |
//Copy input to the name. | |
strcpy(names[numNames], input); | |
//Increment the number of names. | |
numNames++; | |
} | |
cout << "Done with names. Sorting." << endl; | |
//Sort! | |
for (int i = numNames - 1; i >= 0; i--) //Loop through all names, starting with the last one, and looping down to (and including) 0. | |
{ | |
for (int j = 0; j < i; j++) //Loop through all names < i. | |
{ | |
int len = strlen(names[j]); //Get the length of the name at j's location. | |
if (len > strlen(names[i])) //Make sure that we're not looping past the length of the name at i's location. | |
len = strlen(names[i]); | |
for (int k = 0; k < len; k++) //Loop from the first character to len. | |
{ | |
if (tolower(names[j][k]) > tolower(names[i][k])) //If the character at k within names[j] is greater than the character at k within names[i] | |
{ | |
//The tolower function gets the lowercase version of the character. | |
char * temp = names[j]; //Point temporary to names[j]. | |
names[j] = names[i]; //Set the pointer at names[j] to the address at names[i]. | |
names[i] = temp; //Set the pointer at names[i] to the address of temp. | |
//Break out of the k loop. | |
break; | |
} | |
else | |
break; //Break out of the k loop because the name at i does not belong before the name at j. | |
} | |
} | |
} | |
cout << "Sorted: " << endl; | |
//Print all the names. | |
for (int i = 0; i < numNames; i++) | |
cout << i << " " << names[i] << endl; | |
//Loop and get search string. | |
while (true) | |
{ | |
cout << "Enter a string to find: "; | |
//Read in entire input line. | |
cin.getline(input, LONG_NAME_SIZE); | |
if (strlen(input) == 0) //If empty. | |
break; //Break out of while loop. | |
bool found = false; //Set (is) found to false, so we can know if we found it after the loop. | |
for (int i = 0; i < numNames; i++) | |
{ | |
//If there is no difference between the input and names[i]. | |
if (strcmp(input, names[i]) == 0) | |
{ | |
found = true; //Set (is) found and print the index. | |
cout << "Found search string at " << i << endl; | |
break; //Break out of search. | |
} | |
} | |
if (!found) //If not found. | |
cout << "Search string not found." << endl; //Let the user know of our sorrow. | |
} | |
//Everybody clean up! | |
cout << "Done! Cleaning up..." << endl; | |
//Loop through all names. | |
for (int i = 0; i < numNames; i++) | |
delete [] names[i]; //Delete the array names[i] points to. | |
delete [] names; //Delete the array of arrays. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment