Tutorial 3
Tutorial 3
Tutorial 3
Covers: Lectures 5 and 6
Questions:
1. What is the output of the following the following programs?
a) #include <stdio.h>
int check(int a)
{
a = a + 3;
return a;
}
b) #include <stdio.h>
int a = 2;
void change();
void change()
{
a = a + 3;
}
c) #include <stdio.h>
int a = 2;
void change();
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 1/5
{
printf("%d\n", a);
change();
printf("%d\n", a);
}
void change()
{
int a = 7;
printf("%d\n", a);
a = a + 3;
printf("%d\n", a);
}
d) #include <stdio.h>
void change();
void change()
{
int a = 0;
printf("%d\n", a);
a++;
printf("%d\n", a);
}
e) #include <stdio.h>
void change();
void change()
{
static int a;
printf("%d\n", a);
a++;
printf("%d\n", a);
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 2/5
f) #include <stdio.h>
int a = 5;
void change();
void change()
{
static int a;
printf("%d\n", a);
a++;
printf("%d\n", a);
}
g) #include <stdio.h>
int main(void)
{
unsigned short num1 = 0xFD23;
unsigned short num2 = 0x4567;
displayBits(num1);
displayBits(num2);
displayBits(num1 & num2);
displayBits(num1 | num2);
displayBits(num1 ^ num2);
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 3/5
h) #include <stdio.h>
int main(void)
{
short a = -300;
float b = 8.5;
printf("%hd\n%.5hd\n%05hd\n%-05hd\n%+05hd\n", a, a, a, a, a);
a = +300;
printf("%hd\n%.5hd\n%05hd\n%-05hd\n%+05hd\n", a, a, a, a, a);
printf("%f\n%.4f\n%9.4f\n%09.4f\n%-9.4f\n%+9.4f\n", b, b, b, b, b, b);
b = 854.987654321;
printf("%e\n%.4e\n%9.4e\n%09.4e\n%-9.4e\n%+9.4e\n", b, b, b, b, b, b);
}
2. Write a C program that uses a function to print the multiplication table of a number selected by
the user in the main function. The user should be allowed to enter numbers from the range 1 →
12 only, otherwise an error message should be printed, and the program should end. Make sure
to use suitable prompt messages, and suitable names for identifiers. The output of the program
should be in the following format, assuming that the user entered number 8:
------------------------------
| Multiplication Table for 08 |
------------------------------
| 01 x 08 = 008 |
| 02 x 08 = 016 |
| ………………… ……………… |
| 12 x 08 = 096 |
------------------------------
3. Write a C program that, continuously, receives real numbers from the user. For each number
received, a function is called to print the received number as a whole number and a fraction
rounded to 2 decimal places, e.g., if the number is 13.456 the two parts will be 13 and .46. The
user should be able to end the program by entering a negative number, where in this case, you
should ask the user if he really want to exit or not. Make sure to use suitable prompt messages,
and suitable names for identifiers. The output should be in the following format:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 4/5
should be asked about which bits of the number should be flipped and, using the second function,
the number should be modified and printed again. Make sure to use suitable prompt messages,
and suitable names for identifiers.
Hint: the function displayBits in question 1.g could be used to print the number. Also, a global
variable could be used to allow the original number to be modified, not a copy.
5. Write a C program that receives a short integer number and detects if the number is positive or
negative and also if it is odd or even. The detection should be carried out by inspecting both the
MSB and the LSB of the number, no math functions or condition checks on the whole number are
allowed. Make sure to use suitable prompt messages, and suitable names for identifiers. The
output should be in the following format:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 5/5