Problem Sheet 2

This problem sheet was created by Sir Narasimhan T, Assistant Professor, IT Department LBS College of Engineering, Kasaragod

The softcopy in .pdf is available here. There is something much more in the file.

1) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
float a = 0.7;
printf(“%d %d”, sizeof(a),sizeof(0.7));
}
b) main()
{
float expr = 2.0;
switch(expr)
{
case 1.0: printf(“One\n”);
case 2.0: printf(“Two\n”);
default: printf(“Default\n”);
}
}
2) A character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters.
Characters                            ASCII values
A–Z                                          65 – 90
a–z                                           97 – 122
0–9                                           48 – 57
special symbols                   0 – 47, 58 – 64, 91 – 96, 123 – 127
3) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
float a=1.5;
int b = 3;
a = b/2 + b * 8 / b – b + a / 3 ;
printf(“a = %f”, a);
}
b) main()
{
int expr = 2, j = 1;
switch(expr)
{
case j: printf(“This is case1\n”);
case 2: printf(“This is case2\n”);default: printf(“This is default case\n”);
}
}
4) A library levies a fine for every book returned late. For first 10 days the fine is | 0.5 per day, for next 10 days fine is | 1 per day and for further 10 days fine is | 2 per day. If you return a book after 30 days, the membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.
5) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
int x=0;
if(x=0)
printf(“Well done!”)
else
printf(“Sorry!”);
}
b) #define j 1
main()
{
int expr = 2;
switch(expr)
{
case j: printf(“This is case1\n”);
case 2: printf(“This is case2\n”);
default: printf(“This is default case\n”);
}
}
6) A university has the following rules for a student to qualify for a degree with A as the main subject and B as the subsidiary subject:
i. He should get 55% or more in A and 45% or more in B.
ii. If he gets less than 55% in A he should get 55% or more in B. However, he should get at least 45% in A.
iii. If he gets less than 45% in B and 65% or more in A he is allowed to reappear in an
examination in B to qualify.
iv. In all other cases he is declared to have failed.
Write a program to receive marks in A and B and output whether the student has passed, failed or is allowed to reappear in B.
7) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
if(!3.14)
printf (“I have robbed and killed…”);
else
printf (“Until my evil purse was filled”);}
b) main()
{
int planet=1;
switch(planet<<2+planet)
{
default:printf(“Solar system”);
case 6: printf(“Saturn”);
case 3: printf(“Earth”);
case 8: printf(“Neptune”);
}
}
8) Steel is graded according to the following conditions:
i. Hardness must be greater than 50
ii. Carbon content must be less than 0.7
iii. Tensile strength must be greater than 5600
The grades are as follows:
• Grade is 10 if all three conditions are met
• Grade is 9 if conditions (i) and (ii) are met
• Grade is 8 if conditions (ii) and (iii) are met
• Grade is 7 if conditions (i) and (iii) are met
• Grade is 6 if only one condition is met
• Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.

9) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
int x = 3, y = 4, z = 4;
printf(“%d”, (z>=y>=x?100:200));
}
b) main()
{
int i=1,j=3;
switch(i)
{
case 1:
printf(“This is outer case1\n”);
switch(j)
{
case 3:
printf(“This is inner case1\n”);
break;
default:
printf(“Inner case 2\n”);
}
case 6:printf(“This is outer case2\n”);
}
}
10) You are the computer programmer of a big firm which has recently shifted to a new 7-storey building. To help the clients easily locate the rooms, your boss has assigned you with the task of writing a program that will accept the room number as input and output the directions to reach the room. The room numbers are three digit numbers and follow a fixed pattern. The first digit indicates the floor and the last two digits denote the room number on that floor.
For example the room number 207 denotes the 7 th room on the second floor and room number 015 denotes the 15 th room on ground floor (zeroth floor). Further, on a floor, odd numbered rooms are on the left and even numbered rooms on the right and there are 16 rooms on every floor. Figure 1 shows the floor plan of 7 th floor and all the floors follow this plan. Accomplish your task. Two sample inputs and outputs are shown below:
Sample 1
Input
Enter the room number: 408
Output
Please follow the directions below to reach room number 408
Go to floor number 4
Turn right
Go to room number 4 onto your right
Sample 2
Input
Enter the room number: 011
Output
Please follow the directions below to reach room number 011
Turn left
Go to room number 3 onto your right
stair case
708    706    704    702  Stair case   701    703    705    707
710    712    714    716                        715    713    711    709
(Floor plan of 7 th floor for Question 10)

11) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
printf(“%d %d”, 4/-3, 4%-3);
}b)
main()
{
int expr = 2;
switch(expr)
{
case 1:
printf(“This is case1\n”);
break;
case 2:
printf(“This is case2\n”);
continue;
default:
printf(“This is default case\n”);
}
}
12) In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

13) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a) main()
{
int x = 10,y = 100%90;
if(x!=y);
printf(“x = %d y = %d”, x, y);
}
b)
main( )
{
int c = 3 ;
switch(c)
{
case ’v’:
printf(“I am in case v \n”);
break;
case 3:
printf(“I am in case 3 \n”);
break;
case 12:
printf(“I am in case 12 \n” );
break;
default:
printf(“I am in default \n” );
}}
14) A hotel has a pricing policy as follows:
• 2 people: | 450
• 3 people: | 650
• 4 people: | 950
• Additional people: | 150 per person
If the customer is staying on company business, there is a 20% discount. If the customer is
over 60 years of age, there is a 15% discount. A customer does not receive both discounts.
Write a program to input the required data and then to print the cost of the room.

15) Predict the output of the following code snippets. Clearly specify the reasons for your answer.
a)main()
{
int a, b;
a = 5.999999;
b = 5.000001;
printf(“a = %d b = %d”, a, b);
}
b)
main( )
{
int i;
printf(“Enter any number”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“Sa\n”);
case 3:
printf(“Ga\n”);
case 2:
printf(“Re\n” );
case default:
printf(“Ma Pa Da Ni Sa\n” );
}
}
16) The seat numbers of reserved coaches in Indian Railways follow a fixed pattern as follows:
Seat numbers                                            Berth
1, 4, 9, 12, 17, 20 · · · · · ·                            Lower
2, 5, 10, 13, 18, 21 · · · · · ·                          Middle
3, 6, 11, 14, 19, 22 · · · · · ·                          Upper
7, 15, 23, · · · · · ·                                          Side lower
8, 16, 24, · · · · · ·                                          Side upper

Write a program to input the seat number and print the type of berth. Recall that seat number cannot be greater than 72.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s