10.24 Tutorial2
intro-to-computer-science-gtiit-tutorial-02-2024.pdf
Introduction to Computer Science
Tutorial 2: Variables, Arithmetic Operators, printf()
, scanf()
1. Linux Processes
Exercise 1. Using CTRL+z
, &
, and CTRL+c
, do the following: get 3 background processes running in a terminal at the same time and show that they are running with the command jobs
, then terminate all 3 of them. Choose sufficient amounts of time for the commands to last enough (e.g., more than 30 seconds). You should see the output of jobs
showing the three processes as "running."
The command of killing sleep: killall sleep
2. C Programs with scanf()
and printf()
Exercise 2. Write a C program that asks the user for 3 integer values, then prints their average (using the +
and /
operators).
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("The average between %c, %d and %d is %f\n",a,b,c (a + b +c)/3.0);
// or you can: printf("%f\n", ((float)(a + b +c)/3)); which means convert the int to float
// and %c is same effect with %d because a number is also a character.
return 0;
}
Exercise 3. Write a C program that asks the user for 2 integer values, representing the length and width of a rectangle, then prints the perimeter and area of the rectangle.
#include <stdio.h>
int main() {
int length, width;
printf("Please enter the length and width of a rectangle:");
scanf("%d %d", &length, &width);
printf("The area of rectangle is %d\n", length*width);
printf("The perimeter of rectangle is %d\n", (length+width)*2);
return 0;
}
3. C Programs with Conditions
Exercise 4. Write a C program that does the following:
- Declare two
int
variablesa
andb
- Ask the user for two values for these variables
a
andb
- Then, if
a
is equal tob
, print "a and b are equal" - Else, if
a
is strictly greater thanb
, print "a is greater" - Else, print "b is greater"
- Finally, print the absolute value of the difference between
a
andb
Hint: If x
is an arithmetic expression, the expression abs(x)
gives its absolute value.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
if (a==b) {
printf("%d is equal to %d\n",a ,b);}
else if (a<b) {
printf("%d is less than %d\n",a ,b);}
else {
printf("%d is bigger than %d\n",a ,b);}
printf("The difference is: %d\n", abs(a-b));
return 0;
}
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
if (a % 5 == 0) {
printf("%d is divisible by 5\n",a);}
else {
printf("%d is not divisible by 5\n",a);}
return 0;
}
4. Manual Execution
"Manual execution" works as follows. A C program is given (typically without boilerplate like #include <...>
, main(){...}
), and you have to create a table that shows the execution of the program. Each row starts with the statement being evaluated. Each variable has its own column, indicating new values when updated. There is also a "condition" column that indicates TRUE/FALSE if the statement is a condition.
The point of the exercise is not only to correctly perform the assignments and arithmetic operations but also to follow the program's execution with respect to conditional structures (if
, if-else
).
Example:
int a = 1;
if (a < 4)
a = a + 4;
else
a = a - 1;
if (a > 4)
a = a + 1;
else
a = a - 1;
Exercise 5. For each of the following C programs, write the table of manual execution. The table should contain a "statement" column, then a column for every variable in the program, and a "condition" column.
1.1
int a = 35;
int b = 7;
a = a % 10;
b = a + b;
if (a + b > 10) {
a = 0;
} else {
b = 0;
}
a | b | condition | |
---|---|---|---|
int a = 35 |
35 | ||
int b = 7 |
7 | ||
a = a % 10 |
5 | ||
b = a + b |
12 | ||
a + b > 10 |
True | ||
a = 0 |
0 | 12 |
1.2
int i = 2;
if (i > 0) {
i = i + 2;
}
if (i > 3) {
i = i - 3;
}
i | condition | |
---|---|---|
int i = 2 |
2 | |
i > 0 |
True | |
i = i + 2 |
4 | |
i > 3 |
True | |
i = i - 3 |
1 |
1.3
int b = 7;
int c = 0;
if (b > 9) {
c = 1;
} else if (b > 5) {
c = 2;
} else {
c = 3;
}
b | c | condition | |
---|---|---|---|
int b = 7 |
7 | ||
int c = 0 |
0 | ||
b > 9 |
False | ||
b > 5 |
True | ||
c = 2 |
2 |
5. Simplifying Programs with "if-else" Statements
Exercise 6. Simplify the following programs by using else
statements wherever relevant.
Program 1 (badif01.c)
Raw:
main() {
int x;
scanf("%d", &x);
if (x > 0)
printf("x is positive");
if (x < 0)
printf("x is negative");
if (x == 0)
printf("x is zero");
}
Modified:
main() {
int x;
scanf("%d", &x);
if (x > 0)
printf("x is positive");
else if (x < 0)
printf("x is negative");
else
printf("x is zero");
}
Program 2 (badif02.c)
Raw:
main() {
int bill_y, wallet_y, wallet_usd;
int d_to_y = 7;
scanf("%d", &bill_y);
scanf("%d", &wallet_y);
scanf("%d", &wallet_usd);
if (wallet_y >= bill_y)
printf("Pay in yuans.");
if (wallet_y < bill_y) {
if (wallet_usd * d_to_y >= bill_y)
printf("Pay in dollars.");
if (wallet_usd * d_to_y >= bill_y)
printf("Cannot pay.");
}
}
Modified:
main() {
int bill_y, wallet_y, wallet_usd;
int d_to_y = 7;
scanf("%d, %d, %d", &bill_y, &wallet_y, &wallet_usd);
if (wallet_y >= bill_y)
printf("Pay in yuans.");
else (wallet_y < bill_y) {
if (wallet_usd * d_to_y >= bill_y)
printf("Pay in dollars.");
else (wallet_usd * d_to_y >= bill_y)
printf("Cannot pay.");
}
}