Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CSI-302

PROGRAMMING FUNDAMENTALS

LECTURE # 4
AL G OR I T HM: COMPAR I NG T WO NUMB E R S

1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 endif
6.0 if (n2 < n1)
6.1 print "n2 is greater"
7.0 endif
8.0 if (n1 = n2)
8.1 print "they are equal“
9.0 endif
10.0 end
2
NE ST E D I F/ E L SE SE L E CT ION ST RUCT UR E

• Nested if/else structures

• One inside another, test for multiple cases

• Once condition met, other statements skipped

3
E XAMPL E
COMPAR I NG T WO NUMB E R S W I T H NE ST E D I F - E LSE

1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 else
5.1 if (n2 < n1)
5.1.1 print "n2 is greater"
5.2 else
5.2.1 print "they are equal“
5.3 endif
6.0 endif
7.0 end
4
E XAMPL E: G R ADI NG W I T H NE ST E D I F - E L SE

if grade is greater than or equal to 90


Print “A”
else
if grade is greater than or equal to 80
Print “B”
else
if grade is greater than or equal to 70
Print “C”
else
if grade is greater than or equal to 60
Print “D”
else
Print “F”

5
E XAMPL E: G R ADI NG AL G OR I T HM W I T H NE ST E D I F - E L SE

if ( grade >= 90 ) then // 90 and above


Print "A"
else if ( grade >= 80 ) then // 80-89
Print "B"
else if ( grade >= 70 ) then // 70-79
Print << "C";
else if ( grade >= 60 ) then // 60-69
Print "D";
else // less than 60
Print "F";
end

6
I F- E L SE SE L E CT ION ST RUCT URE

Compound statement

• Set of statements within a block

if grade >= 60 then


Print "Passed"
else
Print "Failed"
Print "You must take this course again"
endif

7
E XAMPL E: I F - E L SE SE L E CT ION ST RUCT UR E

1.0 Declare num1


2.0 Print “Enter a number”
3.0 input num1
4.0 if num1 > 0 then
4.1 print “You entered a positive number”, num1
5.0 else if num1 < 0 then
5.1 Print “You entered a negative number” , num1
6.0 else
6.1 print “You entered 0”
7.0 endif
8.0 print “This line is always printed”

8
CL ASS ACT I V I T Y

• Write a pseudocode and draw a flowchart


• to read an employee name (NAME), overtime hours worked
(OVERTIME), hours absent (ABSENT)
• determine the bonus payment (PAYMENT)

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

> 40 hours 5000


> 30 but  40 hours 4000
> 20 but  30 hours 3000
> 10 but  20 hours 2000
 10 hours 1000

9
L OOPI NG FL OWCH ART

While condition is true then Is No


condition
Statement(s) true?

End while Yes

Statement (s)

10
R E PE T I T ION ST RUCT UR E

• In flowcharting one of the more confusing things is to separate


selection from looping

• This is because both structures use the diamond as their control


symbol

• In pseudocode we avoid this by using specific keywords to designate


looping

WHILE / ENDWHILE

11
W H I L E / E NDW H I LE

Start

count = 0
1.0 count = 0

2.0 WHILE count < 10

2.1 ADD 1 to count No


count <10

2.2 WRITE count


Yes
Write
3.0 ENDWHILE add 1 to “The End”
count

4.0 WRITE “The End”


Stop
write count

12
E XAMPL E OF P R I NT I NG ODD NUMB E R S

• Write an algorithm and draw a flowchart that will


• Print first 10 odd numbers starting from the number provided by the
user.
• If it is even number, then start from the next odd number if it is odd
number then start with the current number
• E.g. the user input 10 then the out put will be
• 11 13 15 17 19 21 23 25 27 29
• If the user input 5 then the output will be
• 5 7 9 11 13 15 17 19 21 23

• Hint: Use number mod 2 = 0 to determine the even number

13
AL G OR I T HM FOR P R I NT I NG 1 0 ODD NUMB E R S

1. Start
2. declare count, num
3. input num
4. if num mod 2 = 0 then
4.1 num = num + 1
5. end if
6. count = 0
7. while count < 10
7.1 print num
7.2 num = num + 2
7.3 count = count +1
8. end while
9. End
14
FL OW C H ART FOR P R I NT I NG 1 0 OD D NU MB E R S

START
1

Declare count, num


count = 0

Input num False


count < 10

False True
num mod 2 = 0
Print num

True
num = num + 2
num = num +1 count = count +1

END

15
CL ASS ACT I V I T Y

• Write an algorithm and draw a flowchart that do following:

• Read an employee number (EMPNO), employee name (NAME),


overtime hours worked (OVERTIME), hours absent (ABSENT) and

• Determine the bonus payment (PAYMENT) for 10 employees one by


one

Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours 5000


>30 but  40 hours 4000
>20 but  30 hours 3000
>10 but  20 hours 2000
 10 hours 1000

16
CL ASS ACT I V I T Y

• Write an algorithm and draw a flowchart that will get 10 numbers


from the user and print their average.

17
CL ASS ACT I V I T Y: SQUAR E AND CUB E

• Write an algorithm and draw a flowchart for a program that will

• Get one number from user

• Print its square if it is an even number

• Print its cube if it is and odd number

• The loop will run 5 times

18
CL ASS ACT I V I T Y

• Write down an algorithm of a program that will roll two dices and
add the numbers that appear on the top. If the sum is odd and
greater than 8 the player wins one chocolate, otherwise two
chocolates. If the number is even, he/she will win an ice cream.

• Draw a flow chart as well

19
CL ASS ACT I V I T Y

• Write down an algorithm for a program in which player will roll a


dice for 5 times and all the numbers that appear on the top will be
added. If the sum of 5 turns is divisible by 3 and odd the player wins
otherwise player loses

• Draw the flowchart for above given program

20
CL ASS ACT I V I T Y

• Write down an algorithm for a program that takes three


numbers from the user and tells which one is the largest.
Checks if the sum is odd or even and tell the user. The program
keeps on taking three numbers from the user for 5 time.

21
CL ASS ACT I V I T Y

• Write down a program that will take some numbers as input from
the user one by one and will tell the user which number was
maximum and minimum of all the entered numbers.

• The count of the numbers that the user is interested to compare


will be given by the user at the start of the program

22
END

You might also like