Data Structures coding
CPSC 131 Homework 2 D e a d l i n e :D e a d l i n e : Wednesday, Feb-8 (MoWe sections) Thursday, Feb-9 (TuTh sections) Turn in your submission as hard copy in class.
#1 (will be graded for credit) consists of Part A and Part B:
Pa r t A :Pa r t A : Consider the following class called Myscores that stores all the scores for a game.
class Myscores { public: Myscores() { // constructor
nScores = 0; } void addScore(int newscore) {
score[nScores] = newscore; nScores++;
}
CPSC 131 Homework 2 D e a d l i n e :D e a d l i n e : Wednesday, Feb-8 (MoWe sections) Thursday, Feb-9 (TuTh sections) Turn in your submission as hard copy in class.
#1 (will be graded for credit) consists of Part A and Part B:
Pa r t A :Pa r t A : Consider the following class called Myscores that stores all the scores for a game.
class Myscores { public: Myscores() { // constructor
nScores = 0; } void addScore(int newscore) {
score[nScores] = newscore; nScores++;
}
Homework 2
Homework 2.pdf Saved to Dropbox • Feb 2, 2017, 5<06 AM
} private: int score[10]; int nScores; // number of scores stored };
i) The score member variable can store at most 10 scores. Change the code so that score can store as many scores as needed when a Myscores object is created. (Hint: use dynamic memory). Change/add constructor and destructors as needed.
ii) Add a copy constructor for the above case.
Pa r t BPa r t B: Create a template for class GenericArray to print out the values in arrays with data types: integer, float, or string.
Hints: Example of a class for integer data type is shown below. Note that the below is not a template class and your task is to convert it to a template class that will work for integer, float or string.
// this is only a partial example class array for integer data type. // You need to rewrite it as a template class to support int, float, // and string data types.
class GenericArray { public:
} private: int score[10]; int nScores; // number of scores stored };
i) The score member variable can store at most 10 scores. Change the code so that score can store as many scores as needed when a Myscores object is created. (Hint: use dynamic memory). Change/add constructor and destructors as needed.
ii) Add a copy constructor for the above case.
Pa r t BPa r t B: Create a template for class GenericArray to print out the values in arrays with data types: integer, float, or string.
Hints: Example of a class for integer data type is shown below. Note that the below is not a template class and your task is to convert it to a template class that will work for integer, float or string.
// this is only a partial example class array for integer data type. // You need to rewrite it as a template class to support int, float, // and string data types.
class GenericArray { public:
GenericArray(int array[], int arraysize); // constructor ~GenericArray(); // destructor void print(); // the print function private: int *ptr; int size; };
// This main function requires the template class GenericArray to work and it can be used to test the template class that you created. Do not change.
int main() { // using integer data type
int arraya[5] = { 1, 2, 3, 4, 5 }; GenericArray<int> a(arraya, 5); a.print(); // using float data type float arrayb[5] = { 1.012, 2.324, 3.141, 4.221, 5.327 }; GenericArray<float> b(arrayb, 5); b.print(); // using string data type string arrayc[] = { "Ch1", "Ch2", "Ch3", "Ch4", "Ch5" }; GenericArray<string> c(arrayc, 5); c.print(); return 0; }
GenericArray(int array[], int arraysize); // constructor ~GenericArray(); // destructor void print(); // the print function private: int *ptr; int size; };
// This main function requires the template class GenericArray to work and it can be used to test the template class that you created. Do not change.
int main() { // using integer data type
int arraya[5] = { 1, 2, 3, 4, 5 }; GenericArray<int> a(arraya, 5); a.print(); // using float data type float arrayb[5] = { 1.012, 2.324, 3.141, 4.221, 5.327 }; GenericArray<float> b(arrayb, 5); b.print(); // using string data type string arrayc[] = { "Ch1", "Ch2", "Ch3", "Ch4", "Ch5" }; GenericArray<string> c(arrayc, 5); c.print(); return 0; }
Write the complete code for the class template showing the header files, namespaces and:
i) Template declaration
ii) The Constructor
iii) The Destructor
iv) The print() function
v) Test your class template by running the main() function above and capture the console screen output. Show the runtime screenshot.
Write the complete code for the class template showing the header files, namespaces and:
i) Template declaration
ii) The Constructor
iii) The Destructor
iv) The print() function
v) Test your class template by running the main() function above and capture the console screen output. Show the runtime screenshot.