Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSCI 2380 Midterm Sample Problems These problems are similar to some of the ques

ID: 3826429 • Letter: C

Question

CSCI 2380 Midterm Sample Problems These problems are similar to some of the questions on the midterm. They are not comprehensive the midterm contains questions on other topics (including memory allocation, file input/output, and dynamic array-based data structures). Problem I. What does the following program print? Draw a box around your solution. tinclude Kiost ream> using namespace std; int main 0 int 2x1 3; int 10; int yl; int y2; int z; 5x2, syl cout X1 court x1 yl y2 endl: y2 4: cout x1 2x2 y2 endla cout x1 cc yl z endl. court xl x2 y2 z endl: 6x1. 2 cc endl.

Explanation / Answer

Here is the explanation for Problem 1:

#include <iostream>
using namespace std;
int main()
{
    int x1 = 3;   //Initializes x1 with 3.       (Assume the address of x1 is 1111)
    int x2 = 10;   //Initializes x2 with 10.   (Assume the address of x1 is 2222)
    int* y1;   //Declares y1 an integer pointer.(Assume the address of x1 is 3333)  
    int* y2;   //Declares y2 an integer pointer.(Assume the address of x1 is 4444)
    int **z;   //Declares z an integer pointer pointer.
   
    y1 = &x1;   //y1 is assigned the address of x1, i.e., 1111. So, y1 = 1111.
    y2 = &x2;   //y2 is assigned the address of x2, i.e., 2222. So, y2 = 2222.
    z = &y1;   //z is assigned the address of y1, i.e., 3333. So, z = 3333.
   
    cout << *y1 << " " << *y2 << " " << **z << endl;
        //The value at address in y1(1111) i.e., 3.
        //The value at address in y2(2222) i.e., 10.
        //The value at address z(3333) at address *z(1111) i.e., 3.
    x1 += 1;   //The value in x1 is incremented, so now x1 = 4.
   
    cout << x1 << " " << *y1 << " " << *y2 << " " << **z << endl;
        //The value in x1 i.e., 4
        //The value at address y1(1111) i.e., 4.
        //The value at address in y2(2222) i.e., 10.
        //The value at address z(3333) at address *z(1111) i.e., 4.
    *y2 += 4;   //The value at address y2(2222) is increased by 4. i.e., x2 = 14.
   
    cout << x1 << " " << x2 << " " << *y2 << endl;
        //The value in x1 i.e., 4.
        //The value in x2 i.e., 14.
        //The value at address in y2(2222) i.e., 14.  
    **z += 1;   //The value at address z(3333) at address *z(1111) is incremented. i.e., x1 = 5.
   
    cout << x1 << " " << x2 << " " << *y1 << " " <<**z << endl;
        //The value in x1 i.e., 5.
        //The value in x2 i.e., 14.
        //The value at address y1(1111) i.e., 5.
        //The value at address z(3333) at address *z(1111) i.e., 5.
    z = &y2;   //This leads to error. An integer pointer is being assigned to integer pointer pointer.
            //If the instruction is modified to z = &y2;, the the compilation continues,
            //else the code will result in error, and will not compile.
            //After modification, the value in y2(4444) i.e., 2222 is copied to z, i.e., z = 2222.
   
    cout << x1 << " " << x2 << " " << *y2 << " " << **z << endl;
        //The value in x1 i.e., 5.
        //The value in x2 i.e., 14.
        //The value at address y2(2222) i.e., 14.
        //The value at address z(4444) at address *z(2222) i.e., 14.
    *z = &x1; //The address of x1 i.e., 1111 is assigned to the location in z(4444), i.e., y2 = 1111.
   
    cout<< x1 << " " << *y1 << " " << *y2 << " " << **z << endl;
        //The value in x1 i.e., 5.
        //The value at address y1(1111) i.e., 5.
        //The value at address y2(1111) i.e., 5.
        //The value at address z(4444) at address *z(1111) i.e., 5.
    x1 += 1;   //The value in x1 is incremented, so now x1 = 6.  
}   

And therefore, the output is (after modifying the instruction that leads to compilation error):

3 10 3

4 4 10 4

4 14 14

5 14 5 5

5 14 14 14

5 5 5 5