Question
Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated
Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated student2.txt data file. A sample file is shown below:
3
1827356 3.75
9271837 2.93
3829174 3.14
Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. Youll need to define the array pointers in main and pass them into your read data function by reference so that the addresses stored into them go back to main and can be used by the rest of the program.
Demonstrate your pointer prowess by converting your array notation to pointer notation in the binSearch function for this revised program.
Pass by pointer reference, Read Size out of data files, do NEW to allocate arrays
- Pass in pointers by reference
- Either return the size or use a reference parameter (with &) for size
- In function:
- Create and connect the file stream, checking for error
- Read the size (1st number in the file)
- Allocate the arrays, using new
- Read the data as before
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include
using namespace std;
void readData(long int studentID[],double gpa[],int &size); void selectionSort(long int studentID[],double gpa[],int size); int binSearch(long int studentID[],double gpa[],int size,long int searchNumber); long int readStudentID(); void printStats(long int studentID,double gpa);
int main() { long int* sptr; int i=0,size; long int studentID[25]; double* gptr; double gpa[25] ;
//Read all 25 Student ID and corresponding GPA into parallel arrays from a text file readData(studentID,gpa,size);
//Use a selection sort to sort the arrays by Student ID. selectionSort(studentID,gpa,size);
//Read in a Student ID from user, loop back to allow user to continue long int searchStudentID;
while(true) { searchStudentID = readStudentID(); //Terminate loop when -99 entered if(searchStudentID=="-99") break; //Binary search to locate student id once they have been sorted. int index=binSearch(studentID,gpa,size,searchStudentID); if(index==-1){ cout<<" Student ID Not Found! "; } else{ printStats(studentID[index],gpa[index]); } } cout<<"----------------------- "; cout<<"Thank you for searching "; return 0; }
void readData(long int studentID[],double gpa[],int &size) { int i=0; cout<<"Reading data from student.txt file "; ifstream infile; infile.open("student.txt"); while (!infile.eof()) { infile >> studentID[i] >> gpa[i]; i++; } size = i-1;
}
void selectionSort(long int studentID[],double gpa[],int size) { int i,j,min; long int temp; double tempgpa; for(i=0;i tempgpa=gpa[i]; gpa[i]=gpa[min]; gpa[min]=tempgpa; } } int binSearch(long int studentID[],double gpa[],int size,long int searchNumber) { int first, last, middle; int location = -1; first = 0; last = size-1; while (location == -1 && first <= last) { middle = (first + last) / 2; if (searchNumber > studentID[middle]) first = middle + 1; else if (searchNumber < studentID[middle]) last = middle - 1; else location = middle; } return location; } long int readStudentID() { long int searchStudentID; cout<<"Enter Student ID to search (-99 to exit) "; cin>>searchStudentID; return searchStudentID; } void printStats(long int studentID,double gpa) { cout<<"Student ID \tGPA "; cout<<"---------------------------- "; cout<
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started