CJCoding With Joseph
15free / daySign inCreate account

Fixed-Size Array with a Size Parameter

Build a class template FixedArray<T, N> from scratch. T is the element type and N is an int NON-TYPE template parameter: a number fixed at compile time, so FixedArray<int, 3> and FixedArray<int, 5> are different classes with different sizes.

Requirements: - a private member array T items[N] - a constructor that sets every element to T{} - void set(int index, T value): stores value at index - T get(int index) const: returns the element at index - int size() const: returns N - void print() const: prints all elements separated by one space, then a newline

Indexes used by the tests are always valid. The tests create a FixedArray<int, 3>, set index 0 to 4 and index 2 to 9, print it and its size; create a FixedArray<double, 2>, set index 0 to 1.5, print it and its size; create a FixedArray<int, 5> and print it untouched and its size; and finally print the sum of the first array's index 2 and the second array's index 0. Every print and every size goes on its own line.

Do NOT write a main function.

Expected Output:

4 0 9
3
1.5 0
2
0 0 0 0 0
5
10.5

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Templates
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.