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

Analyze the following C++ code. What is the code below supposed to do? What woul

ID: 3767940 • Letter: A

Question

Analyze the following C++ code. What is the code below supposed to do? What would the output be?

Please provide a short answer with your explanation

#include <iostream>
using namespace std;

typedef int* IntArrayPtr;

void function1(int *m, int i, int d2);
{
   for (int j = 0; j < d2; j++)
   m[j]=i+j;
}

void function2(int *m, int d2);
{

   for (int j = 0; j < d2; j++)
       cout << m[j] << " ";
   cout << endl;
}

int main()
{
   int d1 = 3, d2 = 4;
   IntArrayPtr *m = new IntArrayPtr[d1];
   for (int i = 0; i < d1; i++)
       m[i] = new int[d2];

   for (int i = 0; i < d1; i++)
       function1(m[i], i, d2);

   for (int i = 0; i < d1; i++)
       function2(m[i], d2);

   for (int i = 0; i < d1; i++)
       delete[] ,[i];

   delete[] m;

   return 0;
}
      

Explanation / Answer

//First of all if we execute the program or code which you have posted,it will not execute since errors are found.

/* please note the following errors.

1.you should not put the semicolon(;) in the function definition

so remove the semicolon from

a)void function1(int *m, int i, int d2); //remove the darkened semicolon from this line

b)void function2(int *m, int d2);//remove the darkened semicolon from this line

2.In last but 2nd line you will get errors because you have not mentioned the array name.i.e [i]

delete[] ,[i];//correct this line

so if it is m[i],then output will be obtained.

so output is

0 1 2 3

1 2 3 4

2 3 4 5