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

Tutorial 1 Solution

Uploaded by

mwkaa17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Tutorial 1 Solution

Uploaded by

mwkaa17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Zagazig University Fall 2024

Faculty of Engineering First Year


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

Tutorial 1
Covers: Lectures 1 and 2

Questions:
1. Identify the valid and invalid variable names from the following:

3Var Var_4 Double_num Count$


SizeOf INT Dist to sun Invalid_name

➢ Variable Naming Rules:


o Must be a valid identifier.
o Does not begin with a digit.
o Special characters are not allowed.
o Spaces are not allowed.
o Reserved keywords are not allowed.

- Solution:
3Var -> Invalid -> Begins with a number.

Var_4 -> Valid

Double_num -> Valid

Count$ -> Compiler Dependent:


o Invalid in most compilers as ‘$’ is a special character.
o Valid within GNU-C compiler that we use.

SizeOf -> Valid

INT -> Valid

Dist to sun -> Invalid -> contains spaces

Invalid_name -> Valid

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 1/9
2. Which of the following variable declarations are correct?

int var1; (1)


int var2;
int var2, var2; (2)
int var1=4, var2=8; (3)
int var1; var2; (4)
int var1=9; var2=3; (5)
int var1; int var2; (6)

➢ Variables Declaration:
o Declaring a variable:
<Data Type> <Variable Name>;
o Declaring a variable with initialization:
<Data Type> <Variable Name> = <Initial Value>;
o Declaring Multiple variables of the same type:
<Data Type> <Var1 Name>, <Var2 Name>, <Var3 Name>, … ;
Note that you can initialize any or all of these variables if needed in the same line:
<Data Type> <Var1 Name> = <Initial Value>, <Var2 Name>, … ;
➢ Note that variable names mustn’t be repeated within the same scope.

- Solution:
(1) -> Correct Declaration

(2) -> Incorrect Declaration -> repeated names

(3) -> Correct Declaration

(4) -> Incorrect Declaration -> separated using semicolon ‘;’ (end of line), not comma

(5) -> Incorrect Declaration -> separated using semicolon ‘;’ (end of line), not comma

(6) -> Correct Declaration

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 2/9
3. The following C Code contains an error and will not compile. Identify the line that contains the
error.

1 #include <stdio.h>
2
3 int main()
4 {
5 int sum;
6 Sum = 3 + 8;
7 printf("Sum =%d\n", sum);
8 return 0;
9 }

- Solution:
The error is in the line (6) as the variable used within this line “Sum” differs from the declared
one “sum”, as C programming language is a Case-Sensitive language, and to correct that error
the variable in line (6) should be changed to “sum” to be the same as the declared one.

4. In a certain machine, data types are stored in memory using the shown number of bytes.
Complete the table to indicate the range of allowed values.
Data type Number of Bytes Range
int 4 -2,147,483,648 → 2,147,483,647
unsigned int 4 0 → 4,294,967,295
char 1 -128 → 127
unsigned char 1 0 → 255

➢ The Range of values that can be held within Integer Types variables can be
calculated as follows:

o The Range for Unsigned ones:


𝟎 → 𝟐 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐁𝐢𝐭𝐬 − 𝟏
o The Range for Signed ones:
−𝟐 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐁𝐢𝐭𝐬 − 𝟏 → 𝟐 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐁𝐢𝐭𝐬 − 𝟏 − 𝟏
Where: 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐛𝐢𝐭𝐬 = 𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐁𝐲𝐭𝐞𝐬 ∗ 𝟖

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 3/9
5. What is the output of the following C program?

#include <stdio.h>

int main()
{
char ch1, ch2;
ch1 = 67;
ch2 = 'D';
printf("ch1 =%d --> %c\n", ch1, ch1);
printf("ch2 =%d --> %c\n", ch2, ch2);
return 0;
}

- Solution:
Since that the format specifiers used within the “printf” function in this example are:
o “%d” -> that is used for printing integer values.
o “%d” -> that is used for printing characters using their values within the ASCII table.
Thus, the output of this program will be as follows:

➢ Try to memorize these values within the ASCII table:


o ‘0’ -> 48
o ‘A’ -> 65
o ‘a’ -> 96

6. Write a C program that asks the user for his first name, and then prints a message to greet him.
For example, if the first name is “Hasan”, the output should be “Greetings, Hasan!!”
- Solution:
#include <stdio.h>

void main()
{
char first_name[15] = {0};
printf("\n Please enter your First Name: ");
scanf("%s", first_name);
printf(" Greetings, %s!! \n", first_name);
}

Program Output Example:

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 4/9
7. Write a C program that receives three real values from the user, then calculates their average
and prints it on the screen. Make sure to print suitable prompt messages to the user. The
output should be printed using the following format:

The average of …, …, … is …

- Solution:
#include <stdio.h>

