Tutorial 1 Solution
Tutorial 1 Solution
Tutorial 1
Covers: Lectures 1 and 2
Questions:
1. Identify the valid and invalid variable names from the following:
- Solution:
3Var -> Invalid -> Begins with a number.
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 1/9
2. Which of the following variable declarations are correct?
➢ 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
(4) -> Incorrect Declaration -> separated using semicolon ‘;’ (end of line), not comma
(5) -> Incorrect Declaration -> separated using semicolon ‘;’ (end of line), not comma
© 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:
© 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:
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);
}
© 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);
}
➢ 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:
- 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);
}
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);
}
© 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:
- 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);
}
© 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;
return 0;
}
- Solution:
(1) -> 205 -> multiplication then addition
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 8/9
- Solution - continue:
(11) -> 18, 18 -> as “printf” function calculates its parameters from right to left within GNU-C
Compiler (compiler dependent action)
Program Output:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 9/9