Final Exam v1 Solution
Final Exam v1 Solution
Karachi Campus
NOTE: In Question 1, 0.25 marks for getting the correct answer and
0.75 marks for correct reasoning.
CLO 2: Examine code writing, compiling, debugging and program execution.
Q2: Perform and show dry runs using tables or stacks or any way you see fit. Then give the output of
the following programs. (NOTE: There are no compile time errors in the code.)
[Marks 10, 2.5 each, 35 min]
(i) (iii)
#include <stdio.h> #include <stdio.h>
int mysteryRec(int a, int b) { void my_print(char *str) {
if (b == 1) char *ptr; int i, j;
return a; for (i = 0; *(str + i)!='\0'; i++) {
else ptr = str;
return a + mysteryRec(a, b - 1); for (j = 0; j < (5 - i); j++) {
} printf("%c", *(ptr + j));
}
int main(void) { printf("\n");
printf("The result is %u\n", }
mysteryRec(4, 5)); }
return 0;
} int main() {
char text[] = "ABCDE";
my_print(text);
Output return 0;
The result is 20 }
(ii) Output
#include <stdio.h> ABCDE
void processNums(int nums[], int n) { ABCD
int i = -1, j; ABC
for (j=0; j<n; ++j) { AB
if (nums[j] == 1) { A
i++; (iv)
int temp = nums[i];
nums[i] = nums[j]; #include <stdio.h>
nums[j] = temp;
}//end if int mystery(int bits ) {
}//end for int i, total = 0;
} int mask = 1 << 31;
int main() { for (i = 1; i <= 32; ++i,bits<<=1){
int arr[] = {0,1,0,0,1,1,0,1,0,0}; if ((bits & mask) == mask )
int n = 10; ++total;
for (int i = 0; i < n; i++) }
printf("%d ", arr[i]); return !(total % 2) ? 1 : 0;
printf("\n"); }
processNums(arr, n);
for (int i = 0; i < n; i++) int main( void ){
printf("%d ", arr[i]); printf("Result is %d\n", mystery(53));
return 0; printf("Result is %d\n", mystery(8));
} printf("Result is %d\n", mystery(18));
return 0;
}
Output Output
0 1 0 0 1 1 0 1 0 0 Result is 1
1 1 1 1 0 0 0 0 0 0 Result is 0
Result is 1
CLO3: Justify problem solving techniques and analytical thinking by identifying the concepts and properties of algorithm.
Q3: Consider the code snippet below and answer the questions that follow accordingly.
[Marks 10, 2.5 each, 30 min]
return p;
}
(i) If m is 5 and n is 4 how many function calls to malloc will be made in the function above?
Answer: 6, one before for loop and 5 inside the for loop.
(ii) What and where if any error checking should have been performed in the create_matrix
function?
Answer: What: Check for NULL pointer returned from the malloc functions in case of
unsuccessful at getting DMA. When: just before the for loop. Second within the for loop after
each malloc call.
(iii) The code compiles and runs, but gives memory leakage. What is the fault and how can it be
corrected?
Answer: Fault: The function destroy_matrix does not free up the individual arrays allocated
dynamically inside the for loop. Correction: Use a for loop to first free all the individual array
and then free up p.
(iv) Write an updated version of create_matrix that creates matrixes for any values of m and n with
just 2 calls to malloc function and despite that **p can be used as 2D array.
Answer:
int **create_matrix2(int m, int n){
int **p, *t, i;
p = (int**)malloc(sizeof(int*)*m);
t = (int*)malloc(sizeof(int)*m*n);
for (int i = 0; i < m; ++i) {
p[i] = &t[i*n];
}
return p;
}//end function create_matrix
CLO 4: Design basic problems of the real world through small/medium size programs given as course projects.
[Marks 10, 40 min]
Q4: You are given n jobs and m workers. Each job has a difficulty and a profit, and each worker has
an ability which represents the maximum difficulty level they can handle. The task is to assign
jobs to workers in such a way that maximizes the total profit. Write a C program that performs
the above-mentioned task and returns only the maximum total profit possible. Function
prototype is given below with explanation of all the argument and return value: -.
Sample Output:
Input: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
Output: 100
Input: difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
Output: 0
#include <stdio.h>
// Step 1: For each worker, assign the most profitable job they can handle
for (int i = 0; i < m; i++) {
int maxProfitForWorker = 0; // Keep track of the highest profit for this worker
for (int j = 0; j < n; j++) {
// If the worker can do the job (worker's ability >= job difficulty)
if (worker[i] >= difficulty[j]) {
if (profit[j] > maxProfitForWorker) {
maxProfitForWorker = profit[j]; // Assign the best job (highest profit)
}
}
}
totalProfit += maxProfitForWorker; // Add the maximum profit this worker can earn
}
return totalProfit;
}
int main() {
// Example Input
int difficulty[] = {2, 4, 6, 8, 10};
int profit[] = {10, 20, 30, 40, 50};
int worker[] = {6, 2, 6, 9};
return 0;
}
CLO 4: Design basic problems of the real world through small/medium size programs given as course projects.
[Marks 10, 2 each, 30 min]
Q5: Write a C program to work with a 3D coordinate system. The program should include the
following:
(i) Define a struct named Point3D to represent a point in 3D space with three floating-point
members: x, y, and z.
typedef struct {
float x, y, z;
} Point3D;
(ii) Define a struct named Cuboid to represent a cuboid in 3D space. This structure should contain:
a. A Point3D for the back-bottom-left corner of the cuboid.
b. Three float values representing the length, width, and height of the cuboid.
typedef struct {
Point3D corner; // Back-bottom-left corner of the cuboid
float length, width, height; // Dimensions of the cuboid
} Cuboid;
Implement the following functions:
(iii) float distanceBetweenPoints(Point3D p1, Point3D p2): Calculates the Euclidean distance
between two 3D points.
#include <math.h> // For sqrt