C full program please If you could give a sample input file
C++ full program please. If you could give a sample input file too that would be great!
- Create a file of at least 20 student records sorted by student ID. - Read student records into a vector in C++ (For Vector, refer to Chapter 7.11) - Student records should include Student ID, Name, GPA, Student Address, and a pointer which points to (10+) test scores somewhere in the program.
- Display the 20+ student records (entire record, not just ID) and associated test scores.
- Use recursive binary search to search three student-IDs in the vector. These student-IDs should be generated by random function() generator. (See program 3-26, and 9-15)
- 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. Your program output must show proper information to be understood well by the reader/viewer.
Solution
#include <iostream>
 #include <fstream>
 #include <sstream>
 #include <vector>
 #include <cstdlib>
 #include <ctime>
 #include <algorithm>
 using namespace std;
#define MAX_TEST_SCORES 20
struct record{
    int studentId;
    string name;
    double GPA;
    string student_address;
    double* test_scores;
    int lengthOfTestScores;
 };
void printRecord( record* r ){
    cout << \"Student Id: \" << r->studentId << endl;
    cout << \"Student Name: \" << r->name << endl;
    cout << \"GPA: \" << r->GPA << endl;
    cout << \"Student Address: \" << r->student_address << endl;
    cout << \"Test scores: \";
    for(int i = 0; i < r->lengthOfTestScores; i++){
        cout << r->test_scores[i] << \" \";
    } cout << endl << endl;
 }
int binary_search( vector<record*> records, int low, int high, int studentId ){
    if( low <= high ){
        int mid = low + ( high - low )/2;
        if( records[mid]->studentId == studentId ){
            return mid; }
        if( records[mid]->studentId < studentId ){
            return binary_search( records, mid+1, high, studentId ); }
        else{
            return binary_search( records, low, mid-1, studentId); }
    }
    return -1;
 }
bool recordComp( record* a, record* b){
    if( a->studentId < b->studentId ){ return true;}
    return false;
 }
int main(){
    //taking the input
    ifstream in(\"records.txt\");
    vector<record*> sRecords;
    string complete;
    double temp;
    while( getline(in, complete )){
        stringstream ss(complete);
        record* r = new record();
        ss >> r->studentId >> r->name >> r->GPA >> r->student_address;
        r->test_scores = new double[MAX_TEST_SCORES];
        r->lengthOfTestScores = 0;
        while( ss >> temp ){
            r->test_scores[r->lengthOfTestScores++] = temp;
        }
        sRecords.push_back(r);
    }
   //displaying records
    cout << \"Displaying all records \" << endl;
    for(int i =0; i < sRecords.size(); i++ ){
        printRecord( sRecords[i] );
    }
   //before using binary search, sort the records
    sort( sRecords.begin(), sRecords.end(), recordComp );
   
    //binary search three random student ids
    srand(time(NULL));
    for(int i = 0; i < 3; i++){
        int randId = rand()%30;
        cout << \"Searching for record with student id: \" << randId << endl;
        int index= binary_search( sRecords, 0, sRecords.size() - 1, randId );
        if( index == -1 ){
            cout << \"Not found!!!\" << endl << endl; }
        else{
            cout << \"Found at index: \" << index << endl;
            printRecord( sRecords[index]);    }
    }
   //search student id at end of vector
    cout << \"Searching for record at the end of vector, with student id: \";
    cout << sRecords[ sRecords.size() - 1]->studentId << endl;
    int index= binary_search( sRecords, 0, sRecords.size()- 1, sRecords[ sRecords.size() - 1]->studentId );
    if( index == -1 ){
        cout << \"Not found!!!\" << endl << endl; }
    else{
        cout << \"Found at index: \" << index << endl;
        printRecord( sRecords[index] );    }
    cout << \"Searching for record that is not in vector, with student id: \";
    cout << 10000 << endl;
    index= binary_search( sRecords, 0, sRecords.size()- 1, 10000 );
    if( index == -1 ){
        cout << \"Not found!!!\" << endl << endl; }
    else{
        cout << \"Found at index: \" << index << endl;
        printRecord( sRecords[index] );    }
    return 0;
 }
//Sample records.txt
 /*
 1 Name1 3 Address1 20 10 12 31 32 12 32 12 14 43
 2 Name2 3.1 Address1 20 10 12 31 32 12 32 12 14 43
 3 Name3 3.3 Address1 20 10 12 31 32 12 32 12 14 43
 4 Name4 2.2 Address1 20 10 12 31 32 12 32 12 14 43
 5 Name5 3.1 Address1 20 10 12 31 32 12 32 12 14 43
 6 Name6 3.2 Address1 20 10 12 31 32 12 32 12 14 43
 7 Name7 2.3 Address1 20 10 12 31 32 12 32 12 14 43
 8 Name8 1.3 Address1 20 10 12 31 32 12 32 12 14 43
 9 Name9 2.5 Address1 20 10 12 31 32 12 32 12 14 43
 10 Name10 3.5 Address1 20 10 12 31 32 12 32 12 14 43
 11 Name11 3.9 Address1 20 10 12 31 32 12 32 12 14 43
 12 Name12 2.5 Address1 20 10 12 31 32 12 32 12 14 43
 13 Name13 2.7 Address1 20 10 12 31 32 12 32 12 14 43
 14 Name14 2.3 Address1 20 10 12 31 32 12 32 12 14 43
 15 Name15 3.3 Address1 20 10 12 31 32 12 32 12 14 43
 16 Name16 3.5 Address1 20 10 12 31 32 12 32 12 14 43
 17 Name17 1.5 Address1 20 10 12 31 32 12 32 12 14 43
 18 Name18 2.2 Address1 20 10 12 31 32 12 32 12 14 43
 19 Name19 3.2 Address1 20 10 12 31 32 12 32 12 14 43
 20 Name20 3.4 Address1 20 10 12 31 32 12 32 12 14 43
 21 Name21 1.6 Address1 20 10 12 31 32 12 32 12 14 43
 */