void main()
{
float val_1 = 0.0, val_2 = 0.0, val_3 = 0.0, Avg = 0.0;
/* float val_1 = 0, val_2 = 0, val_3 = 0, Avg = 0; // won’t affect the result */
printf("\n Please enter Three Real Values: ");
printf("\n The 1st Value: ");
scanf("%f", &val_1);
printf(" The 2nd Value: ");
scanf("%f", &val_2);
printf(" The 3rd Value: ");
scanf("%f", &val_3);
Avg = (val_1 + val_2 + val_3)/ 3.0;
/* Avg = (val_1 + val_2 + val_3)/ 3; // won’t affect the result as the variables are float already so no casting
or floating point needed */
printf("\n The average value of %f , %f , %f is %f \n", val_1, val_2, val_3, Avg);
}

Program Output Example:

➢ Note that Real Values contains floating point numbers, thus we declare the variables
used as float.

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 5/9
8. Write a C program that receives a positive floating-point number from the user, then displays
the previous, and next integers. Make sure to print suitable prompt messages to the user. The
output should be printed using the following format:

The previous integer of … is …

The next integer of … is …

- Solution:
#include <stdio.h>

void main()
{
float Num = 0.0;
printf("\n Please enter a positive floating-point number: ");
scanf("%f", &Num);
printf("\n The previous integer of %f is %d", Num, (int)Num);
printf("\n The next integer of %f is %d", Num, (int)Num + 1);
}

- Another Solution (using math library):


#include <stdio.h>
#include <math.h>

void main()
{
float Num = 0.0;
int prev = 0, next = 0;
printf("\n Please enter a positive floating-point number: ");
scanf("%f", &Num);
prev = floor(Num);
next = ceil(Num);
printf("\n The previous integer of %f is %d", Num, prev);
printf("\n The next integer of %f is %d", Num, next);
}

Program Output Example:

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 6/9
9. Write a C program that receives the radius of a circle from the user, then calculates the
circumference and area of the circle and prints them on the screen. Make sure to print suitable
prompt messages to the user. The output should be printed using the following format:

The circle with a diameter of … can be described as:

“Area” = … \/\/\/\/ “Circumference” = … \/\/\/\/

- Solution:

#include <stdio.h>

void main()
{
float Radius = 0.0, Circumference = 0.0, Area = 0.0;
printf("\n Please enter the Radius: ");
scanf("%f", & Radius);
Circumference = 2 * (22.0/7) * Radius;
Area = (22.0/7) * Radius * Radius;
/* since the division is done between brackets casting is required
otherwise “(22/7) = 3” not as “(22.0/7) = 3.142857” */
printf("\n The circle with a diameter of %f can be described as: ", 2 * Radius);
printf("\n \"Area\" = %f \\/\\/\\/\\/ ", Area);
printf("\"Circumference\" = %f \\/\\/\\/\\/\n",Circumference);
}

Program Output Example:

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 7/9
10. What is the output of the following C program?

#include <stdio.h>

int main()
{
int a1 = 5, a2 = 10, a3 = 20;
float b1 = 5.0, b2 = 10.0, b3 = 20.0;

printf("\n%d", a1 + a2 * a3); // -> (1)


printf("\n%d", (a1 + a2) * a3); // -> (2)
printf("\n%d", (a1 + a2) / a3); // -> (3)
printf("\n%d", (a1 + a2 / a3)); // -> (4)
printf("\n%d", a3 % (a1 + 1)); // -> (5)
printf("\n%f", b1 + b2 * b3); // -> (6)
printf("\n%f", (b1 + b2) * b3); // -> (7)
printf("\n%f", (b1 + b2) / b3); // -> (8)
printf("\n%f", (b1 + b2 / b3)); // -> (9)

int a4 = a1++ + a2, a5 = ++a1 + a2;


/* a4 = 5 + 10 = 15, a1 = 6 | a5 = (6 + 1) + 10 = 17, a1 = 7 */
printf("\n%d, %d, %d, %d", a1, a2, a4, a5); // -> (10)
printf("\n%d, %d", a1++ + a2, ++a1 + a2); // -> (11)
/* walk through the parameters from right to left:
++a1 + a2 -> (7 + 1) + 10 = 18, a1 = 8
a1++ + a2 -> 8 + 10 = 18, a1 = 9 */

b1 += b2; /* b1 = b1 + b2 = 5.0 + 10.0 = 15.0 */


b2 *= b3; /* b2 = b2 * b3 = 10.0 * 20.0 = 200.0 */

printf("\n%f, %f, %f", b1, b2, b3); // -> (12)

return 0;
}

- Solution:
(1) -> 205 -> multiplication then addition

(2) -> 300 -> addition then multiplication

(3) -> 0 -> addition then integers division

(4) -> 5 -> integers division then addition

(5) -> 2 -> addition then reminder

(6) -> 205.000000 -> multiplication then addition

© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 8/9
- Solution - continue:

(7) -> 300.000000 -> addition then multiplication

(8) -> 0.750000 -> addition then normal division

(9) -> 5.500000 -> normal division then addition

(10) -> 7, 10, 15, 17

(11) -> 18, 18 -> as “printf” function calculates its parameters from right to left within GNU-C
Compiler (compiler dependent action)

(12) -> 15.000000, 200.000000, 20.000000

Program Output:

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy