Review structures, pointers and dynamic memory allocation from CSIT 839

Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C sort functions". I've also included under files, a sample C source file named sort_ which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to Co Sci 839 or google: "binary search function in C ".

 

You can separate the main function from the definitions of the functions by adding a second C file to your project (Project/Add New Item: C File), and write the function definitions there. Likewise, you can separate the class definition by adding a header file to the project (Project/Add New Item: Header File), and write the class definition there. Thus, you should have two cpp files: courses_ and courses_ and />

Define a class called Course which contains a c-string for its title ( CSIT 839), an integer for its number of units and a character for the letter grade received. Member functions must be added as needed: constructor to initialize a Course object, to edit the information of an existing object, to display Course information, the function for calculating the GPA and a destructor to free dynamically allocated memory for each object. The destructor ensures that each and every object created will be freed. This will make it unnecessary for Quit to call a delete() function to delete all objects and free their allocated memory .

 

In main, declare an array of 10 Course pointers, initialized to NULL and continuously present the following menu to the user asking him or her to choose one:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

If Add new course is selected, pass the array of pointers to an add() function, which dynamically allocates memory for a new Course, reads the course information (title, units and grade) from the user, saves its pointer in the array of pointers and displays the menu again.

 

If Edit is selected, the program passes the array of pointers and size to edit() function that displays all courses with a number next to them and asks which course to edit, like so:

 

Select the course to edit:

 

Math 102

2. CSIT 839

3. English 112

Enter your selection: 2

 

It then reads new course information from the user and writes to the selected course using its pointer, as follows:

 

Enter course name: CSIT 802

 

Enter number of units: 5

 

Enter grade: B

 

If Display a course is selected, the program must pass the array of pointers and size to a sort() function to sort the pointers, ask the user to enter a course, pass the course name and array of pointers to a bin_search() function which will do a binary search on the course names using the pointers and either find and display the course information, or that “Course was not found”. For example, if CSIT 839 is to be searched, when found, it will display: CSIT 839, 3 units, grade: A. 

 

If List all courses is selected, the program must again pass the array of pointers and size to the sort() function to sort the pointers based on their title and then pass the array of pointers and size to a display() function to display all courses.

 

If Display GPA is selected, the program must calculate and display the GPA as follows:

 

GPA = (sum of the products of units x points for all courses) / total number of units for all courses, where points are 4 for A, 3 for B, 2 for C, 1 for D and 0 for F. For example, if only 3 courses are taken worth 3, 5 and 4 units with the letter grade of C, A and B, respectively, the GPA is given by (3 units x 2 points 5 units x 4 points 4 units x 3 points) / (3 5 4) = 3.17.

 

If Delete all courses is selected, the program must pass the array of pointers and size to a delete_course() function that will delete all structures created dynamically and set their pointers to NULL.

 

Selecting Quit must pass all pointers and size to the delete() function that will delete all structures created dynamically, set the pointers to NULL and quit the program. Not explicitly deleting the objects created dynamically will cause memory leak.

 

The following is a sample run of the program:

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 1

 

Enter course name: Math 102

 

Enter number of units: 3

 

Enter grade received: C

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 2

 

Editing: Math 102, 3 units, grade: B

 

Enter course name: Math 102

 

Enter units: 4

 

Enter grade: B

 

Course modified.

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 3

 

Math 102, 3 units, grade: B

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 1

 

Enter course name: CSIT 839

 

Enter number of units: 3

 

Enter grade: A

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 4

 

CSIT 839, 3 units, grade: A

 

Math 102, 3 units, grade: B

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 5

 

GPA = 3.50

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 6

 

All courses deleted!

 

Select one of the following actions:

 

Add new course

Edit an existing course

Display a course

List all courses

Display GPA

Delete all courses

Quit

Enter selection number: 7

 

Press any key to continue.

Need a custom answer at your budget?

This assignment has been answered 3 times in private sessions.

© 2021 Codify Tutor. All rights reserved