Beruflich Dokumente
Kultur Dokumente
Assignment No 1
Mirza Mubasher Baig
Department of Computer Science
FAST-NUCES Lahore
1. Chapter 2
Algorithm 1 Question No. 18
1: Declare Variables M ilkP roduced, T otalCartons,
M ilkCartonCapacity, CostP erLiter, P rof itP erCarton
2:
M ilkCartonCapacity = 3.78
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
3:
Input P ayRate
4:
5:
6:
7:
Input T axRate
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
Display
Parents
Contributions
P arentContributionT oSavingBonds
to
Prize
Bonds
$,
3:
4:
Input M assOne
5:
6:
Input M assT wo
7:
8:
Input Distance
9:
10:
2. Chapter 4
3. Chapter 5
3:
4:
Input f irstN um
Display Message: Enter Second Number
5:
Input secondN um
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
end while
else
16:
17:
Algorithm 5 : Problem 13
1: Declare
populationT ownA, growthRateT ownA,
growthRateT ownB, numberOf Y ears
2:
3:
4:
5:
6:
7:
8:
9:
populationT ownB,
numberOf Y ears = 0
if growthRateT ownA > growthRateT ownB then
while populationT ownA <= populationT ownB do
13:
numberOf Y ears = numberOf Y ears + 1
14:
populationT ownA = populationT ownA * (1 + growthRateT ownA )
10:
11:
12:
15:
end while
Display After , numberOf Y ears, years population of town A will
exceed that of town B
18: else
19:
Display(Message: Population of town A can never be greater than that
of town B)
20: end if
16:
17:
Algorithm 6 : Problem 14
1: Declare a, k
2:
3:
Input a
4:
5:
6:
k=0
while a 6= 1 do
if (a mod 2) == 0 then
7:
8:
a = a/2
else
9:
a = 3a +1
end if
10:
11:
12:
13:
k=k+1
end while
Display k = , k
Algorithm 7 : Problem 18
1: Declare totalLockers, totalStudents, lockerCounter, studentCounter,
totalDivisors, totalOpenLockers
2:
3:
Input totalLockers
4:
5:
Input totalStudents
6:
totalOpenLockers = 0
7:
8:
lockerCounter = 1
while lockerCounter totalLockers do
9:
totalDivisors = 1
12:
studentCounter = 2
while (studentCounter <= totalStudents) and (studentCounter <=
lockerCounter) do
if studentCounter divides lockerCounter then
13:
14:
15:
16:
totalDivisors = totalDivisors + 1
end if
end while
if (totalDivisors mod 2) == 0 then
17:
18:
totalOpenLockers = totalOpenLockers + 1
end if
10:
11:
19:
20:
lockerCounter = lockerCounter + 1
end while
21:
Algorithm 8 : Problem 27
1: Declare totalApartments, basicRent, rentIncrease, maintaineceAmount,
optimalRent, totalOccupiedApartments, apartmentRent, totalP rof it
2:
3:
4:
5:
totalApartments = 50;
Display Message: Enter basic rent (rent to occupy all apartments)
Input basicRent
Display Message: Enter rent increase that results in a vacant apartment
Input rentIncrease
Display Message: Enter maintenance amount
8: Input maintaineceAmount
6:
7:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
optimalRent = basicRent
totalOccupiedApartments = totalApartments
apartmentRent = basicRent
totalP rof it =
totalOccupiedApartments * ( apartmentRent - maintaineceAmount)
continueIncreasingP rof itF lag = 1
while
(continueIncreasingP rof itF lag
==
1)
and
(totalOccupiedApartments > 0) do
newApartmentRent = optimalRent + rentIncrease
totalOccupiedApartments = totalOccupiedApartments - 1
newP rof it = totalOccupiedApartments *
( newApartmentRent - maintaineceAmount)
if newP rof it > totalP rof it then
totalP rof it = newP rof it
optimalRent = newApartmentRent
else
continueIncreasingP rof itF lag = 0
end if
end while
Display Total Apartments to be rented = , totalOccupiedApartments
Display Total Income = , totalP rof it