Hello I need some guidance i am working on a program for class but i am unable t
ID: 3771164 • Letter: H
Question
Hello I need some guidance i am working on a program for class but i am unable to get it finished can you please help me out.
i'm getting an error when i try to call my quicksort sortNames function, and i dont know if i am missing anything else
THIS IS THE ASSIGNMENT
Name Sorting and Searching
COSC 1437 – Lab #08
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
1. Each name and popularity number must be stored in a separate object.
2. 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.
3. It must give an appropriate message if the name entered is not on the list.
4. Both a linear and a binary search are required, for each name entered and the time it takes for each
one reported.
5. Either the sort or the binary search must use a recursive method (Hint: a recursive binary search is
easier than a recursive sort).
Suggestions
1. Chapters 8 and 19 have example C++ code for sorting and searching arrays.
2. 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;
3. 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)
IT WILL NOT LET ME ATTACH MORE THAN ONE FILE SO HERE IS WHAT I HAVE SO FAR
HEADER.CPP
#include <stdio.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
MAIN.CPP
#include "Header.h"
#include<iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <time.h>
#include<ctime>
using namespace std;
void sortNames(int[],int,int);
int linearSearch (FemaleNames *fnames, string searchKey, int count);
int binarySearch(FemaleNames *fnames, string searchKey, int count);
string toUpper(string name);
const int SIZE=1000;
int main()
{
FemaleNames fnames[SIZE];
string name;
int value;
int count=0;
string searchKey;
ifstream infile("CommonFemaleNames.txt");
if(infile)
{
infile>>value;
infile>>name;
fnames[count].setName(name);
fnames[count].setRank(value);
while(!infile.eof())
{
count++;
infile>>value;
infile>>name;
fnames[count].setName(name);
fnames[count].setRank(value);
}
}
infile.close();
sortNames(fnames[], 0, SIZE-1);
for(int i=0;i<=count;i++)
cout<<fnames[i].getName()<<" "<<fnames[i].getRank()<<endl;
clock_t startClock,finishClock;
double timeCount;
cout << "Enter name to find: " << endl;
cin >> searchKey;
searchKey=toUpper(searchKey);
cout << searchKey<<endl;
startClock = clock()/1000;
int pos1=linearSearch(fnames,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos1!=-1)
cout << "The name is ranked:# "<<pos1+1 << endl;
else
cout << "Name not found" << endl;
cout<< "The time taken to find the name using linear search is: " << timeCount << " nanoseconds." << endl;
startClock = clock()/1000;
int pos2=binarySearch(fnames,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos2!=-1)
cout << "The name is ranked:# "<<pos2+1 << endl;
else
cout << "Name not found" << endl;
cout<< "The time taken to find the name using binary search is: " << timeCount << " nanoseconds." << 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(int fnames[],int left, int right)
{
int i=left,j=right;
int temp;
int pivot=fnames[(left+right)/2];
while (i<=j) {
while (fnames[i]<pivot)
i++;
while (fnames[j]>pivot)
j--;
if (i<=j) {
temp=fnames[i];
fnames[i]=fnames[j];
fnames[j]=temp;
i++;
j--;
}
};
if (left<j)
sortNames(fnames,left,j);
if (i<right)
sortNames(fnames,i,right);
}
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;
}
;>
Explanation / Answer
HEADER.CPP
#include <stdio.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
MAIN.CPP
#include "Header.h"
#include<iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <time.h>
#include<ctime>
using namespace std;
void sortNames(int[],int,int);
int linearSearch (FemaleNames *fnames, string searchKey, int count);
int binarySearch(FemaleNames *fnames, string searchKey, int count);
string toUpper(string name);
const int SIZE=1000;
int main()
{
FemaleNames fnames[SIZE];
string name;
int value;
int count=0;
string searchKey;
ifstream infile("CommonFemaleNames.txt");
if(infile)
{
infile>>value;
infile>>name;
fnames[count].setName(name);
fnames[count].setRank(value);
while(!infile.eof())
{
count++;
infile>>value;
infile>>name;
fnames[count].setName(name);
fnames[count].setRank(value);
}
}
infile.close();
sortNames(fnames[], 0, SIZE-1);
for(int i=0;i<=count;i++)
cout<<fnames[i].getName()<<" "<<fnames[i].getRank()<<endl;
clock_t startClock,finishClock;
double timeCount;
cout << "Enter name to find: " << endl;
cin >> searchKey;
searchKey=toUpper(searchKey);
cout << searchKey<<endl;
startClock = clock()/1000;
int pos1=linearSearch(fnames,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos1!=-1)
cout << "The name is ranked:# "<<pos1+1 << endl;
else
cout << "Name not found" << endl;
cout<< "The time taken to find the name using linear search is: " << timeCount << " nanoseconds." << endl;
startClock = clock()/1000;
int pos2=binarySearch(fnames,searchKey,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos2!=-1)
cout << "The name is ranked:# "<<pos2+1 << endl;
else
cout << "Name not found" << endl;
cout<< "The time taken to find the name using binary search is: " << timeCount << " nanoseconds." << 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(int fnames[],int left, int right)
{
int i=left,j=right;
int temp;
int pivot=fnames[(left+right)/2];
while (i<=j) {
while (fnames[i]<pivot)
i++;
while (fnames[j]>pivot)
j--;
if (i<=j) {
temp=fnames[i];
fnames[i]=fnames[j];
fnames[j]=temp;
i++;
j--;
}
};
if (left<j)
sortNames(fnames,left,j);
if (i<right)
sortNames(fnames,i,right);
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.