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
  • 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...

  • 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...

  • 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...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

    C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

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
ADVERTISEMENT