Professional Documents
Culture Documents
Write a function that accepts size S and dynamically allocates memory to a pointer to float type to
store S elements. Avoid memory leakage.
Question 2
Write a function in C++ that dynamically allocates the memory to double-pointer. Boolean data type
can be used.
Question 3
Write a function prototype in C++ that accepts 2D matrix ‘X’ and computes the equation: 2X+3X
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.
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?
Question 8
What is the problem in the below code snippet, explain the problem and its solution? Do not copy
paste the compiler error!
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.
}
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