Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 110: Programming Language I Matin Saad Abdullah UB 404.

Similar presentations


Presentation on theme: "CSE 110: Programming Language I Matin Saad Abdullah UB 404."— Presentation transcript:

1 CSE 110: Programming Language I Matin Saad Abdullah mabdullah@bracuniversity.net UB 404

2 Problem Solving Start with a real-world problem that needs to be solved Convert the real-world problem into a computational problem Develop an algorithm and use it to solve the computational problem  An algorithm is a precise list of instructions that determine what operations to perform on what pieces of data, in what order

3 Algorithm Example Consider the problem of computing the sum of 2 numbers which are entered by the user at the keyboard: Possible algorithm to solve the problem:  Read (i.e. input) the first number and store it in the computer  Read the second number and store it in the computer  Add the first number to second number to produce a sum, SUM. Store SUM in the computer  Display (i.e. output) the value of SUM

4 Computer Programming  A computer is a machine that can compute  What does “compute” mean ?  Can represent & manipulate data  Programming is about finding a way to tell the machine what computational operations it must do on what data  A program is just an algorithm, expressed in a language the computer an understand

5 Programming Overview Program Design Process has 2 phases:  Problem Solving Phase  Creates an algorithm that solves the problem  Implementation (Coding) Phase  Translates the algorithm into a programming language

6 Flow Control Design Tool: Flowcharts Flowchart - a graphical way of writing algorithms  Rectangle is used for calculations  Parallelogram is used for input and output  Circle is used as connector  Diamond is used as decision  Symbols are connected by arrows to represent the order of the operations

7 Sequential Program Statements With sequential program statements, you execute one statement after another (like steps in a list)  After step X is completed, step X+1 will be performed, until the last statement has been executed. Every step in the list will be performed. Each step in the list will be performed once and only once.

8 Pseudocode Sequential Algorithm Example (1/2) Real World Problem:  Calculate two childrens’ allowances, based upon 75 cents per year old. Known Values  Rate = 75 cents per year Inputs  Ages of children Calculations  Allowance = Age x Rate Outputs  Allowances for each child

9 Allow1 = Age1 x Rate input Age1 start Print Allow1 stop prompt Age1 Allow2 = Age2 x Rate Print Allow2 input Age2 prompt Age2

10 Non-Sequential Program Statements Programs that execute only sequential program statements are pretty simplistic. Most programs need more flexibility in the order of statement execution. The order of statement execution is called the flow of control. Control statements allow the execution of statements based upon decisions.

11 Flow of Control Statements Conditional statements:  Decide whether or not to execute a particular statement or statements  Also called the selection statements or decision statements.

12 Pseudocode Selection Algorithm Example (1/2) Real World Problem:  Calculate one child’s allowance, based upon 75 cents per year old if s/he is under 10, and $1 per year if 10 or over. Known Values  YoungRate = 75 cents per year  OlderRate = 100 cents per year  BreakAge = 10 Inputs  Age of child Calculations  Allowance = Age x Rate Outputs  Allowance

13 Allow = Age x YoungRate start Print Allow stop prompt Age Allow = Age x OlderRate Age < 10 FALSETRUE input Age

14 Flow of Control Statements Loop statements:  Repeatedly execute the same statement(s) for a certain number of times or until a test condition is satisfied.

15 Pseudocode Looping Algorithm Example (1/2) Real World Problem:  Calculate the total allowance paid to each of three children at $1 per year old. Known Values  Rate = $1 per year  NumKids = 3 Inputs  Ages of children Calculations  Allowance = Age x Rate Outputs  Total Allowance paid

16 start Display Total stop Read Age Calc Allowance KidsPaid < 3 FALSETRUE KidsPaid = 0 Total = 0 Add Allowance to Total Increment KidsPaid Prompt Age

17 Flow Control Design Tool: Flowcharts Flowchart - a graphical way of writing algorithms  Rectangle is used for calculations  Parallelogram is used for input and output  Circle is used as connector  Diamond is used as decision  Symbols are connected by arrows to represent the order of the operations

18 High-Level Design Tool: Structure Charts So a flowchart:  Details exactly HOW your program will do each task In contrast, our next design tool, the structure chart:  Shows only WHAT tasks your program will do (not HOW it will complete them)

19 High-Level Design Tool: Structure Charts Structure charts are hierarchical diagrams  They diagram the overall program structure  They show the relationship between all the tasks in the program  They indicate which data is shared by the tasks

20 Creating a Structure Chart A large program design is first broken into tasks Tasks are repeatedly divided into even smaller subtasks  Rectangles are used to represent tasks/subtasks within the program  Lines are used to hierarchically connect the tasks/sutasks  Subtasks are diagrammed below the task that they are part of Task SubTask1SubTask2

21 Creating Structure Charts  An upward arrow indicates that the data value has been set inside the task and is being passed out for use by other tasks  A downward arrow indicates a data value previously set in some other task is now being passed into this task  Data may also be passed both into a task (downward arrow), modified, and passed back out again (upward arrow). Task SubTask1SubTask2 Data1 The passing of data between tasks is shown by arrows going up from or down to the task’s rectangle

22 Sample Structure Chart main Get_InputProcess_DataDisplay_Results Data Result Data Result Given a generic program that reads some data from the user, runs some calculations using that data, and displays results to the user, the structure chart could look like this:

23 Structure Chart for Sequential Program main Get_AgesCalculate_AllowancesDisplay_Allowances Age1, Age2 Allow1, Allow2 Age1, Age2 Allow1, Allow2 Using our previous sequential program design example (calculate your two childrens’ allowances, based upon 75 cents per year old), the structure chart might be:

24 Structure Chart for Selection Program main Get_AgeCalculate_AllowanceDisplay_Allowance AgeAllowanceAge Allowance Using our previous selection program design example (calculate one child’s allowance, based upon 75 cents per year old if s/he is under 10, and $1 per year if 10 or over), the structure chart might be:

25 Structure Chart for Selection Program main Get_AgeCalculate_AllowanceDisplay_Allowance AgeAllowanceAge Allowance Note that this SAME structure chart could also have been used for the sequential program (calculate your two childrens’ allowances, based upon 75 cents per year old). Each of the modules in the chart would be called by main TWICE.

26 Structure Chart for Looping Program main Get_Age Calc_Allowance_TotalDisplay_Total Age Total Calc_Allowance Allowance Age Using our previous looping program design example (calculate the total allowance paid to each of three children at $1 per year old), the structure chart might be: The Get_Age and Calc_Allowance modules would be called every time the program loops.

27 Summary of Structure Chart Creation Put name of program in rectangle root of an upside-down tree. Determine the main subtasks that must be performed by the program to solve the problem.  Put main subtasks in rectangles below the root, and draw a connecting line from the root to each subtask. Examine each subtask individually, and break them down into even smaller tasks.  Put subtasks in rectangles below the task that they are part of, and draw a connecting line from task to subtask. Repeat until the bottom tasks of the tree are very simple tasks to complete


Download ppt "CSE 110: Programming Language I Matin Saad Abdullah UB 404."

Similar presentations


Ads by Google