Managing dynamic arrays and grade management- managing


Assignment: Object Oriented Programming

1 Managing Dynamic Arrays

Managing dynamically sized arrays can be a pain while dealing with memory on the heap. Memory leaks, dangling pointers, etc can lead to invalid memory accesses/segmentation faults that halt your program abruptly. Your task is to design a DataVector class that encapsulates the implementation of a dynamically sized double array. The interface should be such that the users of your class should not be exposed to the underlying implementation details such as memory allocation (new), release (delete), resize, etc. Your DataVector class must have the following public interface. The di_culty levels are marked as L1, L2 and L3, going from easy to hard.

1. DataVector ( ) ; //L2

This is the default constructor with no parameters. By default, this allocates space for a double array of size 10 and assigns a default value of 0 to each of them.

2. DataVector ( unsignedint i n i t S i z e , double i ni t Va l ue ) ; //L2

This is another constructor of the class that takes in arguments and allocates space for a double array of size initSize and assigns a default value of initValue to each of them.

3. DataVector ( ) ; //L2

This is the destructor that does any cleanup necessary such as releasing all the memory on the heap using the delete operator.

4. void Pr i nt I t e ms ( ) ; //L2

This prints all the items in a single line and successive items are separated by a single space. After the last item there should be a space and a newline character.

5. unsignedint Get Si ze ( ) ; //L1

This should return the current size of the double array.

6. void Res er ve ( unsignedint newSi ze ) ; //L3

This function should increase the size of the array to newSize. Note that you would have to allocate a fresh double array, to which you will have to copy the items from the old double array, and then release the memory for the old double array. If newSize is less than the old size, then you copy just the _rst newSize items from the old array. If newSize is greater than the old size, then you copy all items from the old array.

7. double GetItemAt ( unsignedint i ndex ) ; //L1

This returns the item at the speci_ed index. Assume index is in the range 0 to GetSize() - 1.

8. void SetItemAt ( unsignedint i ndex , double val ) ; //L1

This sets the item at the speci_ed index to val. Assume index is in the range 0 to GetSize() - 1.

9. double GetSum( ) ; //L2

This returns the sum of all items.

Note that you must use new and delete to manage the double array internally. All pointers, counters, etc for the array bookkeeping must be made private in your class.

2 Grade Management

Now you will apply your DataVector class to manage the student grades of CS31. You will implement a GradeManager class that internally uses an array of DataVector objects, that were created dynamically using the new operator, to record the homework grades for a set of students. Again, the idea is to encapsulate the underlying implementation such that the user of GradeManager will not know that DataVector is being used internally. Your GradeManager class must have the following public interface:

1. GradeManager ( unsignedint nStudents , unsignedint nHWs ) ; //L3

This is the only constructor of the class, and it speci_es the number of students nStudents and number of homeworks nHWs for the class. You should use these to dynamically set the array sizes. Also, if nStudents is 100 then the student IDs will range from 0 to 99. Similarly, if nHWs is 10 then the homework IDs will range from 0 to 9.

2. GradeManager ( ) ; //L2

This is the destructor that does any cleanup necessary such as releasing all the memory on the heap using the delete operator.

3. void Pr i nt Gr ades ( ) ; //L2

This prints all the homework grades for all students. Each student's grades are printed on a separate line, and the successive homework grades of each student are separated by a single space. After the last grade on a line, there should be a space and a newline character.

4. unsignedint Ge t Cl as s Si z e ( ) ; //L1

Returns the number of students.

5. unsignedint GetHWCount ( ) ; //L1

Returns the number of homeworks.

6. void SetGrade ( unsignedint sID , unsignedint hwID , double val ) ; //L2

This sets the grade of student sID to val for the homework hwID.

7. double GetGrade ( unsignedint sID , unsignedint hwID) ; //L2

This returns the grade of student sID for the homework hwID.

8. double Get Tot al Scor eFor St udent ( unsignedint sID ) ; //L2

This returns the sum of the respective student's scores on all homeworks.

9. double GetTotal ScoreForStudentWi thDrop2Least ( unsignedint sID ) ; //L3

This returns the sum of the respective student's scores on all homeworks except the least 2 homeworks. Henceforth, this is referred to as the Drop2 policy.

10. unsignedint GetBestStudent ( ) ; //L2

This returns the sID of the student who has the best total score across homeworks with the Drop2 policy.

11. unsignedint GetNumStudentsInRange ( double low , double hi gh ) ; //L3

This returns the number of students whose total homework scores (with Drop2 policy) lie in the range low <= totalscore < high. (Including low and excluding high).

12. double GetCl assAverage ( ) ; //L2

This returns the average of the total homework scores (with Drop2 policy), across all students.

13. double GetClassSTD ( ) ; //L2

This returns the population standard deviation of the total homework scores (with Drop2 policy), across all students. The standard deviation of a set of n total scores t whose average is m, is given by,

You can use the standard library sqrt() function from cmath header to _nd the square root.

3 Dos/Donts/General Advice

1. All your code should be written inside the two classes DataVector and GradeManager.

It is recommended that you do not modify anything outside these classes (main() function especially). But if you really want to write your own test cases in the main() function, then it is your responsibility to revert those changes before submitting on the server.

2. A set of basic tests that call into the public inferface on the 2 classes is given to you in the main() function. These tests might not be complete, and if your output matches the output given at the end of this document, then it does not necessarily mean that you will get full credit on the assignment. We will be designing new test cases for grading purposes, and hence, it is your responsibility to make sure the implementation works as expected. Nevertheless, these tests may give you some ideas on how to use the public interface.

3. It is recommended to start implementing the easy functions _rst. You can also follow the order listed above. This order will help you build the class one step at the time, and some of the earlier functions can be called/reused in later functions if needed.

4. Reusability is one of the main objectives of Object Oriented Programming. So, feel free to reuse any function/code you might have already written.

5. You might run into Segmentation Faults (a.k.a SIGSEGV on unix systems), or Invalid memory access/Bad access or Protection faults, or any kind of memory corruption issues. If you do, then the following checks might help.

_ Check if your indexes to the dynamically allocated arrays are within the legal range. For example, if you allocated a 10 element double array using the syntax new double[10], then make sure your index values to this array are in the range 0 9.

_ Check if you have any dangling pointers. These are pointers that hold addresses to memory that have already been released using the delete operator. It is usually a good practice to set your pointer to NULL after you have called delete on it. For example, delete [ ] pt r ;

_ Check if you are dereferencing a NULL pointer.

Solution Preview :

Prepared by a verified Expert
Programming Languages: Managing dynamic arrays and grade management- managing
Reference No:- TGS01673302

Now Priced at $45 (50% Discount)

Recommended (90%)

Rated (4.3/5)