Question

Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

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. You’ll 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

  1. Pass in pointers by reference
  2. Either return the size or use a reference parameter (with &) for size
  3. In function:
  1. Create and connect the file stream, checking for error
  2. Read the size (1st number in the file)
  3. Allocate the arrays, using new
  4. Read the data as before

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

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<<"\nStudent ID Not Found!\n";
}
else{
printStats(studentID[index],gpa[index]);
}
}
cout<<"-----------------------\n";
cout<<"Thank you for searching\n";
return 0;
}

void readData(long int studentID[],double gpa[],int &size)
{
int i=0;
cout<<"Reading data from student.txt file\n";
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<size-1;i++)
{
min=i;
for(j=i+1;j<size;j++)
{
if(studentID[j]<studentID[min])
{
min=j;
}
}
temp=studentID[i];
studentID[i]=studentID[min];
studentID[min]=temp;

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)\n";
cin>>searchStudentID;
return searchStudentID;
}

void printStats(long int studentID,double gpa)
{
cout<<"Student ID \tGPA\n";
cout<<"----------------------------\n";
cout<<studentID<<"\t\t"<<gpa<<"\n";
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Updated code

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

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()
{
  
   int i=0,size;
   long int *studentID;
   ifstream inFile;
   double *gpa;
   inFile.open("student2.txt");
   if(!inFile)
   {
       cout<<"Error in opening the file.. Exiting!......"<<endl;
       return 0;
   }
   inFile>>size;
   studentID=new long[size];
   gpa=new double[size];
   inFile.close();
   //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)
{
   ifstream inFile;
   int i=0;
   inFile.open("student2.txt");
   if(!inFile)
   {
       cout<<"Error in opening the file.. Exiting!......"<<endl;
       return;
   }
   inFile>>size;
  
   while(!inFile.eof())
   {
       inFile>>studentID[i]>>gpa[i];
       i++;
   }
   inFile.close();
}
void selectionSort(long int studentID[],double gpa[],int size)
{
   int i,j,min;
   long int temp;
   double tempgpa;
   for(i=0;i<size-1;i++)
   {
       min=i;
      
       for(j=i+1;j<size;j++)
       {
           if(studentID[j]<studentID[min])
           {
               min=j;
           }
       }
       temp=studentID[i];
       studentID[i]=studentID[min];
       studentID[min]=temp;

       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 GPA ";
   cout<<"---------------------------- ";
   cout<<studentID<<" "<<gpa<<" ";
}

ouptut

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Update your first program to dynamically allocate the item ID and GPA arrays. The number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Please help with this coding 1.You need to dynamically allocate memory to read into a number...

    Please help with this coding 1.You need to dynamically allocate memory to read into a number of elements. At first, you need to determine how many numbers (here, ‘n’) to be read. Next, you need to dynamically allocate memory for ‘n’ integers. As you see,‘x’ is an integer pointer which needs to dynamically allocate memory to read ‘n’integers into it fromthekeyboard. 2 Read nintegers into the allocated block using scanf. 3. Complete the printArrayfunction to display ‘n’ integers. void printArray(int...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • Write the functions needed to complete the following program as described in the comments. Use the...

    Write the functions needed to complete the following program as described in the comments. Use the input file course.txt and change the mark of student number 54812 to 80. /* File: course.cpp A student's mark in a certain course is stored as a structure (struct student as defined below) consisting of first and last name, student id and mark in the course.  The functions read() and write() are defined for the structure student.   Information about a course is stored as a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT