You are on page 1of 5

Question 1

Write a function that accepts size S and dynamically allocates memory to a pointer to float type to
store S elements. Avoid memory leakage.

float* memoryAllocation(int size)


{
if (size == 0)
return nullptr;
else
return new float[size]();
}

Question 2

Write a function in C++ that dynamically allocates the memory to double-pointer. Boolean data type
can be used.

bool** twoDAllocation(int size)


{
if (size == 0)
return nullptr;
else
return new bool*[size]();
}

Question 3

Write a function prototype in C++ that accepts 2D matrix ‘X’ and computes the equation: 2X+3X

void solveEquation(int **X, int rows, int cols);

Question 4

Think and suggest a real-world programming scenario where it is very important to use an array
growing concept. Briefly explain your answer.

Nadra System, Data stored in a file etc.

Question 5

Identify one logical error and one syntax error in the following code snippet
newarray = new int[s+1]; // address cannot be assigned to a variable
Logical Error: oldarray is not set free after copyArray, so memory leaks.
Note: memory leakage, in actual, is not logical error. But it is considered logical.

Question 6

Write a C++ function that creates a static 2D array of 5x5 size of type bool. Suppose indices
represent people and the value at row i and column j of a 2D array is true just in case i and j are
friends, or false otherwise. Initialize your array to represent the following configuration:

void friendStatus()
{
const int rows = 5;
const int cols = 5;
bool friends[rows][cols] = {
{false, true, false, true, true},
{ true, false, true, false, true },
{ false, true, false, false, false },
{ true, false, false, false, true },
{ true, true, false, true, false }
};
}

Question 7
In a 2D dynamic array, why do we need an array of pointers?

We need an array of pointers in 2D dynamic array because we need contiguous memory


to support 2D array.

Question 8

What is the problem in the below code snippet, explain the problem and its solution? Do not copy
paste the compiler error!

First parameter shall be passed by reference.


Pointer c shall not be assigned new memory location in a function.

Second solution:
First and third parameter shall be passed by reference.
*c shall be set free before assigning new address to it.

Question 9

Write a function that makes a 2D array of 4 rows such that size of row 1 is 3, size of row 2 is 7, and
size of row 3 and 4 is 4. Avoid memory leakage and dangling pointer.
// Assume any datatype. I am considering integer type. Question justifies if student
does not return double pointer.
int** allocateMemory()
{
int rows = 4;
int** dptr = new int*[rows]();
dptr[0] = new int[3]();
dptr[1] = new int[7]();
dptr[2] = new int[4]();
dptr[3] = new int[4]();
return dptr;
}

Question 10

Write a C++ function that takes accepts 2D square matrix and rotates the matrix clockwise by 90
degrees.

void Rotate2Darray(int** array, int row, int column)


{
int temp = 0;
for (int i = 0; i < row / 2; i++)
{
for (int j = i; j < column - i - 1; j++)
{
temp = array[i][j];
array[i][j] = array[column - 1 - j][i];
array[column - 1 - j][i] = array[column - 1 - i][column - 1 - j];
array[column - 1 - i][column - 1 - j] = array[j][column - 1 - i];
array[j][column - 1 - i] = temp;

}
cout << endl;
}
}

Example

Original 2D array

1 32 11 -4 2

2 14 18 8 5

13 15 7 17 13

5 12 27 3 2

15 2 71 0 -7

Rotated 2D array
15 5 13 2 1

2 12 15 14 32

71 27 7 18 11

0 3 17 8 -4

-7 2 13 5 2

You might also like