Uploaded by Jay

GIU 2428 57 8021 2022-07-21T20 21 41

advertisement
The German International University
Faculty of Informatics and Computer Science
Prof. Dr. Slim Abdennadher
Prof. Dr. Ansgar Meroth
Introduction to Computer Science, Winter Semester 2021
Practice Assignment 2
Discussion: 30.10.2021 - 4.11.2021
Exercise 2-1
Flight Time
To be solved in Lab
Write an algorithm to determine the flying time between two cities given the mileage between them
and the average speed of the airplane.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
float v ;
float d ;
float t ;
c o u t << " P l e a s e s p e c i f y t h e a v e r a g e s p e e d i n m/ s \n" ;
c i n >> v ;
c o u t << " P l e a s e s p e c i f y t h e d i s t a n c e i n m \n" ;
c i n >> d ;
t = d/v ;
c o u t << "The time i s " << t << " s e c o n d s " << e n d l ;
}
return 0 ;
Exercise 2-2
BMI
To be discussed in Tutorial
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.
Solution:
#include <i o s t r e a m >
#include <cmath>
using namespace s t d ;
int main ( )
{
float h ;
f l o a t m;
f l o a t BMI ;
c o u t << " P l e a s e s p e c i f y t h e mass i n kg \n" ;
c i n >> m;
c o u t << " P l e a s e s p e c i f y t h e h e i g h t i n m e t e r s \n" ;
c i n >> h ;
BMI = m/ ( pow ( h , 2 ) ) ;
c o u t << "The BMI i s " << BMI << e n d l ;
}
return 0 ;
Exercise 2-3
Appliance Cost
Write an algorithm that calculates and prints the annual cost of running an appliance given the
cost per kilowatt-hour in cents and the number of kilowatt-hours the appliance uses in a year.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( ) {
int CPK;
// The c o s t p e r KiloWatt−hour i n c e n t s
f l o a t KPY;
// The number o f KiloWatt−h o u r s used i n a y e a r
f l o a t CPY;
// The Cost p e r y e a r t o be c a l c u l a t e d
cout<<" P l e a s e e n t e r t h e c o s t p e r KiloWatt−h o u r s i n c e n t "<<e n d l ;
c i n >>CPK;
cout<<" P l e a s e e n t e r t h e number o f kiloWatt−h o u r s used i n a y e a r "<<e n d l ;
c i n >>KPY;
CPY=KPY∗CPK;
cout<< "The annual c o s t i s "<<CPY<<" c e n t s "<<e n d l ;
}
return 0 ;
Exercise 2-4
Planting Trees
To be discussed in Tutorial
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.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
float L;
f l o a t W;
f l o a t A;
int n _ t r e e s ;
c o u t << " P l e a s e e n t e r t h e l e n g t h i n m e t e r s " << e n d l ;
c i n >> L ;
c o u t << " P l e a s e e n t e r t h e width i n m e t e r s " << e n d l ;
c i n >> W;
L=100∗L ; // c o n v e r t t h e l e n g t h t o cm
W=100∗W; // c o n v e r t t h e w i d t h t o cm
A = L∗W; // The area i n cm^2
n _ t r e e s = int (A/ 5 0 ) ;
c o u t << "The a r e a i s " << A << " The number o f t r e e s a r e "
<< n _ t r e e s << e n d l ;
}
return 0 ;
Exercise 2-5
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.
Solution:
#include <i o s t r e a m >
#include <cmath>
using namespace s t d ;
int main ( )
{
float b ;
float i ;
f l o a t new_amount ;
cout<<" p l e a s e s p e c i f y t h e i n t e r e s t r a t e \n" ;
c i n >>i ;
cout<<" p l e a s e s p e c i f y t h e i n i t i a l b a l a n c e \n" ;
c i n >>b ;
new_amount=b∗pow((1+ i ) , 3 ) ;
cout<<"The amount a f t e r 3 y e a r s i s "<<new_amount<<e n d l ;
}
return 0 ;
Exercise 2-6
Pythagorean Theorem
To be solved in Lab
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 =
b =
c =
m2 − n2
2×m×n
p
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.
Solution:
#include <i o s t r e a m >
#include <cmath>
using namespace s t d ;
int main ( ) {
int m;
int n ;
int a ;
int b ;
int c ;
cout<<" p l e a s e e n t e r t h e v a l u e o f m "<<e n d l ;
c i n >>m;
cout<<" p l e a s e e n t e r t h e v a l u e o f n "<<e n d l ;
c i n >>n ;
a=pow (m,2) −pow ( n , 2 ) ;
b=2∗m∗n ;
c=s q r t ( pow ( a ,2)+pow ( b , 2 ) ) ;
cout<<" a= "<<a<<" b= "<<b<<" c= "<<c<<e n d l ;
return 0 ;
}
Exercise 2-7
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.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
int h o u r s ;
int minutes ;
int s e c o n d s ;
int i n p u t ;
int rem_seconds ;
c o u t << " P l e a s e s p e c i f y t h e t o t a l time i n s e c o n d s " << e n d l ;
c i n >> i n p u t ;
rem_seconds = i n p u t ;
h o u r s = rem_seconds / 3 6 0 0 ; // Remember h o u r s i s o f t y p e i n t
rem_seconds = rem_seconds %3600;
minutes = rem_seconds / 6 0 ;
rem_seconds = rem_seconds %60;
s e c o n d s = rem_seconds ;
c o u t << h o u r s << " h o u r s "<< minutes << " minutes "
<< s e c o n d s << " s e c o n d s " ;
return 0 ;
}
Exercise 2-8
Get the Money
To be solved in Lab
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.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
int d o l l a r s ;
int q u a r t e r s ;
int dimes ;
int n i c k l e s ;
int p e n n i e s ;
int i n p u t ;
int rem_pennies ;
c o u t << " P l e a s e s p e c i f y t h e t o t a l amount o f p e n n i e s " << e n d l ;
c i n >> i n p u t ;
rem_pennies = i n p u t ;
d o l l a r s = rem_pennies / 1 0 0 ; // Remember d o l l a r s i s o f t y p e i n t
rem_pennies = rem_pennies %100;
q u a r t e r s = rem_pennies / 2 5 ;
rem_pennies = rem_pennies %25;
dimes = rem_pennies / 1 0 ;
rem_pennies = rem_pennies %10;
n i c k l e s = rem_pennies / 5 ;
rem_pennies = rem_pennies %5;
p e n n i e s = rem_pennies ;
c o u t << d o l l a r s << " d o l l a r s "<< q u a r t e r s << " q u a r t e r s "
<< dimes << " dimes " << n i c k l e s << " n i c k l e s "
<< p e n n i e s << " p e n n i e s " << e n d l ;
return 0 ;
}
Exercise 2-9
Reverse Digits
To be discussed in Tutorial
Write an algorithm that given a 3-digit number prints out the number in reversed order.
Example: if the number is 425 then the output should be 524.
Solution:
#include <i o s t r e a m >
using namespace s t d ;
int main ( )
{
int num ;
int r e v e r s e d ;
c o u t << " P l e a s e e n t e r t h e number t o be r e v e r s e d " << e n d l ;
c i n >> num ;
int u n i t s = num % 1 0 ;
int t e n s = ( ( num−u n i t s ) / 1 0 ) % 1 0 ;
int hundreds = ( ( num−10∗ t e n s −u n i t s ) / 1 0 0 ) ;
r e v e r s e d = 100∗ u n i t s +10∗ t e n s +1∗hundreds ;
c o u t << "The r e v e r s e d number i s "<< r e v e r s e d << e n d l ;
}
Download