You are on page 1of 2

INDIAN INSTITUTE OF SCIENCE EDUCATION AND RESEARCH (IISER), PUNE

(An Autonomous Institution, Ministry of Human Resource Development, Govt. of India)


Dr. Homi Bhabha Road, Pune 411 008

FALL 2016
Tutorial 2

Course name: Introduction to Computing


Course Code: IDC101

1. Variables and arithmetic expressions.


Code the program to convert between Farenheit and Celsius temperatures, as discussed in class:
#include <stdio.h>
/* print Farenheit-Celsius table
for fahr = 0, 20, 40, ..., 300*/
main()
{
int fahr, celsius;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;

/* lower limit of temperature table */


/* upper limit */
/* step size */

fahr = lower;
while (fahr <= upper) {
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}

(a) Replace the statement


celsius = 5 * (fahr-32) / 9;

with
celsius = (fahr-32) / 9 * 5;

Do you get the same result, as one might naively expect? Explore what exactly is different.
(b) Next, try to enclose the division within brackets:
celsius = (fahr-32) * (5 / 9);

Now do you get what we need? Recall that in C, as in many other languages, integer division
truncates: any fractional part is discarded. Consider the division without the brackets:
1

celsius = (fahr-32) * 5 / 9;

How do you explain this output then; what changed?


(c) Operator precedence determines the order in which operators are evaluated. For example,
brackets have higher precedence than multiplication and division, which in turn have a higher
precedence that addition and subtraction. In our case, this means brackets will be evaluated
first, multiplication and division next, and finally addition and subtraction.
Associativity. In addition to precedence, you will also need to use the following property.
Notice that multiplication and division have equal precedence. The associativity of an operator is
a property that determines how operators of the same precedence are grouped in the absence of
parentheses. Consider the expression a b c. If the operator has left associativity, this
expression would be interpreted as (a b) c. If the operator has right associativity, the
expression would be interpreted as a (b c). Addition, subtraction, multiplication and division
operators are usually left-associative. Find out if this is true in C (write code if necessary)?
(d) Finally, explain all of the above results in light of these properties.
(e) What about fractions? C has a float data type to allow for decimals. To study the results of
using floating-point arithmetic, define fahr and celsius to be of type float and check all of the
versions of the program studied above. You will need to use %f instead of %d in the printf
statement.
2. The for loop statement. There is an alternative way of writing a loop: Instead of the while
statement, try to use a for statement. The syntax is as follows:
for (init; condition; increment) {
statement(s);
}

that is, use the code: for (fahr = 0; fahr <= 300; fahr = fahr + 20) to rewrite the program.
3. Write a program to print the Farenheit-Celsius table in the reverse order, that is, from 300 degrees to
0.
4. Modify the program to print a heading above the table.
5. Write a program to print the corresponding Celsius to Farenheit program.

You might also like