0% found this document useful (0 votes)
12 views8 pages

CSF111 QP Sol

The document contains the midsemester examination solutions for a Computer Programming course at BITS Pilani Hyderabad Campus. It includes multiple-choice questions (MCQs) covering various C programming concepts, code snippets, and programming tasks related to user input and calculations. Additionally, it features solutions for programming exercises involving meal selection, ice cream sales prediction, shortest path calculation, palindrome detection, and array manipulation.

Uploaded by

korasikha27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

CSF111 QP Sol

The document contains the midsemester examination solutions for a Computer Programming course at BITS Pilani Hyderabad Campus. It includes multiple-choice questions (MCQs) covering various C programming concepts, code snippets, and programming tasks related to user input and calculations. Additionally, it features solutions for programming exercises involving meal selection, ice cream sales prediction, shortest path calculation, palindrome detection, and array manipulation.

Uploaded by

korasikha27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

BITS Pilani Hyderabad Campus

CS F111
Computer Programming
Midsemester Examination Solution
Date: 6th March 2025 Time: 9:30a.m. -11:00 a.m. Weightage: 30% Total Marks = 90

Instructions: Q1-10 are MCQ type question with only one correct answer and no negative marking.
Q1-5 are of 1 Mark. Q6-10 are of 2 marks.

1. The range of type 2. The control string for 3. Which of the following operator has
signed char in C- double type in C is- higher precedence than ‘ + ‘ (binary
language is - (a) %db plus) in C ?
(a) -127 to 128 (b) %dl (a) ! (b) =
(b) -128 to 127 (c) %lf
(c) && ( d) <
(c) -128 to 128 (d) % ld
(d) -127 to 127

4. Assuming that we have a .c file temp.c that has a 5. Which of the following is a standard
program that uses one or more math functions, and identifier in C ___
we have included <math.h>, what is the command
we should use to compile the program ? (a) return (b) scanf
(a) $gcc temp.c –ls (b) $gcc temp.c –ld
(c) void ( d) int
(c) $gcc temp.c –lh ( d) $gcc temp.c –lm

6. Look at the following code snippet. 7. Look at the following code snippet.
val=0; int a=6, b=9;
val= (a>b ? (a>c ? a : c) : (c == b ? c : a)); float res;
printf("val =%d",val); res=b/(float)a;
if a=3, b=5, and c=2, what is printed? printf("result is : %f \n", res);
the value of res in printf statement is:
(a) 3 (b) 0 (c) 5 (d) 2 (a) 1.000000 (b) 1.500000 (c) 1 (d) error

8. The binary representation of decimal value 9. Look at the following code snippet.
137 is: int a;
for(a=1+2; ++a<10; a++) {
(a) 10001001 printf("Hello\n");
(b) 01001001 }
(c) 01000101 “Hello” is printed _ number of times.
(d) (10010101 (a) 6 (b) 5 (c) 4 (d) 3

10. What will be the output of this code snippet?


int main()
{
printf("%ld,%d",sizeof(printf("BITS")),printf("BITS"));
return 0;
}
(a) BITS4,4 (b) BITS,BITS,4 (c) BITS,BITS,4,4 (d)None of these
Q11 [10+5 Marks]: A C program is given below that asks the user for a meal type using a menu driven
approach. The partial code is given.

Fill in the blanks and write the appropriate answer corresponding to the boxes given below: [10 marks]
#include <stdio.h>
int main() {
int mealType, dish;
printf("Select meal type (1 for Vegetarian, 2 for Non-Vegetarian): ");
scanf(____A____);

switch (mealType) {
case 1: // Vegetarian
printf("Select dish (1 for Paneer, 2 for Dal): ");
scanf(____B____);

____C____ {
case 1:
printf("You selected Paneer.\n");
break;
case 2:
printf("You selected Dal.\n");
break;
____D____:
printf("Invalid dish selection for Vegetarian meal.\n");
}
____E____;

case 2: // Non-Vegetarian
printf("Select dish (1 for Chicken, 2 for Fish): ");
scanf(____F____);

____G____ {
case 1:
printf("You selected Chicken.\n");
break;
case 2:
printf("You selected Fish.\n");
break;
____H____
printf("Invalid dish selection for Non-Vegetarian meal.\n");
}
____I____;

default:
printf("Invalid meal type.\n");
}
____J____;
}

(Continued on next page)

2
Now consider this snippet of the code:
case 1:
printf("You selected Paneer.\n");
break;
Include a query to the user asking him/her, the quantity of dishes and then print the same, the flow of
the program should look like this:
Select meal type (1 for Vegetarian, 2 for Non-Vegetarian): 1
Select dish (1 for Paneer, 2 for Dal): 1
How many plates? 4
You ordered 4 plates of Paneer.

Solution:

A "%d", &mealType F switch(dish)

B "%d", &dish G default: (colon should be


there)

C switch(dish) H break

D default: (colon should be there) I break

E break J return 0

Next part:

case 1:
int num; //you can make the variable name as you want but must be int
printf(“How many plates? ”);
scanf(“%d”, &num);
printf(“You ordered %d plates of Paneer”,num);
break;

Q12 [15 Marks]: You own an ice cream shop and have noticed a pattern in your sales based on the
weather (temperature) and whether it's a holiday. Write a C program to predict how many ice cream
cones you'll sell each day based on this pattern. The C program takes the temperature (in Celsius) and
whether it's a holiday (1 for yes, 0 for no) as input, and predicts the number of ice cream cones you'll
sell. Your program must accurately reflect the sales pattern described below. Assume that 75, 150, and
200 ice cream cones are the average number of ice cream cones sold for "low sales", "medium sales",
and "high sales" respectively.

Sales seem to follow these rules:

Cool Day: When it's cool, you tend to sell fewer ice cream cones. The cooler it is, the fewer you sell. If
the temperature is 20°C or below, it's considered a "cool day." Cool days have a "coolness strength" for
each ice cream cone calculated as (20 - Temperature) / 5 (or 0.1 if the temp is 15°C or below). Cool days
contribute to "low sales."

3
Warm Day: When it's warm and it's a holiday, you sell a lot of ice cream. The warmer it is, the more you
sell. If the temperature is above 20°C, it's considered a "warm day." Warm days have a "warmness
strength" for each ice cream cone calculated as (Temperature - 20) / 10 (or 1 if the temp is 30°C or
above). Warm days contribute to "medium sales" or "high sales" depending on the holiday status.

