#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <chrono>
#include "CommonName.h"
#include "Name.h"
using namespace std;
// function prototype(s)
string toUpper(string name);
int main() {
// constant
const int SIZE = 1000;
fstream inputFile;
Name nameList;
// cout << names[0].getName();
string name;
string ranking;
int nextName = 0;
string searchKey;
inputFile.open("CommonFemaleNames.txt", ios::in);
if (!inputFile)
cout << "cannot access file";
else
cout << "file opened\n";
if (inputFile) {
inputFile >> ranking;
int ranking_num = stoi(ranking);
inputFile >> name;
CommonName* newName = new CommonName(ranking_num, name);
nameList.addName(newName);
cout << newName->getName() << " ";
cout << newName->getOrdinal() << endl;
while (inputFile >> ranking) {
ranking_num = stoi(ranking);
inputFile >> name;
CommonName* newName = new CommonName(ranking_num, name);
nameList.addName(newName);
cout << newName->getName() << " ";
cout << newName->getOrdinal() << endl;
}
}
inputFile.close();
nameList.sortNames();
cout << "Enter a female name, enter STOP to end: ";
cin >> searchKey;
searchKey = toUpper(searchKey);
auto start_time = chrono::high_resolution_clock::now();
int ord = nameList.findBinary(searchKey, 0, nextName);
auto end_time = chrono::high_resolution_clock::now();
cout << "Binary search took ";
cout << "Popular " << ord << endl;
cout << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count();
cout << " microseconds" << endl;
start_time = chrono::high_resolution_clock::now();
ord = nameList.findLinear(searchKey);
end_time = chrono::high_resolution_clock::now();
cout << "Linear search took ";
cout << "Popular " << ord << endl;
cout << chrono::duration_cast<chrono::microseconds>(end_time - start_time).count();
cout << " microseconds" << endl;
return 0;
}
// definitions
string toUpper(string name) {
for (int i = 0; i < name.size(); i++) {
name[i] = toupper(name[i]);
}
return name;
}
#ifndef CommonName_H
#define CommonName_H
#include <string>
#include <iostream>
using namespace std;
class CommonName {
private:
int ordinal;
string name;
public:
//Constructor
CommonName (int ordinal, string name);
// accessors
int getOrdinal() const;
string getName() const;
};
#endif
#include "CommonName.h"
#include <string>
using namespace std;
CommonName::CommonName (int ordinal , string name) {
this -> ordinal = ordinal;
this -> name = name;
}
int CommonName::getOrdinal() const {
return ordinal;
}
string CommonName::getName() const {
return name;
}
#ifndef Name_H
#define Name_H
#include <string>
#include <iostream>
#include "CommonName.h"
class Name {
private:
CommonName* namePair;
int nextName;
// void findRecursive(string name, int low, int high);
public:
Name();
void addName(CommonName pair);
void sortNames();
int findLinear(string name);
int findBinary(string name, int low, int high);
};
#endif
#include "Name.h"
#include "CommonName.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name() {
CommonName namePair = CommonName[1000]; // dynamic allocation of an array of 1000 objects
int nextName; // default value, it will increase as we instantiate a new namePair object
}
void Name::addName(CommonName pair) {
if (nextName < 1000) {
namePair[nextName] = pair;
nextName++;
}
}
void Name::sortNames() { /// sorting the array
CommonName* temp;
for (int i = 0; i < 1000; i++) {
for (int j = i + 1; j < 1000; j++) {
if (namePair[i]->getName().compare(namePair[j]->getName()) > 0)
{
temp = namePair[i];
namePair[i] = namePair[j];
namePair[j] = temp;
}
}
}
}
int Name::findLinear(string searchKey) {
int position = -1;
try {
for (int i = 0; i < 1000; ++i) {
if (namePair[i]->getName().compare(searchKey) == 0) {
position = i;
return namePair[i]->getOrdinal(); /// getsvalue, popularity, how common the name is
}
} if (position == -1) // nothing found position is still -1
throw searchKey;
}
catch (string searchKey) {
cout << searchKey << " is not on the list" << endl;
}
}
int Name::findBinary(string searchKey, int first, int last) { int middle;
int position = -1; if (first > last)
return position;
middle = (first + last) / 2;
if ((namePair[middle]->getName().compare(searchKey)) == 0)
return namePair[middle]->getOrdinal();
if ((namePair[middle]->getName().compare(searchKey)) < 0) {
return Name::findBinary(searchKey, middle + 1, last);
}
else
return Name::findBinary(searchKey, first, middle - 1);
}
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps
- #include #include #include "Product.h" using namespace std; int main() { vector productList; Product currProduct; int currPrice; string currName; unsigned int i; Product resultProduct; cin>> currPrice; while (currPrice > 0) { } cin>> currPrice; main.cpp cin>> currName; currProduct.SetPriceAndName (currPrice, currName); productList.push_back(currProduct); resultProduct = productList.at (0); for (i = 0; i < productList.size(); ++i) { Type the program's output Product.h 1 CSE Scanned Product.cpp if (productList.at (i).GetPrice () < resultProduct.GetPrice ()) { resultProduct = productList.at(i); } AM cout << "$" << resultProduct.GetPrice() << " " << resultProduct. GetName() << endl; return 0; Input 10 Cheese 6 Foil 7 Socks -1 Outputarrow_forwardWrite a function getNeighbors which will accept an integer array, size of the array and an index as parameters. This function will return a new array of size 2 which stores the neighbors of the value at index in the original array. If this function would result in returning garbage values the new array should be set to values {0,0} instead of values from the array.arrow_forwardMain.cpp #include <iostream>#include "Deck.h" int main() { Deck deck; deck.shuffle(); std::cout << "WAR Card Game\n\n"; std::cout << "Dealing cards...\n\n"; Card player1Card = deck.Deal(); Card player2Card = deck.Deal(); std::cout << "Player 1's card: "; player1Card.showCard(); std::cout << std::endl; std::cout << "Player 2's card: "; player2Card.showCard(); std::cout << std::endl; int player1Value = player1Card.getValue(); int player2Value = player2Card.getValue(); if (player1Value > player2Value) { std::cout << "Player 1 wins!" << std::endl; } else if (player1Value < player2Value) { std::cout << "Player 2 wins!" << std::endl; } else { std::cout << "It's a tie!" << std::endl; } return 0;} Card.h #ifndef CARD_H#define CARD_H class Card {public: Card(); Card(char r, char s); int getValue(); void showCard();…arrow_forward
- #include#include#includeusing namespace std;// outputHtmlTitle// parameters// This function...void outputHtmlTitle(ofstream & fout, string title){fout << "" << endl;fout << "" << endl;fout << "" << endl;fout << "" << endl;fout << title << endl;fout << "" << endl;}void outputHtmlFooter(ofstream & fout){fout << "" << endl;fout << "" << endl;}void outputHtmlList(ostream & fout, string first, string second, string third){fout << "" << endl;fout << "\t" << first << "" << endl;fout << "\t" << second << "" << endl;fout << "\t" << third << "" << endl;fout << "\t" << endl;}void main(int argc, char * *argv){ofstream htmlFile("myIntro.html");string title;cout << "Please enter the title: ";getline(cin, title);outputHtmlTitle(htmlFile, title);string name;string course1, course2, course3;cout…arrow_forwardC++ Programmingarrow_forwardC++ this is my code so far need help with part 9 #include <iostream> #include <string> using namespace std; int GetNumOfNonWSCharacters(const string text); int GetNumOfWords(const string text); int FindText(string text, string sample_text); string ReplaceExclamation(string text); string ShortenSpace(string text); char PrintMenu(){ char option; cout << "\nMENU"<<endl; cout << "c - Number of non-whitespace characters"<<endl; cout << "w - Number of words"<<endl; cout << "f - Find text"<<endl; cout << "r - Replace all !'s"<<endl; cout << "s - Shorten spaces"<<endl; cout << "q - Quit"<<endl; cout<<endl; cout << "Choose an option:"<<endl; cin >> option; return option; } void ExecuteMenu(string sample_text,char option){ if(option == 'c'){ int nonWSCharacters = GetNumOfNonWSCharacters(sample_text); cout << "Number of non-whitespace characters:…arrow_forward
- Create pseudocode for the following #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; char water[10][10] = {{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'},{'~','~','~','~','~','~','~','~','~','~'}}; void createBoard(int &numShip); void promptCoords(int& userX, int &userY); void shipGen(int shipX[] ,int shipY[], int &numShip); void testCoords(int &userX, int &userY, int shipX[], int shipY[], int &numShip, int& victory); void updateBoard();arrow_forwardMain.cpp #include <iostream>#include "Deck.h" int main() { Deck deck; deck.shuffle(); std::cout << "WAR Card Game\n\n"; std::cout << "Dealing cards...\n\n"; Card player1Card = deck.Deal(); Card player2Card = deck.Deal(); std::cout << "Player 1's card: "; player1Card.showCard(); std::cout << std::endl; std::cout << "Player 2's card: "; player2Card.showCard(); std::cout << std::endl; int player1Value = player1Card.getValue(); int player2Value = player2Card.getValue(); if (player1Value > player2Value) { std::cout << "Player 1 wins!" << std::endl; } else if (player1Value < player2Value) { std::cout << "Player 2 wins!" << std::endl; } else { std::cout << "It's a tie!" << std::endl; } return 0;} Card.h #ifndef CARD_H#define CARD_H class Card {public: Card(); Card(char r, char s); int getValue(); void showCard();…arrow_forwardC++ PLEASE!! // FILE: simplestring.h// CLASS PROVIDED: string (a sequence of characters)//// CONSTRUCTOR for the string class:// string(const char str[ ] = "") -- default argument is the empty string.// Precondition: str is an ordinary null-terminated string.// Postcondition: The string contains the sequence of chars from str.//// CONSTANT MEMBER FUNCTIONS for the string class:// size_t length( ) const// Postcondition: The return value is the number of characters in the// string.//// char operator [ ](size_t position) const// Precondition: position < length( ).// Postcondition: The value returned is the character at the specified// position of the string. A string's positions start from 0 at the start// of the sequence and go up to length( )-1 at the right end.//// MODIFICATION MEMBER FUNCTIONS for the string class:// void operator +=(const string& addend)// Postcondition: addend has been catenated to the end of the string.//// void operator +=(const char addend[ ])//…arrow_forward
- #include <iostream>#include <sstream>#include <string>using namespace std; int main() { string userItem; ostringstream itemsOSS; cout << "Enter items (type Exit to quit):" << endl; cin >> userItem; while (userItem != "Exit") { cin >> userItem; } cout << itemsOSS.str() << endl; return 0;}arrow_forwardplease use DEQUE #include <iostream>#include <string>#include <deque> using namespace std; const int AIRPORT_COUNT = 12;string airports[AIRPORT_COUNT] = {"DAL","ABQ","DEN","MSY","HOU","SAT","CRP","MID","OKC","OMA","MDW","TUL"}; int main(){// define stack (or queue ) herestring origin;string dest;string citypair;cout << "Loading the CONTAINER ..." << endl;// LOAD THE STACK ( or queue) HERE// Create all the possible Airport combinations that could exist from the list provided.// i.e DALABQ, DALDEN, ...., ABQDAL, ABQDEN ...// DO NOT Load SameSame - DALDAL, ABQABQ, etc .. cout << "Getting data from the CONTAINER ..." << endl;// Retrieve data from the STACK/QUEUE here } Using the attached shell program (AirportCombos.cpp), create a list of strings to process and place on a STL DEQUE container. Using the provided 3 char airport codes, create a 6 character string that is the origin & destination city pair. Create all the possible…arrow_forward#include <iostream> #include <iomanip> #include <string> using namespace std; int main() { constdouble MONSTERA_PRICE =11.50; constdouble PHILODENDRON_PRICE =13.75; constdouble HOYA_PRICE =10.99; constint MAX_POTS =20; constdouble POINTS_PER_DOLLAR =1.0/0.75; char plantType; int quantity; double totalAmount =0.0; int totalPoints =0; int availablePots =0; double plantPrice =0.0; cout << "Welcome to Tom's Plant Shop!" << endl; cout << "******************************" << endl; cout << "Enter your full name: "; string fullName; getline(cin, fullName); output: compiling code... Welcome to Tom's Plant Shop!----------------------------------------Enter your full name: ______ Available plants:M - Monstera ($ 11.5)P - Philodendron ($13.75)H - Hoya ($10.99)Q - Quit shoppingEnter the plant type (M/P/H/Q): ________ Part 1 Tom has recently started a plant-selling business, and he offers three different types of plants, namely Monstera,…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education