Must use VECTORS! Read information a text file called student.txt - Create a fil
ID: 3598413 • Letter: M
Question
Must use VECTORS!
Read information a text file called student.txt
- Create a file of at least 20 student records sorted by student ID. Read student records into a vector in C++ or Java program. (For Vectorrefer to Chapter 7.11) - Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores for each student record located somewhere in the program or from a second file. Display the 20+ student records (entire record, not just ID) and associated test Scores. Use recursive binary search to search three-five student-IDs in the vector Use recursive binary search to search a student-ID at the end of the vector. Use recursive binary search to search a student-ID which is not in the vector. For each search, print the ID to be searched and the searched result with the found entire record. For each search, also count the number of times the recursive binary search function has gone thru the recursion cycle. And print the count for each search. Your program output must show proper information to be understood well by the reader/viewer.Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<sstream>
using namespace std;
struct student{
int id;
string name;
double gpa;
string address;
double score[10];
};
void bin_search(student data[],int i,int j, int id){
int mid;
int count = 0;
int index = -1;
while (i < j){
if (data[i].id == id){
index = i;
break;
}
if (data[j].id == id){
index = j;
break;
}
mid = (i+j)/2;
if (data[mid].id == id){
index = mid;
break;
}
if (data[mid].id > id){
j = mid-1;
}
if (data[mid].id < id){
i = mid+1;
}
count++;
}
if (index == -1)
cout << "Element not found ";
else{
cout << "Element found at " << index << endl;
cout << "Recursion count : " << count << endl;
}
}
int main(){
student data[20];
srand(time(NULL));
for (int i = 0; i<20; i++){
ostringstream str1;
str1 << i;
data[i].id = i;
data[i].name = "name" + str1.str();
data[i].address = "address" + str1.str();
double sum = 0;
for (int j = 0; j<10; j++){
data[i].score[j] = rand()%100;
sum = sum + data[i].score[j];
}
data[i].gpa = sum/10;
str1.clear();
}
for (int i = 0; i<20; i++){
cout << "ID:" << data[i].id << endl;
cout << "Name:" << data[i].name << endl;
cout << "Address:" << data[i].address << endl;
cout << "GPA:" << data[i].gpa << endl;
cout << "Score:" << endl;
for (int j = 0; j<10; j++){
cout << data[i].score[j] << " ";
}
cout << "---------------------------------" << endl;
}
bin_search(data,0,19,5) ;
bin_search(data,0,19,19) ;
bin_search(data,0,19,21) ;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.