Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok so this program i have been working on and i finally get it free of errors an

ID: 3771472 • Letter: O

Question

Ok so this program i have been working on and i finally get it free of errors and when i go to run it in visual studio i get a runtime problem and the program crashes, when i run through the debugger it points to last line in the headercpp.cpp and another in the main.cpp and i also ran it through xcode on my mac and it builds successfully but i get nothing from the program just a blank window.

this is what visual studio tells me... 0x56AB4FB2(vcruntime140d.dll) in lab8.exe0xC0000005:Access violation writing location 0x009C8C9Dl if there is a handler for this exception, the program may be safely continued and the debugger points to the last line in the headercpp.cpp

This is what the guidlines for the program are. my program is pasted at the bottom

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

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.

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)

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

headercpp.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;

  

}

main.cpp

#include "Header.h"

#include<iostream>

#include <fstream>

#include <string>

#include <time.h>

#include<ctime>

using namespace std;

void sortNames(FemaleNames[],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(FemaleNames fnames[],int left, int right)

{

int i=left,j=right;

FemaleNames temp;

string pivot=fnames[(left+right)/2].getName();

  

while (i<=j) {

while (fnames[i].getName()<pivot)

i++;

while (fnames[j].getName()>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;

  

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.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

headercpp.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;

  

}

main.cpp

#include "Header.h"

#include<iostream>

#include <fstream>

#include <string>

#include <time.h>

#include<ctime>

using namespace std;

void sortNames(FemaleNames[],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(FemaleNames fnames[],int left, int right)

{

int i=left,j=right;

FemaleNames temp;

string pivot=fnames[(left+right)/2].getName();

  

while (i<=j) {

while (fnames[i].getName()<pivot)

i++;

while (fnames[j].getName()>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;

  

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote