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

Assignment 2

This document outlines the practice assignment for an Introduction to Computer Science course at the German University in Cairo for the Winter Semester 2023-2024. It includes various exercises focused on programming concepts such as error handling, algorithms for calculating air conditioning capacity, time conversion, and financial calculations. The document serves as a guide for students to complete their assignments and prepare for discussions in tutorials and labs.

Uploaded by

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

Assignment 2

This document outlines the practice assignment for an Introduction to Computer Science course at the German University in Cairo for the Winter Semester 2023-2024. It includes various exercises focused on programming concepts such as error handling, algorithms for calculating air conditioning capacity, time conversion, and financial calculations. The document serves as a guide for students to complete their assignments and prepare for discussions in tutorials and labs.

Uploaded by

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

German University in Cairo

Media Engineering and Technology Faculty


Prof. Dr. Slim Abdennadher
Assoc. Prof. Milad Ghantous
Dr. Nourhan Ehab

Introduction to Computer Science, Winter Semester 2023-2024


Practice Assignment 2
Discussion: 21.10.2023 - 26.10.2023

To be discussed in Tutorials:
Exercise 2-1 Errors
Beside each code fragment in the table below, write what is printed when the code fragment is
executed. If the code would cause an error, write ERROR and give a brief explanation.

Code Output or Cause of Error


x = 22
y = 33
x = y
y = x
print(x,y)
s = "two"
v = int(s)
print(v)
x = "1" + 2
print(x)
x = float(10//4)
print(x)
a = 1/2
b = 1.0/2
c = 1.0//2
print(a, b, c)

Exercise 2-2 Air conditioning


You shop for air conditioners. The capacity of the models you find is given in cubic meters per
m3
minute i. e. they can cool 1m3 every one minute, min . You know the height, length, and width
of your room. Further, you want to make sure, that your room gets cool within no more than 10
minutes. Write an algorithm that takes the height, length and width as input from the user and
m3
calculates the minimum capacity of an air conditioner i.e., min . for your room.

Exercise 2-3 Get the Time


Write an algorithm that reads the amount of time in seconds and then displays the equivalent
hours, minutes and remaining seconds.

• One hour corresponds to 60 minutes.


• One minute corresponds to 60 seconds.
Exercise 2-4 Reverse Digits
Write an algorithm that given a 3-digit integer number, converts it out to an integer number in
reversed order.
Example: if the integer number is 425 then the output should be 524.

Exercise 2-5 Planting Trees


Write an algorithm that given the width and length of a garden in meters calculates the area of
the garden together with the number of trees that could be planted on this garden area, knowing
that each tree needs a space of 50 cm2 .
Note: You can use the function int(x/y) that gives the integer part of the result of the division
x/y. For example int(5/2)=2. You can also use the integer division operator " // " directly.
To be solved in Labs:

Exercise 2-6 Cook in a hurry


You want to cook some pasta, but you are short of time. To be sure you can finish cooking before
your next appointment, you need to know how long it takes for the water to boil. At its highest
setting, your stove needs two minutes per liter to reach the boiling point. You use a cylindrical pot.
Write a program that, given the diameter of the pot and the height of the water in it, calculates
the time needed for the water to boil.
volume of cylinder = πr2 h
1 cubic meter = 1000 liter

Example:

diameter = 13
height = 50
output: Your stove needs: 13.2665 minutes to bring the water to boil.

Exercise 2-7 Pythagorean Theorem


The Pythagorean Theorem states that the sum of the squares of the two sides of a right angle
triangle is equal to the square of its hypotenuse. For example, 3, 4 and 5 are the sides of a right
angle triangle as they form a Pythagorean Triple (52 = 42 + 32 ). Given 2 numbers, m and n where
m ≥ n, a Pythagorean Triple can be generated by the following formulae:

a = m2 − n2
b = 2×m×n
p
c = a 2 + b2

Write an algorithm that reads in values for m and n and prints the values of the Pythagorean
Triple generated by the formulae above.

Exercise 2-8 Compound Interest


Write an algorithm that will output the account balance each year for 3 years given the initial
balance and interest rate.
The interest is calculated for one year by multiplying the current account balance by the interest
rate and adding this to the balance.

Exercise 2-9 Get the Money


Write an algorithm that reads the amount of money in pennies and displays the equivalent dollars,
quarters, dimes, nickles and pennies.

• One dollar corresponds to 100 pennies.


• One quarter corresponds to 25 pennies.
• One dime corresponds to 10 pennies.

• One nickle corresponds to 5 pennies.

Extra Exercises:

Exercise 2-10 BMI


Write an algorithm that calculates your BMI given your weight and height.
The BMI is calculated using the weight divided by height squared, where weight is in kg and height
is in meters.

Exercise 2-11 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 2-12 Flight Time


Write an algorithm to determine the flying time between two cities given the mileage between them
and the average speed of the airplane.

Exercise 2-13 Wooden Boxes


A factory manufactures wooden boxes (a box is a cuboid with six faces). The price of the wood
is 10LE/m2 . Write an algorithm that, given the three dimensions of the box in meters, calculates
and prints the total surface area of the box and the price of the wood needed.

Exercise 2-14 Consumer Price Index


The formula for calculating the Inflation Rate using the Consumer Price Index (CPI) is relatively
simple. Assume for the sake of simplicity that the index consists of one item and that in 1984 that
item cost 100 dollars. In December of 2014 that same item would probably cost 198 dollars. Let
us calculate the price difference between 1984 and 2014.

• Step 1: Calculate How Much has the Consumer Price Index Increased?
By looking at the above example, common sense would tell us that the index increased (it
went from 100 to 198). The question is how much has it increased? To calculate the change
we would take the second number (198) and subtract the first number (100). The result
would be 98. So we know that from 1984 until 2014 prices increased (Inflated) by 98 points
• Step 2: Comparing the CPI Change to the Original CPI
Since we know the increase in the Consumer Price Index we still need to compare it to
something, so we compare it to the price it started at (100).We do that by dividing the
increase by the first price or 98/100. the result is (.98).
• Step 3: Convert it to a Percentage
This number is still not very useful so we convert it into a percentage. To do that we multiply
by 100. So the result is a 98 increase in prices since 1984.

Write an algorithm that given two prices of the same item in two different years computes the CPI.
The algorithm should display the following for the example above

The price of the item in year 1984 is 100


The price of the item in year 2014 is 198
The CPI is 98%

Exercise 2-15 Theater


A theater owner would like to calculate his daily profit. The theater has three classes of tickets;
the price of the first class tickets is 150 LE, second class is 100 LE, and third class is 75 LE. The
profit is 20ticket price. The owner loses 70 LE. for each unreserved seat in the theater. Write an
algorithm that given the total seats in the theater and the reserved seats out of each class calculates
the total profit, total loss, and the difference between them.
Running example: For the following input

Total Number of seats: 300


First class tickets sold: 70
Second class tickets sold: 75
Third class tickets sold: 90

the algorithm should output

Total profit: 4950


Total loss: 4550
Difference: 400

You might also like