0% found this document useful (0 votes)
4 views

Tutorial 3

Uploaded by

mwkaa17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Tutorial 3

Uploaded by

mwkaa17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Zagazig University Fall 2024

Faculty of Engineering First Year


Computer and Systems Dept. CSE121: Computer Programming (2)

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);

int main (void)


{
int x = 1;
printf("%d\n", check(x));
printf("%d\n", x);
}

int check(int a)
{
a = a + 3;
return a;
}

b) #include <stdio.h>

int a = 2;
void change();

int main (void)


{
printf("%d\n", a);
change();
printf("%d\n", a);
}

void change()
{
a = a + 3;
}

c) #include <stdio.h>

int a = 2;
void change();

int main (void)

© 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();

int main (void)


{
change();
change();
change();
}

void change()
{
int a = 0;
printf("%d\n", a);
a++;
printf("%d\n", a);
}

e) #include <stdio.h>

void change();

int main (void)


{
change();
change();
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();

int main (void)


{
printf("%d\n", a);
change();
change();
change();
printf("%d\n", a);
}

void change()
{
static int a;
printf("%d\n", a);
a++;
printf("%d\n", a);
}

g) #include <stdio.h>

void displayBits(unsigned short value);

int main(void)
{
unsigned short num1 = 0xFD23;
unsigned short num2 = 0x4567;

displayBits(num1);
displayBits(num2);
displayBits(num1 & num2);
displayBits(num1 | num2);
displayBits(num1 ^ num2);
}

void displayBits(unsigned short value)


{
unsigned short displayMask = 1 << 15;
printf("%10u = ", value);
for (unsigned short c = 1; c <= 16; ++c)
{
putchar(value & displayMask ? '1' : '0');
value <<= 1;
if (c % 8 == 0)
putchar(' ');
}
putchar('\n');
}

© 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);

for (short i = 0; i < 8; i++)


{
printf("-");
}
printf("\n");

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:

The number entered has an integer value of …… and a fraction of ……


Hint: use letters ‘y’ and ‘n’ to receive user response about exiting the program.
4. Write a C program that receives a short integer number from the user and uses a function to
display it as binary number and another function to modify the bits of the number. The user

© 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:

The number …… is odd/even and negative/positive

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 5/5

You might also like