Sie sind auf Seite 1von 6

Partial Solution

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:

CostP erLiter = 0.38

4:

P rof itP erCarton = 0.27

5:

Display Message: Enter Total Milk Produced

6:

Input M ilkP roduced

7:

T otalCartons = ROUND(M ilkP roduced/M ilkCartonCapacity)

8:

T otalCost = M ilkP roduced * CostP erLiter

9:

T otalP rof it = T otalCartons * P rof itP erCarton

10:

Display Total cartons needed = $, T otalCartons

11:

Display Total cost of milk = $, T otalCost

12:

Display Total Profit = $, T otalP rof it

Email addresses: mubasher.baig@nu.edu.pk (Mirza Mubasher Baig),


mubasher.baig@nu.edu.pk (Mirza Mubasher Baig)

Preprint submitted to Elsevier

September 15, 2016

Algorithm 2 Question No. 20


1: Declare Variables
P ayRate, HoursW orkedP erW eek, T otalIncome,
IncomeAf terT axDeduction, T axRate, T otalT ax,
M oneyF orClothes, M oneyF orSchoolSupplies,
SavingBondsV alue, P arentContributionT oSavingBonds
2:

Display Message: Enter Hourly Rate

3:

Input P ayRate

4:

Display Message: Enter Hours Per Week

5:

Input HoursW orkedP erW eek

6:

Display Message: Tax Rate

7:

Input T axRate

8:

T otalIncome = HoursW orkedP erW eek * P ayRate

9:

T otalT ax = T otalIncome * T axRate

10:

IncomeAf terT axDeduction = T otalIncome - T otalT ax

11:

M oneyF orClothes = IncomeAf terT axDeduction / 10

12:

M oneyF orSchoolSupplies = IncomeAf terT axDeduction / 100

13:

RemainingAmount = IncomeAf terT axDeduction - M oneyF orClothes M oneyF orSchoolSupplies

14:

SavingBondsV alue = RemainingAmount / 4;

15:

P arentContributionT oSavingBonds = SavingBondsV alue / 2

16:

Display Income Before Tax Deduction $, T otalIncome

17:

Display Income After Tax Deduction $, IncomeAf terT axDeduction

18:

Display Clothes Expenses $, M oneyF orClothes

19:

Display School Supplies Expenses $, M oneyF orSchoolSupplies

20:

Display Your Contribution to Prize Bonds $, SavingBondsV alue

21:

Display
Parents
Contributions
P arentContributionT oSavingBonds

to

Prize

Bonds

$,

Algorithm 3 Question No. 23


1: Declare Variables M assOne, M assT wo, Distance,
GravitattionalConstant, F orce
2:

SET GravitattionalConstant = 6.67E-8

3:

Display Message: Enter Mass of First Object in grams

4:

Input M assOne

5:

Display Message: Enter Mass of Second Object in grams

6:

Input M assT wo

7:

Display Message: Enter distance between two objects in cm

8:

Input Distance

9:

F orce = GravitattionalConstant* ( M assOne * M assT wo )/( Distance


* Distance)

10:

Display Gravitational force between the two objects is , F orce

2. Chapter 4
3. Chapter 5

Algorithm 4 : Problem 9 (a, b)


1: Declare f irstN um, secondN um, counter
2:

Display Message: Enter First Number

3:
4:

Input f irstN um
Display Message: Enter Second Number

5:

Input secondN um

6:
7:

if f irstN um < secondN um then


counter = f irstN um

8:
9:
10:
11:
12:
13:

if f irstN um is Even then


counter = f irstN um + 1
end if
while counter <= secondN um do
Display counter
counter = counter + 2

14:
15:

end while
else

16:
17:

Display(Message: First Number must be smaller than second number)


end if

Algorithm 5 : Problem 13
1: Declare
populationT ownA, growthRateT ownA,
growthRateT ownB, numberOf Y ears
2:

Display Message: Population of town A =

3:

Input populationT ownA

4:

Display Message: Growth rate of town A =

5:

Input growthRateT ownA

6:

Display Message: Population of town B =

7:

Input populationT ownB

8:

Display Message: Growth rate of town B =

9:

Input growthRateT ownB

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:

populationT ownB = populationT ownB * (1 + growthRateT ownB )

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:

Display Message: Enter starting value =

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:

Display Message: Enter total number of lockers

3:

Input totalLockers

4:

Display Message: Enter total number of students

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:

Display Total Open Lockers = , totalOpenLockers

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

Das könnte Ihnen auch gefallen