Tutorial Sheet 3
ESC111 – Fundamentals of Computing
Revision Topics
1. If-else syntax
• if statement
– Syntax:
if (condition) {
// code if condition is true
}
– Executes the block if condition is true.
• if-else statement
– Syntax:
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
– Executes one block if condition is true, otherwise executes the
other.
• if-else if-else ladder
– Syntax:
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition1 is false and condition2 is true
} else {
// code if all conditions are false
}
– Tests multiple conditions sequentially.
• Nested if-else statement
1
– Syntax:
if (condition1) {
if (condition2) {
// code if both are true
} else {
// code if condition1 is true and condition2 is false
}
} else {
// code if condition1 is false
}
– An if-else statement inside another if-else.
2. Switch syntax
• Syntax:
switch (expression) {
case constant1:
// code for case 1
break;
case constant2:
// code for case 2
break;
...
default:
// code if no case matches
}
• expression is evaluated and compared with each case.
• If a match is found, the corresponding block is executed.
• break; exits the switch after executing a block.
• default is optional and executes if no case matches.
1
1 **For the tutors: Please cover the conversion of base 2 to decimal and vice-versa in the
class.
2
3. 1’s Complement Representation
• Definition:
– One’s complement is a binary number representation used for
signed numbers.
– Positive numbers are represented as their binary equivalents.
– Negative numbers are represented by inverting all bits of the
positive number.
• Example:
– For a 4-bit system:
+5 = 0101 (in binary)
-5 = 1010 (in binary, after inverting all bits)
• Characteristics:
– There are two representations of zero: 0000 (positive zero) and
1111 (negative zero).
– Arithmetic operations require an end-around carry.
4. 2’s Complement Representation
• Definition:
– Two’s complement is a method for representing signed integers
in binary form.
– Positive numbers are represented as their binary equivalents.
– Negative numbers are represented by inverting all bits of the
positive number and adding 1.
• Example:
– For a 4-bit system:
+5 = 0101 (in binary)
-5 = 1011 (in binary, after inverting all bits and adding 1)
• Characteristics:
– Only one representation of zero: 0000.
– Simplifies arithmetic operations, as subtraction can be performed
as addition of the two’s complement.
– The most significant bit (MSB) indicates the sign (0 for positive,
1 for negative).
5. Assignment Operator
• Chained Assignment:
– Multiple variables can be assigned the same value using a single
statement:
a = b = c = 5;
3
6. Conditional Operator
• Definition:
– The conditional operator in C is a shorthand for an if-else
statement.
– It is represented by the symbols ‘? :‘.
– Syntax:
condition? expression1 : expression2;
• Explanation:
– condition is evaluated.
– If condition is true (non-zero), expression1 is executed.
– If condition is false (zero), expression2 is executed.
• Associativity:
– The conditional operator is right-associative.
– This means in an expression like:
condition1 ? expression1 : condition2 ? expression2 : expression3;
– It is evaluated as:
condition1 ? expression1 : (condition2 ? expression2 : expression3);
7. Floating Point Representation
• Format:
– 1 bit for sign
– 8 bits for exponent
– 23 bits for mantissa (fractional part)
• The exponent is stored with a bias of 127.
• Characteristics:
– Floating-point representation has limited precision and can in-
troduce rounding errors.
– Special values include zero, infinity, and NaN (Not a Number).
– Precision and range are constrained by the 32-bit format.
8. Short Circuiting
• Short-circuiting is a behavior in C where the evaluation of a logical
expression stops as soon as the result is determined.
• This behavior applies to the logical AND (&&) and logical OR (||)
operators.
4
Sample Questions for Discussion
1. Decode It!!
You are given a number in its 1’s complement representation(Assume 4-bit
system), you have to output the value of the number in decimal notation.
Example Input1:
1010
Example Output1:
-5
Example Code:
#include <stdio.h>
int main() {
char a,b,c,d;
scanf("%c%c%c%c",&a,&b,&c,&d);
int val = 0;
if(a==’0’){
val = 4*(b-’0’) + 2*(c-’0’) + (d-’0’);
} // Positive Numbers
else{
val = 4*(!(b-’0’)) + 2*(!(c-’0’)) + !(d-’0’);
val = val*(-1);
} // Negative Numbers
printf("%d",val);
return 0;
}
5
2. Arithmetic Operation Evaluation
Given an arithmetic operation with two operands and an operator, eval-
uate the arithmetic operation and output the result. The operator can
be addition (+), subtraction (−), multiplication (∗), division (/), or mod-
ulo (%). (Given operation will be valid, operands and operator are space
separated)
Example Input1: 6 + 7
Example Output1: 13
Example Input2: 9 % 5
Example Output2: 4
Example Code:
#include <stdio.h>
int main() {
int operand1, operand2, result;
char operator;
scanf("%d %c %d", &operand1, &operator, &operand2);
switch (operator) {
case ’+’:
result = operand1 + operand2;
break;
case ’-’:
result = operand1 - operand2;
break;
case ’*’:
result = operand1 * operand2;
break;
case ’/’:
result = operand1 / operand2;
break;
case ’%’:
result = operand1 % operand2;
break;
default:
}
printf("Result: %d", result);
return 0;
}
6
3. Character Categories
Categories of characters are defined as : 1. Capital Alphabets 2. Small
Alphabets 3. Numbers 4. Special characters(All other character than the
first 3 categories)
You are given 2 space separated characters as input, if they belong to the
same category output YES, else NO.
Example Input: A v
Example Output: NO
Example Input: % &
Example Output: YES
Example Code:
#include <stdio.h>
int main() {
char char1, char2;
scanf("%c %c", &char1, &char2);
int category1, category2;
if (char1 >= ’A’ && char1 <= ’Z’) {
category1 = 1; // Capital Alphabets
} else if (char1 >= ’a’ && char1 <= ’z’) {
category1 = 2; // Small Alphabets
} else if (char1 >= ’0’ && char1 <= ’9’) {
category1 = 3; // Numbers
} else {
category1 = 4; // Special Characters
}
if (char2 >= ’A’ && char2 <= ’Z’) {
category2 = 1; // Capital Alphabets
} else if (char2 >= ’a’ && char2 <= ’z’) {
category2 = 2; // Small Alphabets
} else if (char2 >= ’0’ && char2 <= ’9’) {
category2 = 3; // Numbers
} else {
category2 = 4; // Special Characters
}
if (category1 == category2) {
printf("YES");
} else {
printf("NO");
}
return 0;
}
7
Practice Problems
1. Coordinates (x, y) of the center of a circle and its radius (say r) are given
as input. Another point, say (x1, y1), is provided as input. Write a
program to find out whether the point is inside the circle, on the circle,
or outside the circle. Assume x, y, r, x1, y1 are of float data type. Print
Yes if the point is inside or on the circle and print No otherwise.
2. There are four friends Chitwan, Goutam, Monil, and Talin standing at
different points on a 2-D plane. Chitwan is standing at (xA, yA), Goutam
at (xB, yB), Monil at (xC, yC), and Talin at (xD, yD). The strength
of the friendship between any two friends is inversely proportional to the
distance between the points where they are standing. Find the two friends
whose friendship is the strongest. It is guaranteed that the pair will be
unique.
The distance between two points (x1, y1) and (x2, y2) is calculated as
|x2 − x1| + |y2 − y1|.
Input: Eight integers in the order xA, yA, xB, yB, xC, yC, xD, and yD.
Output: Print in alphabetical order the names of the two friends whose
friendship is the strongest. Print the two names in a single line, separated
by a single space.
3. You are provided with 4 positive integers: three numbers are the same
while one is different. You need to find the number that is not equal to
the others. Furthermore, you also need to check whether the sum of the
three equal numbers (denoted by S) is divisible by 6 and 9.
8
Common Pitfalls and Recognizing Compiler Er-
ror Messages
1. Dangling else
• An else statement without a corresponding if will throw an error.
2. Use of else if
• For multiple conditions ( 2), if using the else if ladder, remember to
use ’else if’ properly
• If a series of ’if’ statements are written with an else at the end, the
’else’ will match only the last ’if’
3. Brackets with If condition
• Always put brackets appropriately whenever dealing with ’if’ and
’else if’ statements
• For multiline if conditions close the curly braces properly
4. Careful about operators
• Use of ’=’ vs ’==’
• Use of ’&’ vs ’&&’
5. Comparison between compatible types
• Be cautious about comparing different types, such as comparing an
int with a float. Implicit conversions might lead to unexpected re-
sults.
6. Use break in case statements
• Not using break may lead to fall-through and hence, unexpected
behaviour
7. Better to use default statements
• It’s good practice to always include a default case, even if it’s just to
handle unexpected or error cases.
8. Using Non-constant Expressions
• ’case’ labels must be constant expressions. Using variables or non-
constant values as case labels will cause compilation errors.