PF Final Fall 2023-1
PF Final Fall 2023-1
Final Exam
Fall 2023
Course Code: CS-1002 Course Name: Programming Fundamentals
Instructor(s): Dr. M Shahzad, Dr. Farooque, Dr. Abdul Aziz, Zain, Basit, Sobia, Farooq Zaidi, M Kariz
Date: 18th Dec 2023 Total Time: 3 hours, 09:00am – 12:00pm Total Marks: 100
Student ID: Section
INSTRUCTIONS:
• Return the question paper and make sure to keep it inside your answer sheet.
• Read each question completely before answering it. There are 6 questions and 4 pages (two sided).
• In case of any ambiguity, you may make assumptions. However, your assumption should not contradict any
statement in the question paper.
• Do not write anything on the question paper (except your ID and section).
QUESTION 1: ...................................................................... [CLO: 1, TIME: 20 MINS, POINTS: 15]
Write on the answer sheet the output of the following programs, when they are executed. There are no
compilation errors in the programs.
Part A. Part B.
#include <stdio.h> #include <stdio.h>
struct Element { int main() { int i,j;
int value; int arr1[] = {1, 2, 3};
}; int arr2[] = {4, 5, 6};
void recurseOp(struct Element arr[][3], int arr3[] = {7, 8, 9};
int rows, int cols, int i, int j){ int *ptrArr[] = {arr1,arr2,arr3};
if(i<rows){ printf("Original Array: \n");
if(j < cols){ for ( i = 0; i < 3; ++i) {
printf("%d ",arr[i][j].value); for ( j = 0; j < 3; ++j)
recurseOp(arr, rows, cols, i,j+1); printf("%d ", ptrArr[i][j]);
} printf("\n");
else{ }//end for i
printf("\n");
recurseOp(arr, rows, cols, i+1,0); for ( i = 0; i < 3; ++i) {
} int *start = ptrArr[i];
}//end if (i<rows) int *end = ptrArr[i] + 2;
} while (start < end) {
int main() { int temp = *start;
struct Element arr[2][3] = { *start = *end;
{ {1}, {2}, {3} }, *end = temp;
{ {4}, {5}, {6} } ++start; --end; }//end while
}; }//end for i
recurseOp(arr, 2, 3, 0, 0);
return 0; printf("Modified: \n");
}// end main for ( i = 0; i < 3; ++i) {
for ( j = 0; j < 3; ++j)
printf("%d ", ptrArr[i][j]);
printf("\n");
}//end for i
return 0;
}//end main
printf("\n");
}//end for i
return 0; }//end main
Part B.
#include <stdio.h>
#define MAX_SIZE 5 Output:
int* getMinMax(int *array, const int size); Enter size of array: 5
int main() { Enter 5 elements in array:
int array[MAX_SIZE] = {1, -2, 3, -1, 9}; 1 -2 3 -1 9
int *resultArr =getMinMax(array, MAX_SIZE); Minimum value in array : -2
printf("Min value in array: %d\n", resultArr[0]); Maximum value in array : 9
printf("Max value in array: %d\n", resultArr[1]);
free(resultArr);
return 0;}
int* getMinMax(int *numbers, const int size) {
----------------
----------------
}
Page 2 of 4
Part C.
#include <stdio.h> Output:
#include <string.h> Enter string to remove a
void removeWordFromString(char str[], word from: Programming
char word[], char neww[]) { Fundamental
-----------
----------- Enter the word you want
} removed: gram
int main(){ char str[100], neww[100], word[100];
printf("Enter string to remove a word from:"); After word removed: Proming
gets(str); Fundamental
printf("\nEnter the word you want removed: ");
gets(word);
removeWordFromString(str, word, neww);
printf("\nAfter word removed: %s\n", neww);
return 0; } //end main
Page 3 of 4
QUESTION 5: ...................................................................... [CLO: 2, TIME: 35 MINS, POINTS: 15]
Develop a “C program” to manage electricity consumption and billing information. You are required to utilize
structures to encapsulate details about the electricity usage for different days and implement a tiered billing
system with different rates for various consumption levels. The program should analyze and display
comprehensive information about a customer's electricity consumption.
Part A. Structures Definition [1+1+1 = 3 points]
• Structure “DailyConsumption” with members day representing the day of the month, unitsConsumed
representing units of electricity consumed on that day.
• Structure “ElectricityBill” with members customerName representing the name of the customer, customerID
representing the customer's unique identifier, and dailyConsumptions containing details for each day for 30
days.
• Structure “BillingTier” with members rate representing the rate per unit for the tier, upperLimit representing
the upper limit for the tier(example given below). If -1.0, it indicates an unlimited upper limit.
struct BillingTier billingTiers[] = {
{0.10, 50.0}, // Rate for the first 50 units
{0.15, 100.0}, // Rate for the next 50 units
{0.20, -1.0} // Rate for any units beyond 100 (unlimited)
};
Page 4 of 4