The attached file, CommonFemaleNames.txt, contains the top 1000 female names in
ID: 3770882 • Letter: T
Question
The attached file, CommonFemaleNames.txt, contains the top 1000 female names in the United States. The format is integer string where the integer is the popularity number (1 is most popular) and string is a female first name. The program is to read the list of names and create an array of objects. It then sorts the array in alphabetical order. The user enters a name, which the program searches for and then reports how common that name is. The program is to use two different search algorithms, a linear search and a binary search. It reports on how many nanoseconds it takes to find the name using each method.
Requirements
Each name and popularity number must be stored in a separate object.
The names on the list are all uppercase, but the program must find the name even if it is entered in
lower case. Do this by converting the entered name to all caps.
It must give an appropriate message if the name entered is not on the list.
Both a linear and a binary search are required, for each name entered and the time it takes for each
one reported.
Either the sort or the binary search must use a recursive method (Hint: a recursive binary search is
easier than a recursive sort).
Suggestions
Chapters 8 and 19 have example C++ code for sorting and searching arrays.
Here is an example of getting the current time in microseconds.
auto start_time = chromo::high_resolution_clock::now();
ord = namelist.findBinary(name);
auto end_time = chrono::high_resolution_clock::now();
cout << "Binary search took " <<
chrono::duration_cast<chrono::microseconds>(end_time -
start_time).count() << " microseconds" << endl;
One way to organize this is in the UML below.
Extra Credit
· Use quicksort to sort the name objects (5 points)
· Throw an exception when a name is not found. Handle it in the calling procedure (5 points)
Upload
Your source files (.h and .cpp) for this program as usual.
This is all I can get so far it keeps erroring on the file read, please help as soon as possible
This is my .h
#ifndef Header_H
#define Header_H
#include <iostream>
#include <string>
using namespace std;
class Name{
private:
int nextName();
std::string CommonName[1000];
void findRecursive(std::string name, int low, int high);
public:
Name();
void addName(std::pair <std:: string, double> CommonName);
void sorthName();
int findLinear(std::string name);
int findBinary(std::string name);
};
class CommonName : public Name{
private:
int newField;
std::string name;
public:
int getOrdinal();
std::string getName();
CommonName(int ord, std::string aName);
};
#endif
AND THIS IS MY .CPP
#include <iostream>
#include<string>
#include<fstream>
#include <sstream>
#include "Header.h"
using namespace std;
int main()
{
string CommonName[1000];
string name;
ifstream infile;
infile.open("CommonFemaleNames.txt");
if (!infile)
{
cout << "unable to open the file" << endl;
return -1;
}
for (double i = 0; infile.good(); i++){
getline(infile, CommonName[0]);
}
cout<< "Enter the name you want to find" << endl;
cin >> name;
return 0;
}
Can you please help guide me to getting this program finished I am lost at the moment.
Explanation / Answer
Note: The text file contains only 100 names. So, the time in nanoseconds is 0. Inorder to increase the time change the size of text file to 1000 by inserting 1000 names.
CommonFemaleNames.txt:
1 EMMA
2 OLIVIA
3 SOPHIA
4 AVA
5 ISABELLA
6 MIA
7 CHARLOTTE
8 AMELIA
9 MADISON
10 EMILY
11 HARPER
12 ABIGAIL
13 AVERY
14 LILY
15 ELLA
16 CHLOE
17 SOFIA
18 EVELYN
19 ARIA
20 AUBREY
21 SCARLETT
22 ELLIE
23 HANNAH
24 ZOEY
25 ADDISON
26 GRACE
27 ZOE
28 ELIZABETH
29 AUDREY
30 NORA
31 LAYLA
32 MILA
33 RILEY
34 NATALIE
35 VICTORIA
36 LILLIAN
37 LUCY
38 BROOKLYN
39 PENELOPE
40 CLAIRE
41 SAVANNAH
42 VIOLET
43 ANNA
44 STELLA
45 LEAH
46 MAYA
47 ALICE
48 SAMANTHA
49 SKYLAR
50 SARAH
51 EVA
52 HAILEY
53 SADIE
54 MARIA
55 HAZEL
56 KENNEDY
57 MADELYN
58 JULIA
59 MACKENZIE
60 SOPHIE
61 PAISLEY
62 ELEANOR
63 CAROLINE
64 AALIYAH
65 GABRIELLA
66 KYLIE
67 PEYTON
68 PIPER
69 KAYLEE
70 ARIANA
71 BELLA
72 TAYLOR
73 CLARA
74 CORA
75 ARIANNA
76 ISABELLE
77 AUTUMN
78 ANNABELLE
79 ALYSSA
80 QUINN
81 LYDIA
82 CAMILA
83 ISLA
84 EMILIA
85 ALEXA
86 ALLISON
87 AURORA
88 NAOMI
89 RUBY
90 ALEXIS
91 GIANNA
92 MADELINE
93 ELENA
94 VALENTINA
95 AUBREE
96 ELIANA
97 LILLY
98 HADLEY
99 LUNA
100 SYDNEY
Header.cpp:
#include "stdafx.h"
#include "Header.h"
//constructor
FemaleNames::FemaleNames()
{
this->rank=0;
this->name="";
}
//accessor methods of class FemaleNames
int FemaleNames::getRank()
{
return this->rank;
}
string FemaleNames::getName()
{
return this->name;
}
//mutator methods of class FemaleNames
void FemaleNames::setRank(int rank)
{
this->rank = rank;
}
void FemaleNames::setName(string name)
{
this->name = name;
}
Header.h:
#ifndef Header_H
#define Header_H
#include<iostream>
#include<string>
using namespace std;
class FemaleNames
{
private:
int rank;
string name;
public:
FemaleNames();
int getRank();
string getName();
void setRank(int rank);
void setName(string name);
};
#endif
femaleNames.cpp:
#include "Header.h"
#include<iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <time.h>
#include<ctime>
using namespace std;
void sortNames(FemaleNames *fnames, int count);
int linearSearch (FemaleNames *fnames, string searchKey, int count);
int binarySearch(FemaleNames *fnames, string searchKey, int count);
string toUpper(string name);
int main()
{
FemaleNames fn[100];
string name;
int value;
int count=0;
string searchKey;
ifstream infile("CommonFemaleNames.txt");
if(infile)
{
infile>>value;
infile>>name;
fn[count].setName(name);
fn[count].setRank(value);
while(!infile.eof())
{
count++;
infile>>value;
infile>>name;
fn[count].setName(name);
fn[count].setRank(value);
}
}
infile.close();
sortNames(fn, count);
for(int i=0;i<=count;i++)
cout<<fn[i].getName()<<" "<<fn[i].getRank()<<endl;
clock_t startClock,finishClock;
double timeCount;
cout << "Enter search key: " << endl;
cin >> searchKey;
searchKey=toUpper(searchKey);
cout << searchKey<<endl;
startClock = clock()/1000;
int pos1=linearSearch(fn,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos1!=-1)
cout << "The key is found at: "<<pos1 << endl;
else
cout << "Element not found" << endl;
cout<< "The time taken to find the key using linear search is: " << timeCount << " ns." << endl;
startClock = clock()/1000;
int pos2=binarySearch(fn,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos2!=-1)
cout << "The key is found at: "<<pos2 << endl;
else
cout << "Element not found" << endl;
cout<< "The time taken to find the key using binary search is: " << timeCount << " ns." << endl;
system("pause");
return 0;
}
string toUpper(string name)
{
for(int i=0;i<name.size();i++)
{
name[i]=toupper(name[i]);
}
return name;
}
void sortNames(FemaleNames *fnames, int count)
{
FemaleNames temp;
for(int i=0;i<count;i++)
{
for(int j=i+1;j<=count;j++)
{
if(fnames[i].getName().compare(fnames[j].getName())>0)
{
temp = fnames[i];
fnames[i]=fnames[j];
fnames[j]=temp;
}
}
}
}
int linearSearch (FemaleNames *fnames, string searchKey, int count)
{
for (int i = 0; i <=count; ++i)
{
if (fnames[i].getName().compare(searchKey) == 0)
return i;
}
return -1;
}
int binarySearch(FemaleNames *fnames, string searchKey, int count)
{
int first = 0,last = count+1,middle,position = -1;
bool found = false;
while (!found && first < last)
{
middle = (first + last) / 2;
if ((fnames[middle].getName().compare(searchKey)) == 0)
{
return middle;
}
else if ((fnames[middle].getName().compare(searchKey)) > 0)
last = middle - 1;
else
first = middle + 1;
}
return -1;
}
Sample Output:
AALIYAH 64
ABIGAIL 12
ADDISON 25
ALEXA 85
ALEXIS 90
ALICE 47
ALLISON 86
ALYSSA 79
AMELIA 8
ANNA 43
ANNABELLE 78
ARIA 19
ARIANA 70
ARIANNA 75
AUBREE 95
AUBREY 20
AUDREY 29
AURORA 87
AUTUMN 77
AVA 4
AVERY 13
BELLA 71
BROOKLYN 38
CAMILA 82
CAROLINE 63
CHARLOTTE 7
CHLOE 16
CLAIRE 40
CLARA 73
CORA 74
ELEANOR 62
ELENA 93
ELIANA 96
ELIZABETH 28
ELLA 15
ELLIE 22
EMILIA 84
EMILY 10
EMMA 1
EVA 51
EVELYN 18
GABRIELLA 65
GIANNA 91
GRACE 26
HADLEY 98
HAILEY 52
HANNAH 23
HARPER 11
HAZEL 55
ISABELLA 5
ISABELLE 76
ISLA 83
JULIA 58
KAYLEE 69
KENNEDY 56
KYLIE 66
LAYLA 31
LEAH 45
LILLIAN 36
LILLY 97
LILY 14
LUCY 37
LUNA 99
LYDIA 81
MACKENZIE 59
MADELINE 92
MADELYN 57
MADISON 9
MARIA 54
MAYA 46
MIA 6
MILA 32
NAOMI 88
NATALIE 34
NORA 30
OLIVIA 2
PAISLEY 61
PENELOPE 39
PEYTON 67
PIPER 68
QUINN 80
RILEY 33
RUBY 89
SADIE 53
SAMANTHA 48
SARAH 50
SAVANNAH 41
SCARLETT 21
SKYLAR 49
SOFIA 17
SOPHIA 3
SOPHIE 60
STELLA 44
SYDNEY 100
TAYLOR 72
VALENTINA 94
VICTORIA 35
VIOLET 42
ZOE 27
ZOEY 24
Enter search key:
ruby
RUBY
The key is found at: 82
The time taken to find the key using linear search is: 0 ns.
The key is found at: 82
The time taken to find the key using binary search is: 0 ns.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.