Implementing a One Dimensional Array ADT
Implement the one dimensional array ADT described in HSM Ch.2.2, exercise 1,
as a C++ class.
Rather than making the array elements specifically float
,
use a template <class ElementType>
or
typedef float ElementType
for the element type, so
that it can easily be changed.
If the LHS of an assignment is smaller than the RHS, copy across only the
required elements.
If the RHS of an assignment is smaller than the LHS, copy across all the
elements and initialize the rest of the LHS to the initial value for the
array elements.
If you use a typedef float ElementType
for the element type,
make sure that the class works with
TestOneDArrayClass.cpp.
If you use a template <class ElementType>
,
make sure that the class works with
TestOneDArrayTemplateClass.cpp.
Both should produce the output:
The default array is 0
The 5 element array is 0 0 0 0 0
The 10 halves array is 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
The other 10 halves array is 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5
Enter 5 elements 6 6 6 6 6
The 5 elements are 6 6 6 6 6
Assigning 5 elements to 10 halves
The 10 elements are 6 6 6 6 6 0.5 0.5 0.5 0.5 0.5
Assigning 10 half elements to 5
The 5 elements are 0.5 0.5 0.5 0.5 0.5
First half is 0.5
Assign -1 to prime indices in 10 halves
The 10 elements are -1 -1 -1 -1 0.5 -1 0.5 -1 0.5 0.5
Answer
This answer uses typedef float ElementType
.
Answer
This answer uses template <class ElementType>
.