Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CIS22B Homework 5 https://tinyurl.com/cis22b-win18-hw5 Binary File IO and Structures. The purpose of this assignment is to give you practice using C++ structs and binary file

CIS22B Homework 5

https://tinyurl.com/cis22b-win18-hw5

Binary File IO and Structures.

The purpose of this assignment is to give you practice using C++ structs and binary file input / output, and function overloading.

Download this C++ code save it as CIS22B-HW5-XXXXX.cpp and add your code to it. XXXXX are last 5 digits of your ID

Program Steps

Add the code in main() to do the following:

1-Create a binary file named HW5-course-XXXXX.dat where XXXXX are the last 5 digits of your ID. Open the file for writing

2-Create an array of 6 courses (using an array of struct pointers) with the following data:

Name Professor Number of students

CIS02-Computer & Society John Smith 20

CIS03-Business Information System Mary Joe 22

CIS14-Visual Basic Art Lynn 25

CIS18-Introduction to Unix/Linux Edward Ahren 22

CIS22A-Introduction to C++ Hann So 16

CIS22B-Intermediate C++ Tony Nguyen 24

3- Use a loop to write all the array of courses to the binary file HW5-course-XXXXX.dat.

4- Close the file.

5- Open the file HW5-course-XXXXX.dat again for reading

6- Create a text report file named HW5-course-XXXXX.txt for writing and open it.

7-Use a loop to read all the data from the file HW5-course-XXXXX.dat regardless of the number of records, while doing the following steps for each course:

-print the course information to file HW5-course-XXXXX.txt

-calculate the average total student score of each course

= (total score of student 1 + total score of student 2 + .total score of student N) / N

-print the average total student score to HW5-course-XXXXX.txt

8- Close the output report file

Requirements:

-The loop that reads the binary data file in step 7 should be flexible to read any number of records, not just exactly 6 records because users may need to have more courses in the future.

-You can add more functions to the program, but do not modify the already given functions.

-Create a new function named printCourse with an extra second reference parameter of type ofstream (that overloads the existing printCourse() with 1 parameter) to print each course to the output report file.

-Create a new function named printStudents with an extra third reference parameter of type ofstream (that overloads the existing printStudents() with 2 parameters) to print students to the output report file.

Note:

-Function overloading is in chapter 6 and was covered in class previously.

Output on console:

image text in transcribed

Program Output File:

Your output file should look quite similar to the following.

See complete output

Course name: CIS02-Computer & Society

Professor: John Smith

Student name: V836

Scores: 8 16 18 18 6 15 12 19 7 9

Student name: B999

Scores: 0 8 12 8 4 5 11 4 15 13

Student name: X421

Scores: 9 2 7 1 8 14 14 0 20 20

Student name: J556

Scores: 13 0 8 4 6 17 16 5 3 10

Student name: Z343

Scores: 10 12 19 11 14 0 18 3 0 14

Student name: E281

Scores: 17 8 16 4 18 18 16 7 11 8

Student name: Q657

Scores: 20 17 12 19 4 3 11 9 17 0

Student name: W646

Scores: 7 14 17 20 7 3 9 9 20 15

Student name: A651

Scores: 4 8 8 0 4 8 14 2 17 11

Student name: V869

Scores: 11 9 3 3 9 1 4 1 11 16

Student name: W426

Scores: 15 6 4 20 11 12 17 11 5 18

Student name: C692

Scores: 5 13 11 1 7 20 10 0 4 15

Student name: T423

Scores: 4 19 13 13 16 7 0 13 4 11

Student name: V416

Scores: 20 19 2 7 2 9 12 4 5 7

Student name: O263

Scores: 8 16 0 19 9 14 5 8 8 15

Student name: H558

Scores: 0 10 19 7 12 20 10 19 12 17

Student name: T466

Scores: 11 8 0 0 12 11 9 5 20 6

Student name: E313

Scores: 1 8 5 11 13 14 3 20 17 13

Student name: J414

Scores: 11 15 18 2 12 20 10 14 11 10

Student name: E177

Scores: 7 14 20 1 14 20 15 17 2 4

Average total student score: XXX.XX

----------------

Course name: CIS03-Business Information System

Professor: Mary Joe

Student name: XXXXX

..

CIS22B-HW5-XXXXX.cpp file.

Average total student score: XXX.XX

#include

#include

#include

#include

#include

#include

#include

#include

// Home work 5

using namespace std;

const int MAX_COURSE = 10;

const int SCORES_SIZE = 10;

const int MAX_SCORE = 20;

const int MAX_RAND = 999;

const int MAX_STUDENT_NAME = 50;

const int MAX_COURSE_NAME = 50;

enum EnrollStatus {FULL_TIME = 1, PART_TIME = 2};

struct Student {

char name[MAX_STUDENT_NAME];

int scores[SCORES_SIZE];

EnrollStatus status;

};

struct Course {

char courseName[MAX_COURSE_NAME];

char* professor;

int studentCount = 0;

Student** students;

};

Student** getRandomStudents(int stCount, int maxScore);

void printStudents(Student** students, int stCount);

Course* getCourse(string courseName, string professor, int studentCount);

void printCourse(Course* course);

int main()

{

return 0;

}

// DO NOT MODIFY CODE BELOW THIS

// Get a string that starts with a random letter followed by a random number not exceeding maxValue

string getRandomString(int maxValue) {

int r = rand() % 26;

int r2 = rand() % (maxValue + 1);

stringstream ss;

ss

string s;

ss >> s;

return s;

}

// Get dynamic array of random students

Student** getRandomStudents(int stCount, int maxScore, int maxRand) {

Student** stu = new Student*[stCount];

for (int i = 0 ; i

stu[i] = new Student;

string name = getRandomString(maxRand);

strcpy(stu[i]->name, name.c_str());

for (int j = 0; j

stu[i]->scores[j] = rand() % (maxScore + 1);

}

}

return stu;

}

// Print dynamic array of random students

void printStudents(Student** students, int stCount) {

for (int i = 0; i

cout name

cout

for (int j = 0 ; j

cout scores[j];

cout

}

}

// Create a course and fill in random students.

// Call getRandomStudents()

Course* getCourse(string courseName, string professor, int studentCount) {

Course* c = new Course;

strcpy(c->courseName, courseName.c_str());

c->professor = new char[professor.size() + 1];

strcpy(c->professor, professor.c_str());

c->students = getRandomStudents(studentCount, MAX_SCORE, MAX_RAND);

c->studentCount = studentCount;

return c;

}

// Print a course

// Call printStudents to print all students

void printCourse(Course* c) {

cout courseName

cout professor

printStudents(c->students, c->studentCount);

cout

}

reating course output file Hw5-course-Xxxxx. dat losing course output file Hw5-course-XXXXX. dat pening course output file Hw5-course-XXXXX. dat pening course report Hw5-course-XXXXX.txt Reading course input file Hw5-course-XXXXX. dat. riting a course to report Hw5-course-XXXXX. txt riting a course to report HW5-course-XXXXX.txt riting a course to report HW5-course-XXXXX.txt riting a course to report HW5-course-XXXXX.txt riting a course to report HW5-course-XXXXX.txt riting a course to report HW5-course-XXXXX.txt losing report Hw5-course-XXXX.txt Process returned 0 (Ox0) xecution time 0.080 s Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions