Top Banner
Problem Solving and Programming CSE1001 Prof. Tulasi Prasad Sariki August 9, 2019 Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 1 / 41
45

Problem Solving and Programming CSE1001

Dec 06, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Problem Solving and Programming CSE1001

Problem Solving and ProgrammingCSE1001

Prof. Tulasi Prasad Sariki

August 9, 2019

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 1 / 41

Page 2: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Different patterns in Algorithm

Sequential - Sequential structure executes the program in the orderin which they appear in the program

Selectional (conditional-branching) - Selection structure controlthe flow of statement execution based on some condition

Iterational (Loops) - Iterational structures are used when part of theprogram is to be executed several times

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 2 / 41

Page 3: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Sequential Pattern

Example1: Find the average runs scored by a batsman in 4 matches

Algorithm

Step 1: StartStep 2: Input 4 scores say runs1,runs2,runs3 and runs4Step 3: Accumulate runs1,runs2,run3,and runs4 and store it in the variablecalled total runsStep 4: Divide total runs by 4 and find the averageStep 5: Display the averageStep 6: Stop

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 3 / 41

Page 4: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Flow Chart

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 4 / 41

Page 5: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Pseudo code:

Beginread run1,run2,run3 and run4compute total run= run1+run2+run3+run4compute batting average= total run/4display batting average

end

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 5 / 41

Page 6: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Batting Average

Program

p r i n t ( ” E n t e r f o u r S c o r e s ” )run1= i n t ( input ( ) )run2= i n t ( input ( ) )run3= i n t ( input ( ) )run4= i n t ( input ( ) )t o t a l r u n =(run1+run2+run3+run4 )b a t t i n g a v e r a g e = t o t a l r u n /4p r i n t ( ” B a t t i n g Average i s ” , b a t t i n g a v e r a g e )

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 6 / 41

Page 7: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Area of Circle

Algorithm

Step 1 : StartStep 2: Get the input for RADIUSStep 3 : Find the square of RADIUS and store it in SQUAREStep 4 : Multiply SQUARE with 3.14 and store the result in AREAStep 5 : Display AREAStep 6 : Stop

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 7 / 41

Page 8: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Flow Chart

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 8 / 41

Page 9: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Pseudo code:

Beginaccept radiuscompute square = radius * radiuscompute area = pi * squaredisplay area

end

Program

import mathp r i n t ( ” E n e t r Rad ius ” )r a d i u s = f l o a t ( input ( ) )a r e a = math . p i ∗ r a d i u s ∗ r a d i u sp r i n t ( ” Area o f C i r c l e i s ” , a r e a )

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 9 / 41

Page 10: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Exercise

An university is setting up a new lab at their premises. Design analgorithm and write Python code to determine the approximate cost to bespent for setting up the lab. Cost for setting the lab is sum of cost ofcomputers, cost of furnitures and labour cost. Use the following formulafor solving the problem:Cost of computer = cost of one computer * number of computersCost of furniture = Number of tables * cost of one table + number ofchairs * cost of one chairLabour cost = number of hours worked * wages per hour

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 10 / 41

Page 11: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Budget for Lab

Input Processing Outputcost of one com-puter, numberof computers,number of tables,cost of one table,number of chairs,cost of one chair,number of hoursworked, wages perhour

Budget = Cost of computers + costof furniture + labour costCost of computer = cost of one com-puter * number of computersCost of furniture = Number of tables* cost of one table + number of chairs* cost of one chairLabour cost = number of hoursworked * wages per hour

Budget forLab

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 11 / 41

Page 12: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Python Program

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 12 / 41

Page 13: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Python Program

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 13 / 41

Page 14: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Browsing Problem

Exercise

Given the number of hours and minutes browsed, write a program tocalculate bill for Internet Browsing in a browsing center. The conditionsare given below.(a) 1 Hour Rs.50(b) 1 minute Re. 1(c) Rs. 200 for five hoursBoundary condition: User can only browse for a maximum of 7 hoursCheck boundary conditions

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 14 / 41

Page 15: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Browsing Program

Input Processing OutputNumber ofhours andminutesbrowsed

Check number of hours browsed, if itis greater than 5 then add Rs 200 toamount for five hours and subtract 5from hoursAdd Rs for each hour and Re 1 foreach minuteBasic process involved: Multiplicationand addition

Amount tobe Paid

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 15 / 41

Page 16: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Pseudo code:

READ hours and minutesSET amount = 0IF hours >= 5 then

CALCULATE amount as amount + 200COMPUTE hours as hours 5

END IFCOMPUTE amount as amount + hours * 50COMPUTE amount as amount + minutes * 1PRINT amount

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 16 / 41

Page 17: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

InputHours = 6Minutes = 21OutputAmount = 271Processing InvolvedAmount = 200 for first five hours50 for sixth hour21 for each minuteInputHours = 8Minutes = 21OutputInvalid inputProcessing InvolvedBoundary conditions are violated

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 17 / 41

Page 18: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Already Know

To read values from user

Write arithmetic expressions in Python

Print values in a formatted way

Yet to Learn

Check a condition

Selection Pattern

A selection control statement is a control statement providingselective execution of instructions.

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 18 / 41

Page 19: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Already Know

To read values from user

Write arithmetic expressions in Python

Print values in a formatted way

Yet to Learn

Check a condition

Selection Pattern

A selection control statement is a control statement providingselective execution of instructions.

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 18 / 41

Page 20: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Already Know

To read values from user

Write arithmetic expressions in Python

Print values in a formatted way

Yet to Learn

Check a condition

Selection Pattern

A selection control statement is a control statement providingselective execution of instructions.

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 18 / 41

Page 21: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Control flow of decision making

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 19 / 41

Page 22: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

if Statement

An if statement is a selection control statement based on the valueof a given Boolean expression.

The if statement in PythonIf statement Example use

If condition:statements

else:statements

If grade >= 70 :print(’pass’)

else:print(’fail’)

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 20 / 41

Page 23: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Indentation in Python

One fairly unique aspect of Python is that the amount of indentationof each program line is significant.

In Python indentation is used to associate and group statements

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 21 / 41

Page 24: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Nested if Statements

There are often timeswhen selection amongmore than two sets ofstatements (suites) isneeded.

For such situations, ifstatements can benested, resulting inmulti-way selection.

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 22 / 41

Page 25: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Else if Ladder

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 23 / 41

Page 26: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Multiple Conditions

Multiple conditions can be check in a ’if’ statement using logicaloperators ’and’ and ’or’.

Python code to print ’excellent’ if mark1 and mark2 is greater than orequal to 90, print ’good’ if mark1 or mark2 is greater than or equal to90, print ’need to improve’ if both mark1 and mark2 are lesser than 90

Exampleif (mark1 >= 90 and mark2 >= 90):

print(’excellent’)if (mark1 >= 90 or mark2 >= 90):

print(’good’)else:

print(’needs to improve’)

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 24 / 41

Page 27: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Multiple Conditions

Multiple conditions can be check in a ’if’ statement using logicaloperators ’and’ and ’or’.

Python code to print ’excellent’ if mark1 and mark2 is greater than orequal to 90, print ’good’ if mark1 or mark2 is greater than or equal to90, print ’need to improve’ if both mark1 and mark2 are lesser than 90

Exampleif (mark1 >= 90 and mark2 >= 90):

print(’excellent’)if (mark1 >= 90 or mark2 >= 90):

print(’good’)else:

print(’needs to improve’)

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 24 / 41

Page 28: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Browsing Problem

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 25 / 41

Page 29: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Eligibility for Scholarship

Problem

Government of India has decided to give scholarship for students who arefirst graduates in family and have scored average > 98 in math, physicsand chemistry. Design an algorithm and write a Python program to checkif a student is eligible for scholarship.Boundary Conditions: All marks should be > 0

PACInput Processing Output

Read first grad-uate, physics,chemistry andmaths marks

Compute total = phy mark +che mark + math markAverage = total/3Check if the student is firstgraduate and average >= 98

Print either candidatequalified for Scholar-ship or candidate notqualified for Scholar-ship

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 26 / 41

Page 30: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Eligibility for Scholarship

Problem

