Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ using bubble Sort, selection sort, sequential search and binary search. I am stuck finishing this program and only wrote the skeleton code as I

C++ using bubble Sort, selection sort, sequential search and binary search.

I am stuck finishing this program and only wrote the skeleton code as I have not yet learned some of the topics needed to complete it. Thank you ahead of time for your help.

The program needs to be able to do the following:

1. Implement an array of struct Person.

2. Load the data from the file person.txt. (Data placed below)

3. Use Bubble Sort to sort the records in the array based on ID.

4. Create a sequential search to locate a student record. The function should return the index of the record if the record is found. Otherwise, it returns -1.

5. Use the returned index from sequential search to display the content of the person.

6. A selection and binary search must also be added within the code.

The program does not need to be interactive to the user.

Here is what should be stored in the Person.txt file:

12546 Amy (925)456-1234

23455 Bill (925)456-2345

14388 Henry (925)674-4356

15667 Peter (925)674-4356

22222 Mini (510)456-1444

10455 John (510)456-1237

22328 Joe (925)458-1234

14328 Jim (925)674-4356

Here is the skeleton code:

#include

#include

using namespace std;

struct Person

{

int ID;

string Name;

string PhoneNo;

};

void BubbleSort(Person arr[], int SIZE)

{

// Add code here

return;

}

int SequentialSearch(Person arr[], int SIZE, int target)

{

int index =-1;

// Add code here

return index;

}

void displayPersonInfo(Person arr[], int SIZE, int ii)

{

// Add code here

return;

}

int main()

{

fstream inputFile;

string fileName = "person.txt";

const int SIZE = 29;

Person arr[SIZE];

int target = 22222;

int ID2 = 0;

string Name2, PhoneNo2 = " ";

inputFile.open(fileName.c_str(), ios::in);

if (inputFile.is_open())

{

int personCount = 0;

while(!inputFile.eof())

{

inputFile >> ID2 >> Name2 >> PhoneNo2;

arr[personCount].ID = ID2;

arr[personCount].Name = Name2;

arr[personCount].PhoneNo = PhoneNo2;

personCount++;

}

inputFile.close();

}

else

{

cout << "File cannot be opened." << endl;

}

cout << "Before Bubble Sort" << endl;

displayAll(arr, personCount);

BubbleSort(arr, personCount);

cout << "After Bubble Sort" << endl;

displayAll(arr, personCount);

int ii = SequentialSearch(arr, personCount, target); /// Return the index of the record if the target is found.

if (ii != -1)

{

cout << "Display personal information of 22222 " << endl;

displayPersonInfo(arr, personCount, ii);

}

/// Read data from input file to the array again so that the IDs are not sorted.

cout << "Before Selection Sort" << endl;

displayAll(arr, personCount);

SelectionSort(arr, personCount);

cout << "After Selection Sort" << endl;

displayAll(arr, personCount);

ii = binarySearch(arr, personCount, target);

if (ii != -1) {

cout << "Display personal information of 22222 " << endl;

displayPersonInfo(arr, personCount, ii);

}

return 0;

}

Thanks again. J

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions