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

Create a file of at least 10 student records sorted by student ID. Read student

ID: 3598811 • Letter: C

Question

Create a file of at least 10 student records sorted by student ID.

Read student records into a vector in C++

Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores for each student

Display the 20+ student records (entire record, not just ID) and the associated test Scores.

Use recursive binary search to search for 3 random student-IDs in the vector.

"Searching for record with student id: "

"Searching for record with student id: "

"Searching for record with student id: "

Use recursive binary search to search a student-ID at the end of the vector.

"Searching for record at the end of vector, with student id:"

Use recursive binary search to search a student-ID which is not in the vector.

"Searching for record with student id"

Explanation / Answer

//main file,, just modified one random value to search with fixed value to test the found condition,, u can remove that later,, find that change with keyword CheggEA

//main.cpp

#include<iostream>

#include<vector>

#include<string>

#include<fstream>

#include<time.h>

using namespace std;

typedef struct rec

{

int StudentID;

string name;

string student_address;

int *test_scores;

}record;

bool binary_search(const vector<record>& sorted_vec, int id);

void sort(vector<record>& vec);

int main()

{

//declare vector of record

record tmp;

vector<record> studentRec;

ifstream in;

//open input file and read into vector

in.open("StudentRec.txt");

if (!in)

{

cout << "Not able to open input file" << endl;

return -1;

}

while (!in.eof())

{

in >> tmp.StudentID;

in.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

getline(in, tmp.name);

getline(in, tmp.student_address);

//in.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

//before reading tstscore allocate memory

tmp.test_scores = new int[10];

int n;

for (int i = 0; i < 10; i++)

{

in >> n;

tmp.test_scores[i] = n;

}

//now push this tmp to vector

studentRec.push_back(tmp);

}

//sort vector befor using binary search

sort(studentRec);

//now generate random number betwen 1 to 100 and search vctor using binary search

int searchId[3];

/* initialize random seed: */

srand(time(NULL));

for (int i = 0; i < 3; i++)

{

searchId[i] = rand() % 100 + 1;

}

//just assigned 49 to check found conditiona, CheggEA

searchId[2] = 49;

//now binary search

for (int i = 0; i < 3; i++)

{

cout << "Searching for record with student id : " << searchId[i] << endl;

if (binary_search(studentRec, searchId[i]) == true)

{

cout << "Id : " << searchId[i] << " found" << endl;

}

else

{

cout << "Id : " << searchId[i] << " not found" << endl;

}

}

}

bool binary_search(const vector<record>& sorted_vec, int id) {

size_t mid, left = 0;

size_t right = sorted_vec.size(); // one position passed the right end

while (left < right) {

mid = left + (right - left) / 2;

if (id > sorted_vec[mid].StudentID){

left = mid + 1; }

else if (id < sorted_vec[mid].StudentID){

right = mid;

}

else {

return true;

}

}

return false;

}

void sort(vector<record>& vec)

{

record tmp;

for (int i = 0; i < vec.size(); i++)

{

for (int j = 0; j < vec.size()-1-i; j++)

{

if (vec[j].StudentID > vec[j + 1].StudentID)

{

tmp.StudentID = vec[j].StudentID;

tmp.name = vec[j].name;

tmp.student_address = vec[j].student_address;

tmp.test_scores = vec[j].test_scores;

vec[j].name = vec[j + 1].name;

vec[j].StudentID = vec[j + 1].StudentID;

vec[j].student_address = vec[j + 1].student_address;

vec[j].test_scores = vec[j + 1].test_scores;

vec[j + 1].name = tmp.name;

vec[j + 1].test_scores = tmp.test_scores;

vec[j + 1].StudentID = tmp.StudentID;

vec[j + 1].student_address = tmp.student_address;

}

}

}

}

---------------------------------------------------------------------------------------------------------

//tested with just four files ,, u can test with more

//StudentRec.txt

49
John disouza
#121 Ghallaghar drive,Minnesota
65 70 89 90 65 77 80 90 50 90
12
Smith rawl
#89 Ghallaghar drive,Minnesota
90 70 89 90 80 77 80 90 50 90
5
Brian smith
#100 Ghallaghar drive,Minnesota
99 70 89 90 80 88 80 90 60 90
3
Ronald Bishep
#150 Ghallaghar drive,Minnesota
100 80 89 99 80 88 80 90 50 90

//output

Searching for record with student id : 74
Id : 74 not found
Searching for record with student id : 44
Id : 44 not found
Searching for record with student id : 49
Id : 49 found

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