Holiday Influence: Holidays have a "holiday strength" of 1. Non-holidays have a "holiday strength" of 0.
Sample Input:
Enter the temperature (Celsius): 19.0
Is it a holiday? (1 for yes, 0 for no): 1
Sample Output:
Predicted Ice-cream sales: 15

Explanation: Since, temperature is 19 < 20°C, it is a “cool day”. Then coolness strength = 20.0 – 19.0/5 =
0.2. Cool days contribute to “low sales”. Since it is a a holiday. Holiday strength is 1. But Holiday has no
effect on cool days. Thus the predicted ice-cream sales will be = 75*coolness strength = 75 * 0.2 = 15.

Solution:
#include <stdio.h>
int main() {
int temperature;
int holiday;
//Read Input
printf("Enter the temperature (Celsius): ");
scanf("%d", &temperature);

printf("Is it a holiday? (1 for yes, 0 for no): ");


scanf("%d", &holiday);

float coolness_strength = 0.0;


float warmness_strength = 0.0;
float holiday_strength = (float)holiday;
int sales = 0;
//handle cool temperatures
if (temperature <= 20) {
coolness_strength = (20.0 - temperature) / 5.0;
if (temperature <= 15) {
coolness_strength = 1.0;
}
sales = (int)(75 * coolness_strength); // Low sales
} else { // handle warm temperatures
warmness_strength = (temperature - 20.0) / 10.0;
if (temperature >= 30) {
warmness_strength = 1.0;
}

if (holiday_strength == 1.0) { // It's a holiday


sales = (int)(200 * warmness_strength); // High sales

4
} else { // It's not a holiday
sales = (int)(150 * warmness_strength); // Medium sales
}
}

printf("Predicted ice cream sales: %d\n", sales);

return 0;
}

Q 13 [8+7 Marks] A wood cutter leaves the woods at a given point (A). He must reach a highway, which
follows a straight line, and go back into the woods at another given point (B). How should he do this,
following the shortest path possible? Argue for the correctness of your answer.

Highway

Let X-axis be the highway. Let coordinates of points A and B are given by the user (x1,y1) and (x2,y2)
respectively.

(a) Write a program in C to find the point K on the highway, such that the path A-K-B is the shortest
possible path. Your input to the program is four floating point coordinate x1, y1, x2, and y2 of A
and B respectively and output will be the coordinates of K.
(b) Argue, why your algorithm is correct?

Solution:

(a)

#include <stdio.h>

int main()
{
float x1, y1, x2, y2;

scanf("%f %f", &x1, &y1);


scanf("%f %f", &x2, &y2);

printf(" The point on Highway is (%f,%f):",((y2*(x1-x2))/(y1+y2))


+ x2 ,0.0);

return 0;
}

5
(b)
Step 1: Take the symmetric point A’ of Aabout x axis. So A’ will have coordinates (x1, -y1). Connect the
line A’ to B. The intersection point of A’B with x-axis is the desire point K.

Reason: If we choose any point K’ on the highway, AK’= A’K’. So the length of path AK’ + K’B is same as
A’K’ + K’B. AK’ + K’B is smallest when K’ lies on A’B. Hence the point of intersection of A’B with x-axis is
desired K.

Q14 [15 Marks]: A palindromic number reads the same both ways. The largest palindrome made from
the product of two 2-digit numbers is 9009=91×99.

Write a program in C to find the largest palindrome made from the product of two 3-digit numbers.

Solution:

Q15 [15 Marks] Write a C program that asks the user for an arbitrary integer input n to create an empty
integer array with a length of n*n. Utilize the positional indices of the array cells to display the required
output pattern illustrated on the right. Note: Your program must work for array of any length and the
printed value must be the indices of input array. Do not use 2D-arrays.

6
Solution:
#include <stdio.h>

int main()
{
int n, count=0;
scanf("%d", &n);
int X[n*n];
for(int i = 0; i <= n-1;i++){
X[i] = count;
count++;
}

int flip = 1;

//column(c) number n-1 to 0


for(int c = n-1; c >= 0; c--){
if(flip){
//iterate rows(r) 1 to n-1
for(int r = 1; r <= n-1; r++){
X[n*r + c] = count;
count++;
flip = 0;
}
}
else{
//iterate rows(r) n-1 to 1
for(int r = n-1; r >= 1; r--){
X[n*r + c] = count;
count++;
flip = 1;
}
}
}

for(int i = 0; i < n*n; i++){

7
if((i+1)%n == 0)
printf("%d \n",X[i]);
else
printf("%d ",X[i]);
}

return 0;
}

You might also like