Skip to content

Instantly share code, notes, and snippets.

@abaduser
Last active April 21, 2018 11:07
Show Gist options
  • Save abaduser/5c0fd751a6895e64a94240bb5e26e408 to your computer and use it in GitHub Desktop.
Save abaduser/5c0fd751a6895e64a94240bb5e26e408 to your computer and use it in GitHub Desktop.
An example program
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
bool DEBUG = 0;
// Function prototypes
vector<string> getVector(string);
string getName(string);
void selectionSort(vector<string>&);
bool binarySearch(string, vector<string>);
void displayResult(string, string, bool);
void writeToFile(string, vector<string>);
void reverseVector(vector<string>&);
int main()
{
string boyName, girlName; // Hold's the boy's/girl's name
bool boyNameFound, girlNameFound; // Indicates whether the boy/girl name was found
// Initialize vectors with the names from the files.
vector<string> boyNames(getVector("BoyNames.txt"));
vector<string> girlNames(getVector("GirlNames.txt"));
if (DEBUG)
cout << "getvector done..." << endl;
boyName = getName("boy's"); // Get the boy/girl names as input from the user.
girlName = getName("girl's");
if (DEBUG)
cout << "getname done..." << endl;
selectionSort(boyNames); // sort the boy/girl vectors in ascending order
selectionSort(girlNames);
if (DEBUG)
cout << "selectionsort done..." << endl;
boyNameFound = binarySearch(boyName, boyNames); // search for the boy/girl names using binary search
girlNameFound = binarySearch(girlName, girlNames);
if (DEBUG)
cout << "binary done..." << endl;
displayResult("boy's", boyName, boyNameFound); // Display the results for the boy/girl names
displayResult("girl's", girlName, girlNameFound);
if (DEBUG)
cout << "displayresult done..." << endl;
writeToFile("Boynames_asc.txt", boyNames); // write the boy/girl names to their respective files in ascending order
writeToFile("Girlnames_asc.txt", girlNames);
if (DEBUG)
cout << "writetofile done..." << endl;
reverseVector(boyNames); //reverse the boy/girl vectors so they are both in descending order
reverseVector(girlNames);
if (DEBUG)
cout << "reversevector done..." << endl;
writeToFile("Boynames_desc.txt", boyNames); // write the boy/girl names to their respective files in descending order
writeToFile("Girlnames_desc.txt", girlNames);
if (DEBUG)
cout << "writetofile done..." << endl;
cout<<endl;
system("PAUSE");
return 0;
}
vector<string> getVector(string filename){
ifstream finput;
vector<string> lvector;
string current_name;
finput.open(filename);
if (finput){
while (getline(finput,current_name)){
lvector.push_back(current_name);
}
}
else{
cout << "File could not read";
}
finput.close();
return lvector;
}
// ********************************************************
// The getName function prompts the user to enter a *
// boy's/girl's name and returns the value. *
// ********************************************************
string getName(string gender){
string name;
cout << "Enter a " << gender << " name, or N if you do not wish to enter a "
<< gender << " name:";
getline(cin, name);
cout << endl;
return name;
}
void selectionSort(vector<string> &arr){
int startScan, minIndex;
string minValue;
for (startScan = 0; startScan < (arr.size() - 1); startScan++){
minIndex = startScan;
minValue = arr[startScan];
for(int index = startScan + 1; index < arr.size(); index++){
if (arr[index] < minValue){
minValue = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minValue;
}
}
bool binarySearch(string name, vector<string> arr)
{
// beginning with the algorithm
int s = 0, // represents index of start of search
e = (arr.size() - 1), // represents last index of search
m; // represents middle of search
while (s < e){
m = floor((s+e) / 2);
if (name > arr[m])
s = m + 1;
else
e = m;
}
if (name == arr[s])
return true;
else
return false;
// end of algorithm
}
void displayResult(string gender, string name, bool nameFound){
if (nameFound)
cout << name << " is one of the most popular " << gender << " names.\n";
else{
if (name == "N")
cout << "You chose not to enter a " << gender << " name.\n";
else
cout << name << " is NOT one of the most popular " << gender << " names.\n";
}
}
void writeToFile(string filename, vector<string> arr){
ofstream foutput;
foutput.open(filename);
for (string s : arr)
foutput << s << endl;
foutput.close();
}
void reverseVector(vector<string> &arr){
int b = 0,
e = (arr.size() - 1);
string temp_name;
while( b < e ){ // swap beginning and end elements until at middle.
temp_name = arr[e];
arr[e] = arr[b];
arr[b] = temp_name;
b++;
e--;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment