#include
#include //Might end up not being used
#include
using namespace std;
void CopyString (char string1[24], char string2[24]);
int main ()
{
int doDebug = 0;
cout << \"Do Debug Output? [Y/N]:\";
char response;
cin >> response;
if (response == \'y\' || response == \'Y\')
{
doDebug = 1;
}
int nameAmount;
char inputFileName[50];
cout << \"Input the name of the file to be read (Include the extension, \";
cout << \"must be less than 48 characters): \";
cin >> inputFileName;
inputFileName[49] = \'\\0\'; //To ensure null terminator
cout << \"Input the number of names: \";
cin >> nameAmount;
char namesUnsorted[nameAmount][24];
char namesSorted[nameAmount][24];
ifstream inputFile;
inputFile.open(inputFileName);
for (int i = 0; i < nameAmount; i++)
{
inputFile.getline(namesUnsorted[i], 24, \'\ \');
namesUnsorted[i][23] = \'\\0\';
}
inputFile.close();
cout << \"Found the following entries in \" << inputFileName << \":\" << endl;
for (int i = 0; i < nameAmount; i++)
{
cout << namesUnsorted[i] << endl;
}
//Debug
if (doDebug)
{
cout << \"The following chars were found in entry 3: \" << endl;
for (int i = 0; namesUnsorted[3][i] != \'\\0\'; i++)
{
cout << \"\'\" << namesUnsorted[3][i] << \"\'\" << endl;
}
}
//Char values A-Z = [65, 90], a-z = [97, 122]
//To go from lower to upper, subtract 32
//To keep track of which position is next in the sorted array
int onName = 0;
//Begin main sorting algorithm
//Go through every letter
for (char thisLetter = \'A\'; thisLetter <= \'Z\'; thisLetter++)
{
//Go through every name in the unsorted array
for (int i = 0; i < nameAmount; i++)
{
//If a match is found
if (namesUnsorted[i][0] == thisLetter)
{
//If there is already a name entered before and it starts with
//the same letter as the match found
if ((onName > 0) && (namesSorted[(onName - 1)][0] == thisLetter))
{
cout << \"Comparing \" << namesSorted[(onName - 1)] << namesUnsorted[i] <<
endl;
//Go through every letter to find which is lower
for (int checkChar = 0; checkChar < 24; checkChar++)
{
char sortedChar, unsortedChar;
sortedChar = namesSorted[onName - 1][checkChar];
unsortedChar = namesUnsorted[i][checkChar];
//If the char is a lowercase letter, convert to
//uppercase
if (sortedChar > \'Z\')
{
sortedChar -= 32;
}
if (unsortedChar > \'Z\')
{
unsortedChar -= 32;
}
//The sorted name ended sooner
if (sortedChar == \'\\0\' && unsortedChar != \'\\0\')
{
//namesSorted[onName - 1][checkChar] = \'\\0\';
//Keep the sorted name where it is and place the
//unsorted name in the entry below
CopyString(namesUnsorted[i], namesSorted[onName]);
break;
} else if (unsortedChar == \'\\0\' && sortedChar != \'\\0\')
{
//The unsorted name ended sooner
//namesUnsorted[i][checkChar] = \'\\0\';
//Move the sorted name down one and place the
//unsorted name to where the sorted one was
CopyString(namesSorted[(onName - 1)], namesSorted[onName]);
CopyString(namesUnsorted[i], namesSorted[(onName - 1)]);
break;
}
//Debug
if (doDebug)
{
cout << \"S: \" << sortedChar << \" | U: \" << unsortedCh.
#include iostream #include iomanip Might end up not being.pdf
1. #include
#include //Might end up not being used
#include
using namespace std;
void CopyString (char string1[24], char string2[24]);
int main ()
{
int doDebug = 0;
cout << "Do Debug Output? [Y/N]:";
char response;
cin >> response;
if (response == 'y' || response == 'Y')
{
doDebug = 1;
}
int nameAmount;
char inputFileName[50];
cout << "Input the name of the file to be read (Include the extension, ";
cout << "must be less than 48 characters): ";
cin >> inputFileName;
inputFileName[49] = '0'; //To ensure null terminator
cout << "Input the number of names: ";
2. cin >> nameAmount;
char namesUnsorted[nameAmount][24];
char namesSorted[nameAmount][24];
ifstream inputFile;
inputFile.open(inputFileName);
for (int i = 0; i < nameAmount; i++)
{
inputFile.getline(namesUnsorted[i], 24, ' ');
namesUnsorted[i][23] = '0';
}
inputFile.close();
cout << "Found the following entries in " << inputFileName << ":" << endl;
for (int i = 0; i < nameAmount; i++)
{
cout << namesUnsorted[i] << endl;
}
//Debug
if (doDebug)
{
cout << "The following chars were found in entry 3: " << endl;
for (int i = 0; namesUnsorted[3][i] != '0'; i++)
{
3. cout << "'" << namesUnsorted[3][i] << "'" << endl;
}
}
//Char values A-Z = [65, 90], a-z = [97, 122]
//To go from lower to upper, subtract 32
//To keep track of which position is next in the sorted array
int onName = 0;
//Begin main sorting algorithm
//Go through every letter
for (char thisLetter = 'A'; thisLetter <= 'Z'; thisLetter++)
{
//Go through every name in the unsorted array
for (int i = 0; i < nameAmount; i++)
{
//If a match is found
if (namesUnsorted[i][0] == thisLetter)
{
//If there is already a name entered before and it starts with
//the same letter as the match found
if ((onName > 0) && (namesSorted[(onName - 1)][0] == thisLetter))
{
cout << "Comparing " << namesSorted[(onName - 1)] << namesUnsorted[i] <<
endl;
//Go through every letter to find which is lower
for (int checkChar = 0; checkChar < 24; checkChar++)
{
char sortedChar, unsortedChar;
sortedChar = namesSorted[onName - 1][checkChar];
unsortedChar = namesUnsorted[i][checkChar];
//If the char is a lowercase letter, convert to
4. //uppercase
if (sortedChar > 'Z')
{
sortedChar -= 32;
}
if (unsortedChar > 'Z')
{
unsortedChar -= 32;
}
//The sorted name ended sooner
if (sortedChar == '0' && unsortedChar != '0')
{
//namesSorted[onName - 1][checkChar] = '0';
//Keep the sorted name where it is and place the
//unsorted name in the entry below
CopyString(namesUnsorted[i], namesSorted[onName]);
break;
} else if (unsortedChar == '0' && sortedChar != '0')
{
//The unsorted name ended sooner
//namesUnsorted[i][checkChar] = '0';
//Move the sorted name down one and place the
//unsorted name to where the sorted one was
CopyString(namesSorted[(onName - 1)], namesSorted[onName]);
CopyString(namesUnsorted[i], namesSorted[(onName - 1)]);
break;
5. }
//Debug
if (doDebug)
{
cout << "S: " << sortedChar << " | U: " << unsortedChar << endl;
}
if (sortedChar > unsortedChar) //Sorted is higher
{
//Move the sorted name down one and place the
//unsorted name to where the sorted one was
CopyString(namesSorted[(onName - 1)], namesSorted[onName]);
CopyString(namesUnsorted[i], namesSorted[(onName - 1)]);
break;
} else if (sortedChar < unsortedChar)
{
//Keep the sorted name where it is and place the
//unsorted name in the entry below
CopyString(namesUnsorted[i], namesSorted[onName]);
}
//If it's checking the final char and no match has
//been found, they must be the same
if (checkChar == 23)
{
CopyString(namesUnsorted[i], namesSorted[onName]);
}
}
} else
{
6. CopyString(namesUnsorted[i], namesSorted[onName]);
}
onName++;
}
}
}
cout << endl << "Sorted names: " << endl;
for (int i = 0; i < nameAmount; i++)
{
cout << namesSorted[i] << endl;
}
char outputFileName[50];
cout << "Enter output file name (including extension): ";
cin >> outputFileName;
outputFileName[49] = '0';
ofstream outputFile;
outputFile.open(outputFileName);
for (int i = 0; i < nameAmount; i++)
{
outputFile << namesSorted[i] << " ";
}
7. outputFile.close();
cout << "Sorted list successfully stored in " << outputFileName << endl;
return 0;
}
void CopyString (char string1[24], char string2[24])
{
for (int i = 0; i < 24; i++)
{
string2[i] = string1[i];
}
string2[23] = '0';
return;
}
input.txt
Milda
Dan
Dale
Letha
Kurt
Daniel
Kandy
Tammy
Malisa
Corey
output.txt
Corey
Dale
Dan
8. Daniel
Kandy
Kurt
Letha
Malisa
Milda
Tammy
Solution
#include
#include //Might end up not being used
#include
using namespace std;
void CopyString (char string1[24], char string2[24]);
int main ()
{
int doDebug = 0;
cout << "Do Debug Output? [Y/N]:";
char response;
cin >> response;
if (response == 'y' || response == 'Y')
{
doDebug = 1;
}
int nameAmount;
char inputFileName[50];
9. cout << "Input the name of the file to be read (Include the extension, ";
cout << "must be less than 48 characters): ";
cin >> inputFileName;
inputFileName[49] = '0'; //To ensure null terminator
cout << "Input the number of names: ";
cin >> nameAmount;
char namesUnsorted[nameAmount][24];
char namesSorted[nameAmount][24];
ifstream inputFile;
inputFile.open(inputFileName);
for (int i = 0; i < nameAmount; i++)
{
inputFile.getline(namesUnsorted[i], 24, ' ');
namesUnsorted[i][23] = '0';
}
inputFile.close();
cout << "Found the following entries in " << inputFileName << ":" << endl;
for (int i = 0; i < nameAmount; i++)
{
cout << namesUnsorted[i] << endl;
10. }
//Debug
if (doDebug)
{
cout << "The following chars were found in entry 3: " << endl;
for (int i = 0; namesUnsorted[3][i] != '0'; i++)
{
cout << "'" << namesUnsorted[3][i] << "'" << endl;
}
}
//Char values A-Z = [65, 90], a-z = [97, 122]
//To go from lower to upper, subtract 32
//To keep track of which position is next in the sorted array
int onName = 0;
//Begin main sorting algorithm
//Go through every letter
for (char thisLetter = 'A'; thisLetter <= 'Z'; thisLetter++)
{
//Go through every name in the unsorted array
for (int i = 0; i < nameAmount; i++)
{
//If a match is found
if (namesUnsorted[i][0] == thisLetter)
{
//If there is already a name entered before and it starts with
//the same letter as the match found
if ((onName > 0) && (namesSorted[(onName - 1)][0] == thisLetter))
{
cout << "Comparing " << namesSorted[(onName - 1)] << namesUnsorted[i] <<
endl;
11. //Go through every letter to find which is lower
for (int checkChar = 0; checkChar < 24; checkChar++)
{
char sortedChar, unsortedChar;
sortedChar = namesSorted[onName - 1][checkChar];
unsortedChar = namesUnsorted[i][checkChar];
//If the char is a lowercase letter, convert to
//uppercase
if (sortedChar > 'Z')
{
sortedChar -= 32;
}
if (unsortedChar > 'Z')
{
unsortedChar -= 32;
}
//The sorted name ended sooner
if (sortedChar == '0' && unsortedChar != '0')
{
//namesSorted[onName - 1][checkChar] = '0';
//Keep the sorted name where it is and place the
//unsorted name in the entry below
CopyString(namesUnsorted[i], namesSorted[onName]);
break;
} else if (unsortedChar == '0' && sortedChar != '0')
{
//The unsorted name ended sooner
12. //namesUnsorted[i][checkChar] = '0';
//Move the sorted name down one and place the
//unsorted name to where the sorted one was
CopyString(namesSorted[(onName - 1)], namesSorted[onName]);
CopyString(namesUnsorted[i], namesSorted[(onName - 1)]);
break;
}
//Debug
if (doDebug)
{
cout << "S: " << sortedChar << " | U: " << unsortedChar << endl;
}
if (sortedChar > unsortedChar) //Sorted is higher
{
//Move the sorted name down one and place the
//unsorted name to where the sorted one was
CopyString(namesSorted[(onName - 1)], namesSorted[onName]);
CopyString(namesUnsorted[i], namesSorted[(onName - 1)]);
break;
} else if (sortedChar < unsortedChar)
{
//Keep the sorted name where it is and place the
//unsorted name in the entry below
CopyString(namesUnsorted[i], namesSorted[onName]);
}
//If it's checking the final char and no match has
//been found, they must be the same