Roll No…………………..
Dr B R Ambedkar National Institute of Technology, Jalandhar
B Tech (All Branches) – 1st Semester
CSFC0101, Computer Programming
End-Semester Examination, December 2024
Duration: 03 Hours Max. Marks: 50 Date: 02 December 2024
Marks Distribution & Mapping of Questions with Course Outcomes (COs)
Question 1 2 3 4 5 6 7 8 9 10
Number
Marks 10 4 4 3 4 4 4 4 5 8
CO No. 1,2,3 1 1 1 2 2 2 3 2 1,2
Cognitive U Ap An An Ap C An C Ap An
Level
Note:
1. Attempt all the TEN questions available on FOUR pages.
2. Write all the possible steps in your solutions.
3. If you are assuming something, then clearly mention it.
4. CALCULATORS ARE NOT ALLOWED.
1. Answer the following questions with proper examples. [2x5]
a) Write all the different ways to initialize a 2-D array.
1) int arr[3][3] = {{1, 2}, {3}, {4, 5, 6}};------------------------[0.5]
2) int arr[2][2] = {1, 2, 3, 4};--------------------------------------[0.5]
3) int arr[][3] = {{1, 2, 3}, {4, 5, 6}};---------------------------[0.5]
4) int arr[3][3] = {{1, 2, 3}, {4, 7, 3}, {4, 5, 6}};--------------[0.5]
b) What is a pointer to an array?
A pointer to an array is a pointer that points to the entire array, not
just the first element of the array. This differs from a regular
pointer to a single element in the array. -------------------------[0.5]
int arr[5]; ------------------------------------------------------------[0.5]
int (*p)[5] = &arr; // Points to the entire array of 5 integers. --[1]
c) Write the function declaration and function call of the function
foo() that accepts two arguments and returns a float. Note that one
of the arguments must be passed by value and the other by
reference.
1
float foo(int a, float *b); -------------------------------------------[1]
int main() {-----------------------------------------------------------[0.5]
int x = 5; // Argument to be passed by value
float y = 10.5; // Argument to be passed by reference
float result;
// Function call
result = foo(x, &y); ----------------------------------------------[0.5]
printf("Result: %.2f\n", result);
printf("Modified y: %.2f\n", y);
return 0;
}
d) Explain a pointer to a constant integer.
A pointer to a constant integer is a pointer that points to a memory
location holding an integer value, but the value at that location
cannot be modified through the pointer. This means you can only
read the value, not change it. However, the pointer itself (the
address it holds) can be changed to point to another memory
location. -------------------------------------------------------------[0.5]
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
const int *ptr; // Pointer to a constant integer--------------[0.5]
ptr = &a; // Pointer now points to 'a'
printf("Value of a: %d\n", *ptr);
*ptr = 15; // Error: Cannot modify the value of 'a' through
the pointer------------------------------------------------------------[0.5]
ptr = &b; // Pointer now points to 'b'---------------------[0.5]
printf("Value of b: %d\n", *ptr);
return 0;
}
2
e) What is a register storage class? What type of variables can be
declared as register storage classes?
The register storage class in C is used to indicate that a variable
should be stored in a CPU register instead of RAM for faster
access. It is useful for loop counters, frequently accessed
variables, etc. The number of CPU registers is limited. If the
compiler cannot allocate a CPU register, the variable is stored in
RAM. We cannot use the address-of operator (&) with register
variables because they may not have a memory address in RAM.
Ex: register int counter; --------------------[2]
2. a) Write the IEEE 32-bit floating point standard representation of the
following numbers: i) -7024 ii) 6.484375. [2]
i) 11000101110110111000000000000000----------------------[1]
ii) 01000000110011111000000000000000----------------------[1]
b) Convert the following numbers into base 9: [2]
i) (3897)10 ii) (1AF)16 iii) (6573)8 iv) (542)6
i) (5310)9 ii) (528)9 iii) (4654)9 iv) (248)9---------------[0.5x4]
3. a) Write a program to print 24 hours of a day with suitable suffixes
like AM, PM, Noon, and Midnight. [2]
#include <stdio.h>
int main() {
for (int hour = 0; hour < 24; hour++) {---------------------------[0.5]
if (hour == 0) {----------------------------------------------------[0.5]
printf("12:00 Midnight\n");
} else if (hour < 12) {---------------------------------------------[0.5]
printf("%02d:00 AM\n", hour);
} else if (hour == 12) {-------------------------------------------[0.5]
printf("12:00 Noon\n");
} else {
printf("%02d:00 PM\n", hour - 12);
}
}
return 0;
}
3
b) Write a program that will read the value of x from the user and
evaluate the following function using a) nested if statements, b) else if
statements, and c) conditional operator. [2]
1 𝑓𝑜𝑟 𝑥 > 0
𝑦 = { 0 𝑓𝑜𝑟 𝑥 = 0
−1 𝑓𝑜𝑟 𝑥 < 0
a) Using nested if statements----------------------------------[0.5]
#include <stdio.h>
int main() {
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
if (x > 0) {
y = 1;
}
if (x == 0) {
y = 0;
}
if (x < 0) {
y = -1;
}
printf("The value of y is: %d\n", y);
return 0;
}
b) Using nested else if --------------------------------------[0.5]
#include <stdio.h>
int main() {
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
if (x > 0) {
y = 1;
} else {
if (x == 0) {
y = 0;
4
} else {
y = -1;
}
}
printf("The value of y is: %d\n", y);
return 0;
}
c) Using conditional operator-----------------------------[1]
#include <stdio.h>
int main() {
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
y = (x > 0) ? 1 : ((x == 0) ? 0 : -1);
printf("The value of y is: %d\n", y);
return 0;
}
4. A Niven number in mathematics is an integer divisible by the sum of
its digits. A C program to compute a Niven number is given below.
Each line number in the program is marked with a line number. Write
the appropriate C statements in the line numbers to complete the
program. Note that a curly brace is also considered as a line. Writing
any extra line will fetch zero marks. [3]
L1: #include<stdio.h>
L2: int main()
L3:{
L4: unsigned int num, sum = 0, value; // Declaration of variables
L5: scanf("%u", &num); // Input a number to check ----------[0.5]
L6: value = num;
L7: while (num > 0) ------------------------------------------------[0.5]
L8: {
L9: int k = num % 10; -------------------------------------------[0.5]
L10: sum = sum + k; ----------------------------------------------[0.5]
L11: num /= 10; ---------------------------------------------------[0.5]
5
L12: }
L13: if (value % sum == 0) ----------------------------------------[0.5]
L14: printf("YES\n");
L15: else
L16: printf("NO\n");
L17: return 0;
L18:}
5. Write a program in C that accepts a string that contains digits and
converts the string to an integer. For example, if the string is “124”,
then the output is 124. [4]
#include <stdio.h>
int main() {
char str[100]; ----------------------------------------------------------[0.5]
int num = 0, i = 0;
printf("Enter a string containing digits: ");
scanf("%s", str); ------------------------------------------------------[0.5]
while (str[i] != '\0') {--------------------------------------------------[0.5]
if (str[i] >= '0' && str[i] <= '9') {--------------------------------[1]
num = num * 10 + (str[i] - '0'); -------------------------------[1]
} else {
printf("Invalid input: String contains non-digit characters.\n");
return 0;
}
i++;------------------------------------------------------------------[0.5]
}
printf("The converted integer is: %d\n", num);
return 0;
}
6. Write a C program that accepts a positive integer through the
keyboard, and write a recursive function to obtain the prime factors of
the number. [4]
#include <stdio.h>
void foo(int n, int factor) {
if (n <= 1) {------------------------------------------------------------[0.5]
return;
}
if (n % factor == 0) {-------------------------------------------------[0.5]
printf("%d ", factor);
foo (n / factor, factor); --------------------------------------------[1]
6
} else {
foo(n, factor + 1); -------------------------------------------------[1]
}
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {-------------------------------------------------------[0.5]
printf("Please enter a positive integer.\n");
return 1;
}
printf("Prime factors of %d are: ", num);
foo(num, 2); ---------------------------------------------------------[0.5]
printf("\n");
return 0;
}
7. Suppose a 2D Array is given as follows: int A[4][5] =
{5,1,4,7,3,2,8,9,0,1,3,2,8,5,4,8,6,3,1,2}. Assuming the size of an
integer is 2 bytes and the base address of the array is 1260, compute
the values of the following: a) A, b) A+2, c) A[3], d) A[2]+3, e)
&A[1], f) &A[2]+3, g) *(&A[2]+2), h) &A[6]+2. [4]
a) 1260, b) 1280, c) 1290, d) 1286, e) 1270, f) 1310, g) 1300, h) 1340
--------------------------------------------------------------------------[8x0.5]
8. There is a car showroom with ten different cars. You need to construct
a structure to store the specifications of each car appropriately. The
car specification details are as follows: Name of the car, model
number, brand, color, weight, dimension (length and breadth), show-
room price and on-road price. [4]
struct Car {
char name[50]; // Name of the car ---------------------------------[0.5]
int model_number; // Model number
char brand[50]; // Brand ---------------------------------------------[0.5]
char color[20]; // Color ----------------------------------------------[0.5]
float weight; // Weight in kilograms
float length; // Length in meters ------------------------------------[0.5]
float breadth; // Breadth in meters ----------------------------------[0.5]
7
float showroom_price; // Showroom price in INR----------------[0.5]
float onroad_price; // On-road price in INR ----------------------[0.5]
}cars[10]; -----------------------------------------------------------------[0.5]
9. Write a program in C to sort the given array of elements using
insertion sort. Then explain your program step by step using the
following array: A[]={40, 20, 10, 90, 60, 70, 80}. [5]
#include <stdio.h>
void sort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {------------------------------------------------[0.5]
key = arr[i]; ---------------------------------------------------------[0.5]
j = i - 1; --------------------------------------------------------------[0.5]
while (j >= 0 && arr[j] > key) {---------------------------------[0.5]
arr[j + 1] = arr[j]; -----------------------------------------------[0.5]
j = j - 1; -----------------------------------------------------------[0.5]
}
arr[j + 1] = key; ----------------------------------------------------[0.5]
}
}
void print(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int A[] = {40, 20, 10, 90, 60, 70, 80};
int n = sizeof(A) / sizeof(A[0]);
printf("Original Array: ");
print(A, n);
sort(A, n);
printf("Sorted Array: ");
print(A, n);
return 0;
}
Initial Array: {40, 20, 10, 90, 60, 70, 80}
Array iteration 1: {20, 40, 10, 90, 60, 70, 80}---------------------[0.5]
Array iteration 2: {10, 20, 40, 90, 60, 70, 80}
8
Array iteration 3: {10, 20, 40, 90, 60, 70, 80}---------------------[0.5]
Array iteration 4: {10, 20, 40, 60, 90, 70, 80}
Array iteration 5: {10, 20, 40, 60, 70, 90, 80}
Array iteration 6: {10, 20, 40, 60, 70, 80, 90}---------------------[0.5]
10. Answer the following questions with proper justifications. Answers
without a proper justification will incur zero marks. [2x4]
a) Compute the total space required b) How Many time “NITJ” is
to store set[1000]. printed?
typedef struct{ #include<stdio.h>
int xcoord; void main(){
int ycoord; int x;
}point; for(x=-1;x<=10;x++){
typedef struct{ if(x<5)
point vertex[3]; continue;
}Triangle; else
Triangle set[1000]; break;
printf(“NITJ”);
}
Assuming an integer takes 4 bytes, the }
total space required = Zero times
[(4+4)*3]*1000=24000 bytes
b) What is the output of the following d) What is the output of the following
program? program?
#include<stdio.h> #include<stdio.h>
void foo(int *a, int b, int *c, int #include<string.h>
d){ void main()
*a = *a * 3; {
b = b * 3; char str1[]="CaLiCuT";
*c = *c * 3; char str2[]="cAlIcUt";
d = d * 3; printf("%d ",strcmp(str1,str2));
} if(strcmp(str1,str2) >= 0){
void main(){ strcpy(str1,str2);
int a=3, b=6, c=2, d=4; printf("%s",str1);
foo(&a,b--,&c,d+2); }
printf(“%d,%d,%d,%d”,a,b,c,d); else {
} strcpy(str2,str1);
9,5,6,4 printf("%s",str2);
}
}
-32 CaLiCuT