Question: An elderly professor who has only ever programmed in FORTAN 7 7 and C decides to re - implement a function in C + +

An elderly professor who has only ever programmed in FORTAN77 and C decides to
re-implement a function in C++. The new program is to be written using modern
C++ memory management and collections from the Standard Template Library.
The result, along with a short test program, is as follows:
1// File: dblsum.h
2 #include
3 #include
4
5 int dblsum(std::unique_ptr>&);
1// File: dblsum.cxx
2 #include
3 #include
4
5 int dblsum(std::unique_ptr>& a)
6{
7 return 2*((*a)[0]+(*a)[1]);
8}
9
10 #include
11 int main()
12{
13 auto a_ptr {
14 std::make_unique>(
15 std::array({3,-5})
16)
17};
18 std::cout << dblsum(a_ptr)<< std::endl;
19 return 0;
20}
(Line numbers are for reference only and are not part of the code).
Question continues on the next page...
Page 5 of 8 Continued Overleaf
(a) Write out the type of dblsum in full in English words, including the type of its
argument. [5]
(b) Why must the argument to this function be passed as a reference? [1]
(c) What is the advantage of passing a reference to an array constant rather than
passing an array variable? [2]
(d) What is the type of (*a) on line 7?[2]
(e) In lines 1416, a constant array object containing two integers is constructed
from a variable array object containing two integers. Is this allowed? If so,
explain why. [2]
(f) FORTRAN77 passes all function arguments by reference. The professors postgraduate student told him that was the same as passing a pointer in C++.
Do you agree with the post-graduate student? If not, explain the difference
between passing a pointer and passing a reference. [3]
(g) The code could be simplified but achieve the same design aims by changing
the type of dblsum. Rewrite the function using a simpler convention, and
show how the test program would have to be modified to accommodate the
change. [8]
(h) The make_unique function on lines 1416 allocates a new array on the heap.
Is the allocated memory freed, and if so, when?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!