Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
W EEK -E ND A SSIGNMENT-02
Operating Systems Workshop (CSE 3541)
Problem Statement:
Experiment with selection structures; if, if-else, if-else if-else and switch statements to develop applications.
Assignment Objectives:
To become familiar with one of the control structures, selection, out of sequence, selection, and repetition
kinds of control structure.
Instruction to Students (If any):
Students are required to write his/her own program by avoiding any kind of copy from any sources.
Additionally, They must be able to realise the outcome of that question in relevant to systems pro-
gramming. You may use additional pages on requirement.
Programming/ Output Based Questions:
1. Find the value that is assigned to x when y is 10.0.
Code snippet: 1(a)
Output
x = 25.0;
if(y != (x - 10.0))
x = x - 10.0;
else
x = x / 2.0;
Code snippet: 1(b)
if(y < 15.0) Output
if(y >= 0.0)
x = 5 * y;
else
x = 2 * y;
else
x = 3 * y;
Code snippet: 1(c) Output
if (y < 15.0 && y >= 0.0)
x = 5 * y;
else
x = 2 * y;
2. Write C statements to carry out the following.
1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Output
If item is nonzero, then multiply product by item
and save the result in product ; otherwise, skip
the multiplication. In either case, print the value
of product. Declare the appropriate type and ini-
tialize, if required.
3. Correct the following if statement; assume the indentation is correct.
Output
if (deduct < balance);
balance = balance - deduct;
printf("New balance is %.2f\n", balance);
else;
printf("Deduction of %.2f refused.\n",
deduct);
printf("Would overdraw account.\n");
printf("Deduction = %.2f Final balance = %.2f",
deduct, balance);
4. Write an interactive program that contains an if statement that may be used to compute the area of a
square ( area = side2 ) or a circle ( area = pi × radius2 ) after prompting the user to type the first
character of the figure name (S or C).
Write/paste your code here t
5. Write a nested if statement for the decision diagrammed in the accompanying flowchart. Use a
multiple-alternative if for intermediate decisions where possible.
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Space for Program and output t
3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
6. Implement the following decision table using a nested if statement. Assume that the grade point
average is within the range 0.0 through 4.0.
Grade Point Average Transcript Message
0.00.99 Failed semesterregistration suspended
1.01.99 On probation for next semester
2.02.99 (no message)
3.03.49 Deans list for semester
3.54.00 Highest honors for semester
Space for Program and output t
4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
7. Implement the following decision table using a multiple-alternative if statement. Assume that the
wind speed is given as an integer.
Wind Speed (mph) Category
below 25 not a strong wind
2538 strong wind
3954 gale
5572 whole gale
above 72 hurricane
Space for Program and output t
5
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
8. What will be printed by this carelessly constructed switch statement if the value of color is ’R’?
switch (color) { / break statements missing /
Output
* *
case ’R’:
printf("red\n");
case ’B’:
printf("blue\n");
case ’Y’:
printf("yellow\n");
}
9. Determine life expectancy of a standard light bulb given the input watts; 35, 45, 76, 120 respectively.
switch (watts) {
case 25:
Output
life = 2500;
break;
case 40:
case 60:
life = 1000;
break;
case 75:
case 100:
life = 750;
break;
default:
life = 0;
}
10. C relational and equality operators give a result of 1 for true and 0 for false. Evaluate the following
expression for different values of x. Also write the statement to avoid such common error.
if (0 <= x <= 4) Test cases and output t
printf("Condition is true\n");
(a) x=5
Common Error Correction (b) x=15
(c) x=34
(d) x=-20
(e) x=-45
11. Evaluate the following code snippet for different values of x.
printf("Enter x \n");
scanf("%d",&x);
if (x = 10)
printf("x is 10");
printf("Differentiate: == and =");
else Test cases and output t
printf(" simply incorrect results");
(a) x=5
Common Error correction in if statement (b) x=15
(c) x=34
(d) x=-20
(e) x=-45
6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
12. Write a switch statement that assigns to the variable lumens the expected brightness of a standard
light bulb whose wattage has been stored in watts. Assign 1 to lumens if the value of watts is not in
the [Link] this table:
Watts Brightness (in Lumens)
15 125
25 215
40 500
60 880
75 1000
100 1675
Space for Program and output t
7
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
13. Keiths Sheet Music needs a program to implement its music teachers discount policy. The program is
to prompt the user to enter the purchase total and to indicate whether the purchaser is a teacher. The
store plans to give each customer a printed receipt, so your program is to create a nicely formatted file
called [Link]. Music teachers receive a 10% discount on their sheet music purchases unless the
purchase total is $100 or higher. In that case, the discount is 12%. The discount calculation occurs
before addition of the 5% sales tax. Here are two sample output filesone for a teacher and one for a
nonteacher.
Total purchases $122.00
Teacher’s discount (12%) 14.64
Discounted total 107.36
Sales tax (5%) 5.37
Total $112.73
Total purchases $24.90
Sales tax (5%) 1.25
Total $26.15
Note: to display a % sign, place two % signs in the format string:
printf("%d%%", SALES_TAX);
To write the output in the file [Link] use output redirection, ./[Link] > [Link]
Space for Program and output t
8
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Space for Program and output t
9
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
14. A particular cell phone plan includes 50 minutes of air time and 50 text messages for $15.00 a month.
Each additional minute of air time costs $0.25, while additional text messages cost $0.15 each. All
cell phone bills include an additional charge of $0.44 to support 911 call centers, and the entire bill
(including the 911 charge) is subject to 5 percent sales tax.
Write a program that reads the number of minutes and text messages used in a month from the user.
Display the base charge, additional minutes charge (if any), additional text message charge (if any),
the 911 fee, tax and total bill amount. Only display the additional minute and text message charges if
the user incurred costs in these categories. Ensure that all of the charges are displayed using 2 decimal
places.
Space for Program and output t
10
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Space for Program and output t
11
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
15. Write a program that determines the day number (1 to 366) in a year for a date that is provided as
input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31,
1996, is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that
any year divisible by 100 is a leap year only if it is divisible by 400. Your program should accept the
month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0
otherwise.
Space for Program and output t
12
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
Space for Program and output t
13
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
16. A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All
three sides of an equilateral triangle have the same length. An isosceles triangle has two sides that are
the same length, and a third side that is a different length. If all of the sides have different lengths then
the triangle is scalene. Write a program that reads the lengths of the three sides of a triangle from the
user. Then display a message that states the triangles type.
Space for Program and output t
14