The code segment in Listing 1, when completed and executed, creates nums, an uninitialized 4-integer array. It stores 4 and 5 as the first and second elements, respectively, of the array. It adds the first and second elements of the array and stores their sum as the third element of the nums. It prints the third element of nums. It then adds the second and third elements of the array and stores their sum as the fourth element of nums. Finally, it prints the fourth element of nums. Do not add numeric literals for either the second blank on line 4 or the second blank of line 6. Use expressions involving the addition of array elements for each blank. Complete the code segment. [15 points]
Listing 1: Code Segment
----------------------------------------------------------------------
1. int nums ______;
2. nums _____ = 4;
3. nums ____ = 5;
4. nums ___ = ___ ;
5. cout<<"third element: "<<nums____<<endl;
6. nums____ = ____;
7. cout<<"fourth element: "<<nums____<<endl;
B. From the choices given, select the alternate execution sequence(s) for which the end-user of the code segment experiences the same interactivity.
Answer
A.)only i, ii and vii only
B.)ii only
C.)iii and iv
D.)ii and iv [5 points]
i. 1, 2, 3, 4, 5, 7, 6
ii. 1, 3, 2, 4, 5, 6, 7
iii. 1, 3, 2, 6, 5, 4, 7
iv. 1, 3, 2, 4, 6, 7, 5
v. 1, 2, 3, 4, 6, 5, 7
A.
//c++ program completion of code segment
#include <iostream>
using namespace std;
int main() {
//initialising array
int nums[4];
nums[0]=4;//first element in array
nums[1]=5;//second element in array.
nums[2]=nums[0]+nums[1];//adding first and second element and store
as third element.
cout<<"Third element "<< nums[2]<<endl;//printing
third element.
nums[3]=nums[2]+nums[1];//adding second and third element and store
in fourth element.
cout<<"fourth element "<<
nums[3]<<endl;//printing fourth element.
return 0;
}
screenshot of code and output is as follows:
Please refer to the screenshot of the code to understand the indentation of the code.
B.
option B, II only.:the sequence ii only follows alternate solution.
from the figure ,only option 2 follows the alternate solution of the code segment.others do not follow the order.
The ii follows the same addition order of the code segment.
Get Answers For Free
Most questions answered within 1 hours.