Nothing Special   »   [go: up one dir, main page]

Computer Science Practice Assignement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

German University in Cairo

Faculty of Media Engineering and Technology


Prof. Dr. Slim Abdennadher
Dr. Nada Sharaf
Dr. Mohammed Abd El Megeed

Introduction to Computer Science, Winter Semester 2020


Class Practice Assignment 3
Discussion: 07.11.2020 - 12.11.2020

Exercise 3-1 Swaping Numbers


Write an algorithm that takes as input two numbers and swaps the values of these numbers.

• Write the algorithm using a temporary variable


• Swap the two numbers without using a temporary variable. Is it always possible to swap any two
values of any types?

Exercise 3-2 Dices


To be solved in Tutorial
Write an algorithm that generates two random numbers between 1 and 100 exclusive. if the sum
of the two random numbers’ digits are even, the program should display: You win , and displays
You lose! try again otherwise.

Exercise 3-3 The Sum of Rounded Numbers


To be discussed in Tutorial
Write an algorithm that given three integers displays the sum of their rounded values. We will round
an integer value to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20.
Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 122 rounds
down to 120. Here are some examples of evaluating the algorithm on representative input values:

16 17 12 -> 50
12 13 14 -> 30
6 4 4 -> 10

Exercise 3-4
To be discussed in Tutorial
Your task is to implement an algorithm that can calculate your maximum heart rate and your optimal
training pulse for fat-burning, endurance increase or cardiovascular system improvement. The Formula
for calculating the maximum heart rate (Pulse) depends on the age and the gender:

• For men: 220 - Age


• For women: 226 - Age

The training can be classified into the following zones:

• Health zone: This amounts to 50-60% of the maximum heart rate. Within this pulse range
particularly the cardiovascular system will be invigorated. This range is particularly suitable for
beginners.

1
• Fat burning zone: This amounts to 60-70% of the maximum heart rate. Within this pulse range,
most calories from fat are burned. Furthermore the cardiovascular system will be trained.
• Aerobic zone: This amounts to 70-80% of the maximum heart rate. Within this pulse range,
carbohydrates and fats are burned for power production in the muscle cells. This range requires
the cardiovascular system as well as the lung and the metabolism.
• Anaerobic zone: This amounts to 80-90% of the maximum heart rate. Within this pulse range,
the body cannot cover the oxygen demand any longer. This range is for the development of power
and muscle mass.
• Red zone: This amounts to 90-100% of the maximum heart rate. This pulse range should be
handled with caution. It is dangerous for beginners and can be harmful for the heart.

a) Write an algorithmthat given the age and the gender should display the different zones. For example,
your algorithm should display the following for a 45 years old man:

Health zone: Between 88 and 105


Fat-burning zone: to 122
Aerobic zone: to 140
Anaerobic zone: to 158
Maximum heart rate / Red zone: to 175

b) Write an algorithm that given the age, the gender and the heart rate will display the corresponding
zone. For example, the algorithm should display for a 45 years old man and heart rate of 190, the
following message

Red Zone

Your algorithm should consist of only if then statements, i.e. you are not allowed to use any else
statements.

Exercise 3-5 Discount


To be solved in Lab
A discount is made on a purchase as follows:

• if purchase ≤ 1000 L.E., there is no discount


• if purchase > 1000 L.E., the discount is 5%

Given the cost of the purchase, write an algorithm to calculate and print the money paid taking into
consideration the 10% sales taxes. The taxes are calculated on the amount after the discount.

Exercise 3-6 Dog age


To be solved in Lab
For the first two years, a dog year is equal to 10.5 human years. After that, each dog year is equal to 4
human years. Write a Python program to calculate a dogâs age in dog years, given a dogâs age in human
years. Expected output should look like the following:

• Input a dog’s age in human years: 2 The dog’s age in dog years is 21
• Input a dog’s age in human years: 15 The dog’s age in dog years is 73
• Input a dog’s age in human years: -5 Age must be positive number.

Exercise 3-7 Gas Station


National Last Chance gas station sits on Cairo Alexandria route. There is no other gas station for 200
Miles. Write an algorithm to help drivers decide if they need gas or not. The user will enter:

2
• The capacity of the gas tank, in Gallons.
• The indication of the gas gauge in percent (full=100, three quarters=75 and so on).
• The miles per gallon of the car.

The following is a sample run of the algorithm:

Tank Capacity: 12
Gas Gauge Reading in percent: 50
Miles per Gallon: 30

Get Gas!

The algorithm should print out Get gas or Safe to proceed depending on if the car can cross the 200
miles with the gas remaining in the tank.

Exercise 3-8 BMI


Write an algorithm that determines whether you are underweight, fit, or overweight given your weight
and height based on your BMI calculation.
The BMI is calculated using the weight divided by height squared, where weight is in kg and height is in
meters.

• If BMI ≤ 18.5, you are underweight


• If BMI > 18.5 and BMI ≤ 25, you are fit
• If BMI > 25, you are overweight

Exercise 3-9 Triangle


To be solved in Lab
Write an algorithm that determines whether A, B, and C can form the sides of a triangle. Note A, B
and C can form the sides of a triangle if each side is less than the sum of the two other sides, i.e.:
A < B + C, B < A + C and C < A + B .
If A, B, and C forms a triangle, calculate its area using the formula:
p
Area = S(S − A)(S − B)(S − C) , where S = (A + B + C)/2

Exercise 3-10 Letter Grades


Students marks in a class are graded on the following policy:

• A: 85-100
• B: 74-85

• C: 60-74
• D: 50-60
• F: <50

Keeping in mind that a student cannot score more than 105 marks, nor less than 0 marks.
Write an algorithm that reads each student’s marks, print either a grade or an error message.

3
Exercise 3-11 Student School
To be solved in Tutorial
The following algorithm prints out whether a current student is in elementary (1st - 5th), middle (6th -
8th), or high school (9th - 12th).

grade = eval(input())
if(grade <= 5):
print("this student is in elementary school")
elif(grade <= 8):
print("this student is in middle school")
elif(grade <= 12):
print("this student is in high school")

The algorithm above uses nested if-statements.

a) Write an equivalent algorithm that will print the same messages as the algorithm above without
using any nested if-statements.
b) Discuss the drawback of your algorithm? Hint: Compare the efficiency of both algorithms.

You might also like