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

For programming questions, show your code and take a snapshot of your code and t

ID: 3549097 • Letter: F

Question

For programming questions, show your code and take a snapshot   of your code and the result. Thank you

Question 1 :

What will be printed? Assume the following lines are executed sequentially

(i.e., line by line, from top to bottom)



Question 2:


What is the output of the following code?

               


      int x;   


     int *p;


    


      int *q;


      p = new int[10];


      q = p;


      *p = 4;


      for (int j = 0; j < 10; j++)


      {


           x = *p;


       p++;


     *p = x + j;


     }


      for (int k = 0; k < 10; k++)


      {


           cout << *q << " ";


          q++;


     }


      cout << endl;


question 3;

What is the output of the following code?


int *p;      //Line 1


int *q;   //Line 2


p = new int[5];   //Line 3


*p = 2;   //Line 4


for (int i = 1; i < 5; i++)   //Line 5


  p[i] = p[i ? 1] + i;  //Line 6


  q = p;   //Line 7


    delete [] p; //Line 8


         for (int j = 0; j < 5; j++)    //Line 9


         cout << q[j] << " ";  //Line 10


         cout << endl;   //Line 11


For programming questions, show your code and take a snapshot of your code and the result. What will be printed? Assume the following lines are executed sequentially (i.e., line by line, from top to bottom) What is the output of the following code? int x; int *p; int *q; p = new int[10]; q = p; *p = 4; for (int j = 0; j

Explanation / Answer

#include<iostream>
using namespace std;
int main()
{
int x = 3, y = 4;
int *ptr = &x, *ptr2 = &y;

cout << *ptr << endl; // output (1)
cout << *ptr2 << endl; // output (2)

*ptr = *ptr + 1;

if(ptr == ptr2)
cout << "Boom ";      // output (3)
else
cout << "Bang ";      // output (3)
if(*ptr == *ptr2)

cout << "Humpty ";      // output (4)
else
cout << "Dumpty ";      // output (4)

int ar[4] = {11, 22, 33, 44};
ptr = ar;
cout << *ptr << endl;    // output (5)

*ptr = *ptr -1 ;
cout << *ptr << endl;    // output (6)

ptr = ptr+3;
cout << *ptr-- << endl;    // output (6)

return 0;
}

// output1
// since ptr pointint to 3.. *ptr prints value of 3.
// output1
// since ptr2 pointint to 4.. *ptr2 prints value of 4.
// *ptr = *ptr+1
// thus value pointed by ptr increases by 1.
// since ptr and ptr2 are not same both are different addresses
// output 3 prints Bang
// since ptr and ptr2 are pointing to same value i.e 4
// this prints Humpty
// ptr points to base address of array thus
// *ptr prins first elements thus   11
// *ptr = *ptr -1 ;
// value pointed by ptr decreases by 1. i.e 11 - 1 = 10;
// *ptr prints 10
// ptr = ptr + 3
// ptr points to 3rd elements
// so *ptr prints 44.
/* OUTPUT IS
3
4
Bang
Humpty
11
10
44
*/

// PROGRAM 2
#include<iostream>
using namespace std;
int main()
{
int x;
int *p;
int *q;
p = new int[10]; // p assigned to array of 10 integers.
q = p;          // now q pointint to base address of array pointed by p.
*p = 4;          // now first element of array is initialized with 4. p[0] = 4;
for (int j = 0; j < 10; j++)
{
x = *p; // x holds value pointed by p.
p++;     // increment pointer so it points to next element in array.
*p = x + j; // now value pointed by next element in array is equal to *p + value of j.
// *p is equal to value pointed by x + value of j. thus it will be as follows.
// p[1] = 4 + 0 = 4
// p[2] = 4 + 1 = 5
// p[3] = 5 + 2 = 7
// p[4] = 7 + 3 = 10
// p[5] = 10 + 4 = 14
// p[6] = 14 + 5 = 19
// p[7] = 19 + 6 = 25
// p[8] = 25 + 7 = 32
// p[9] = 32 + 8 = 40
}
for (int k = 0; k < 10; k++)
{
cout << *q << " "; //since p and q points to same addresss it prints contents of array pointed by p.
q++;
}
cout << endl;
return 0;
}

/* OUTPUT
4 4 5 7 10 14 19 25 32 40
*/

// PROGRAM 3.
#include<iostream>
using namespace std;
int main()
{
int *p; //Line 1
int *q; //Line 2
p = new int[5]; //Line 3
*p = 2; //Line 4
for (int i = 1; i < 5; i++) //Line 5
p[i] = p[i-1]+i; // Line 6
q = p; //Line 7
delete [] p; //Line 8
for (int j = 0; j < 5; j++) //Line 9
cout << q[j] << " "; //Line 10
cout << endl; //Line 11
return 0;
}

//Line 1 :   create pointer to integer i.e p
//Line 2 :   create pointer to integer i.e q
//Line 3 :   p is pointing to new array of integer size 5.
//Line 4 :   *p = 2 initialize first element of array with value 2 i.e p[0] = 2;
//Line 5 :   for loop to fill array elements pointed by p
//Line 6 :   p[1] = p[0] + 1 = 2 + 1 = 3
//           p[2] = p[1] + 2 = 3 + 2 = 5
//           p[3] = p[2] + 3 = 5 + 3 = 8
//           p[4] = p[3] + 4 = 8 + 4 = 12
//Line 7:    now q and p pointing to same array;
//Line 8:    delete pointer p here.
//Line 9:    for loop
//Line 10:   print contents pointed by q.
//Line 11:   new line.

//OUTPUT
//0 0 5 8 12
            

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote