Question

Using C++, fix the following code so that (1) Change the student array storage part so...

Using C++, fix the following code so that

(1) Change the student array storage part so that the data is loaded from a file "students.txt". Use an appropriate data   
termination flag.

(2) Sort the student array in ascending order of GPA using insertion sort.

Print the sorted array

(3) Sort the student array in ascending order of ID using insertion sort.

Print the sorted array

(4) Illustrate Binary search for a particular ID that would be found and one that would not be found.

CODE:

#include <iostream>

using namespace std;

struct Date {

int year;

int month;

int day;

};

/*

Declare a struct that can hold a student's record, where each

record contains an ID (int), number of courses taken (int) and

GPA (double).   

*/

struct Student {

int ID;

int numC;

double GPA;

};

void storeStuArray (Student stus[], int max) {

for (int j = 0; j < 8; j++){

stus[j].ID = (j+1) * 1000;

stus[j].numC = (j+1) * 2;

stus[j].GPA = (j % 9)/2.0;

}

}

//Write a function, which given an ID of a student,

//returns the total number of courses that student

//is taking. Print an appropriate message if the student

//does not exist.

int getNumCourses (Student stus[], int max, int target) {

for (int j = 0; j < 8; j++)

if (stus[j].ID == target)

return stus[j].numC;

}   

void printStuArray (Student stus[], int max) {

cout << endl<< "Student data" << endl;

for (int j=0;j<max;j++) {

cout << "Student ID= "<<stus[j].ID <<endl;

cout << "No of courses= "<<stus[j].numC <<endl;

cout << "GPA= "<<stus[j].GPA<<endl;

}

}  

//ID of student with highest GPA

int highGPA (Student stus[], int max) {

double highGPA=stus[0].GPA;

int highID=stus[0].ID;

for (int j=1;j<max;j++)

if (stus[j].GPA >highGPA) {

highGPA=stus[j].GPA;

highID=stus[j].ID;

}

cout << endl<<"High ID=" << highID <<"GPA="<<highGPA;

return highID;

}   

int main() {

void storeStuArray (Student stus[], int max);

cout << "STUDENT PROCESSING";

Student stus[8];

storeStuArray (stus, 8);

printStuArray (stus, 8);

cout <<endl<<"Finding highest GPA ID";

int highID=highGPA (stus,8);

cout << endl<< "High ID=" <<highID;

int val=getNumCourses (stus, 8, 8000);

cout <<endl<<"Student is following " << val << " courses";

system("pause");

}

/*

*/   

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

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

using namespace std;

struct Date {

int year;

int month;

int day;

};

/*

Declare a struct that can hold a student's record, where each

record contains an ID (int), number of courses taken (int) and

GPA (double).

*/

struct Student {

int ID;

int numC;

double GPA;

};

int storeStuArray(Student stus[], int max) {

ifstream in;

in.open("students.txt");

int i = 0;

while (!in.eof() && i < max) {

in >> stus[i].ID >> stus[i].numC >> stus[i].GPA;

i++;

}

}

// Write a function, which given an ID of a student,

// returns the total number of courses that student

// is taking. Print an appropriate message if the student

// does not exist.

int getNumCourses(Student stus[], int max, int target) {

for (int j = 0; j < 8; j++)

if (stus[j].ID == target)

return stus[j].numC;

}

void printStuArray(Student stus[], int max) {

cout << endl << "Student data" << endl;

for (int j = 0; j < max; j++) {

cout << "Student ID= " << stus[j].ID << endl;

cout << "No of courses= " << stus[j].numC << endl;

cout << "GPA= " << stus[j].GPA << endl;

}

}

// ID of student with highest GPA

int highGPA(Student stus[], int max) {

double highGPA = stus[0].GPA;

int highID = stus[0].ID;

for (int j = 1; j < max; j++)

if (stus[j].GPA > highGPA) {

highGPA = stus[j].GPA;

highID = stus[j].ID;

}

cout << endl << "High ID=" << highID << "GPA=" << highGPA;

return highID;

}

void sortByGPA(Student stus[], int n) {

int i, j;

Student temp;

for (i = 1; i < n; i++) {

temp = stus[i];

j = i - 1;

while (j >= 0 && stus[j].GPA > temp.GPA) {

stus[j + 1] = stus[j];

j = j - 1;

}

stus[j + 1] = temp;

}

}

void sortByID(Student stus[], int n) {

int i, j;

Student temp;

for (i = 1; i < n; i++) {

temp = stus[i];

j = i - 1;

while (j >= 0 && stus[j].ID > temp.ID) {

stus[j + 1] = stus[j];

j = j - 1;

}

stus[j + 1] = temp;

}

}

int binarySearch(Student stus[], int n, int searchID) {

int l = 0, r = n - 1;

while (l <= r) {

int m = l + (r - l) / 2;

if (stus[m].ID == searchID)

return m;

if (stus[m].ID < searchID)

l = m + 1;

else

r = m - 1;

}

return -1;

}

int main() {

cout << "STUDENT PROCESSING";

Student stus[8];

int n = storeStuArray(stus, 8);

printStuArray(stus, n);

cout << endl << "Finding highest GPA ID";

int highID = highGPA(stus, n);

cout << endl << "High ID=" << highID;

int val = getNumCourses(stus, n, 8000);

cout << endl << "Student is following " << val << " courses";

sortByID(stus, n);

cout << "After sorting by ID: " << endl;

printStuArray(stus, n);

sortByGPA(stus, n);

cout << "After sorting by GPA: " << endl;

printStuArray(stus, n);

int searchID;

cout << "Enter ID to search: ";

cin >> searchID;

int pos = binarySearch(stus, n, searchID);

if (pos == -1)

cout << "Student ID not found in list" << endl;

else

cout << "Student ID Found in index " << pos << endl;

// system("pause");

}

/*

*/

Add a comment
Know the answer?
Add Answer to:
Using C++, fix the following code so that (1) Change the student array storage part so...
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...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add...

    C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add any new comments or remove comments from teachers. Thank you very much *R 2B PROGRAM 1B: INSERTION SORT Find and rix errors. Ron the program once and save the outpat as a conment at the end of the source file Changed by: IDE #include <iostream> using namespace std: void insertionSort (int aryll, int size) int main double list(1001-(50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8,...

  • where is the error in my code? it wont print all positions in the array for...

    where is the error in my code? it wont print all positions in the array for ascending (ralph is missing) or when it lists who got onto the elevator. This is C++. Input Data Anne 150 Bob 250 Ralph 305 Tim 250 Barbara 85 Jane 160 Steve 180 Tom 210 Mike 165 Shirley 155 Pam 125 Frank 130 Code #include <iostream> #include <fstream> using namespace std; /* The purpose of this program is to compare different ways for people to...

  • struct student { char name[10]; int rank; }; Using the student structure given above, create an...

    struct student { char name[10]; int rank; }; Using the student structure given above, create an array of size 5 students.. Then write a complete C program to sort the students array based on the students rank. Use the following sorting techniques in your code. Don’t forget to print initial array and final (sorted) array. a. Selection sort b. Insertion sort c. Merge sort / Quick sort.

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • Using the following Java program, modify the code so that every time you run your program,...

    Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort) import java.awt.Graphics; import java.applet.Applet; public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 }; public void paint(Graphics g)     {       print(g,"Data items in original order",a,25,25);       sort();       print(g,"Data items in ascending order",a,25,55);     } public void sort() {...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

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