Assignment 6 - 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 diculty

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 ( unsigned int i n i t S i z e , double ini tVa lue ) ; //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 int I t ems ( ) ; //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. unsigned int GetSize ( ) ; //L1

This should return the current size of the double array.

6. void Reserve ( unsigned int newSize ) ; //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 ( unsigned int index ) ; //L1

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

8. void SetItemAt ( unsigned int index , 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 ( unsigned int nStudents , unsigned int 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 intGrades ( ) ; //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. unsigned int Ge tClas sSi z e ( ) ; //L1

Returns the number of students.

5. unsigned int GetHWCount ( ) ; //L1

Returns the number of homeworks.

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

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

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

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

8. double GetTotalScoreForStudent ( unsigned int sID ) ; //L2

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

9. double GetTotalScoreForStudentWithDrop2Least ( unsigned int 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. unsigned int GetBestStudent ( ) ; //L2

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

Drop2 policy.

11. unsigned int GetNumStudentsInRange ( double low , double high ) ; //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 GetClassAverage ( ) ; //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 t1; t2; t3:::tn

whose average is m, is given by,

2

r

(t1

    • 12 years ago
    Complete solution A++
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      datavector.zip