Government of India has decided to give scholarship for students who arefirst graduates in family and have scored average > 98 in math, physicsand chemistry. Design an algorithm and write a Python program to checkif a student is eligible for scholarship.Boundary Conditions: All marks should be > 0

PACInput Processing Output

Read first grad-uate, physics,chemistry andmaths marks

Compute total = phy mark +che mark + math markAverage = total/3Check if the student is firstgraduate and average >= 98

Print either candidatequalified for Scholar-ship or candidate notqualified for Scholar-ship

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 26 / 41

Page 31: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Eligibility for Scholarship

Algorithm

Step 1 : StartStep 2: Read first graduate, physcis,chemistry and maths marksStep 3: If anyone of the mark is less than 0 then print ’invalid input’ andterminate executionStep 3 : Accumulate all the marks and store it in TotalStep 4 : Divide Total by 3 and store it in AverageStep 5 : If student is first graduate Average score is greater than or equalto 98 then print candidate qualified for ScholarshipElsePrint candidate not qualified for scholarshipStop 6: Stop

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 27 / 41

Page 32: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

InputFirst graduate = 1, Phy mark = 98, Che mark = 99, math mark = 98

Outputcandidate qualified for Scholarship

Processing InvolvedTotal = 295Average = 98.33Student is first graduate and average > 98

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 28 / 41

Page 33: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

InputFirst graduate = 0, Phy mark = 98, Che mark = 99, math mark = 98

Outputcandidate qualified for Scholarship

Processing InvolvedTotal = 295Average = 98.33Student is not first graduate and average > 98

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 29 / 41

Page 34: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

InputFirst graduate = 1, Phy mark = 98, Che mark = 99, math mark = 90

Outputcandidate qualified for Scholarship

Processing InvolvedTotal = 287Average = 95.67Student is first graduate and average < 98

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 30 / 41

Page 35: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 31 / 41

Page 36: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Algorithm for Largest of Three numbers

Algorithm

Step1: StartStep2: Read value of a, b and cStep3: If a is greater than b then

compare a with c and if a is bigger then saya is biggest else say c is biggestelse Compare b with c , if b is greater than csay b is biggest else c is biggest

Step 5: Stop

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 32 / 41

Page 37: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Flowchart

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 33 / 41

Page 38: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

Inputa = 12, b = 13, c = 14

Outputc is greatest

Processing Involvedb is greater than a but c is greater than b

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 34 / 41

Page 39: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

Inputa = 13, b = 12, c = 14

Outputc is greatest

Processing Involveda is greater than b but c is greater than a

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 35 / 41

Page 40: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

Inputa = 13, b = 2, c = 4

Outputa is greatest

Processing Involveda is greater than b and a is greater than c

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 36 / 41

Page 41: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Test Cases

Inputa = 3, b = 12, c = 4

Outputb is greatest

Processing Involvedb is greater than a and b is greater than c

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 37 / 41

Page 42: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Python Program

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 38 / 41

Page 43: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

if/else Ternary ExpressionConsider the following statement, which sets A to either Y or Z, based onthe truth value of X:

if X:A = Y

else:A = Z

new expression format that allows us to say the same thing in oneexpression:

A = Y if X else Z>>> A = ’t’ if ’spam’ else ’f’>>> A’t’>>> A = ’t’ if ” else ’f’>>> A’f’

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 39 / 41

Page 44: Problem Solving and Programming CSE1001

Different Patterns in Algorithm

Exercise Problem

Exercises

1. Write a python code to check whether a given number is odd or even?

2. Write a python code to check whether a given year is leap year or not?

3. Write a python code in finding the roots of a quadratic equation?

4. Write a python program to Generate Cluster of student based on theirCGPA. The details are as follows:

<= 9 CGPA <= 10 - outstanding<= 8 CGPA < 9 - excellent<= 7 CGPA < 8 - good<= 6 CGPA < 7 - average<= 5 CGPA < 6 - betterCGPA < 5 - poor

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 40 / 41

Page 45: Problem Solving and Programming CSE1001

Prof. Tulasi Prasad Sariki CSE1001 August 9, 2019 41 / 41