1 / 34

Introduction to Programming

Learn the basics of programming, including algorithms and creating flowcharts. Understand how to break down tasks into small steps and rules, implement algorithms, and create programs using flowcharts.

jeffbridges
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 305171 Computer Programming RattapoomWaranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University Introduction to Programming

  2. What is Algorithm • Algorithm is a process or a set of rules to be followed in calculations or other problem-solving operations. • The instructions should be simple and clear enough so that each step can be done without confusion.

  3. Example • This is an example of an algorithm for sorting cards with colors on them into piles of the same color: • Pick up all of the cards. • Pick a card from your hand and look at the color of the card. • If there is already a pile of cards of that color, put this card on that pile. • If there is no pile of cards of that color, make a new pile of just this card color. • If there is still a card in your hand, go back to the second step. • If there is not still a card in your hand, then the cards are sorted, you are done.

  4. Real-world Algorithm

  5. Algorithm?

  6. What is A Program? • A program is a very specific set of steps and rules. • A program is an implementation of an algorithm in a particular language to run on a computer (usually a particular kind of computer) • Algorithm = what we want to do • Program = what we actually did • The process of creating a program is called programming. • Programming is breaking a task down into small steps and rules.

  7. Let’s do some Examples • Put these words in alphabetical order: apple, zebra, abacus • Count the number of words in these sentences: • “Programming really can be fun.” • “John,do it again.” • “Some people just love to type really short lines, instead of using the full width of the page.”

  8. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Flowchart • A flowchart is a diagram that depicts the “flow” of a program.

  9. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Basic Flowchart Symbols • Terminals • represented by rounded rectangles • indicate a starting or ending point Terminal

  10. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Basic Flowchart Symbols • Input/Output Operations • represented by parallelograms • indicate an input or output operation Input/OutputOperations

  11. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Basic Flowchart Symbols • Processes • represented by rectangles • indicates a process such as a mathematical computation or variable assignment Process

  12. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ?

  13. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? How many hours did you work?

  14. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? How many hours did you work? 40

  15. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? How many hours did you work? 40 How much do you get paid per hour?

  16. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: ? How many hours did you work? 40 How much do you get paid per hour? 300

  17. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 How many hours did you work? 40 How much do you get paid per hour? 300

  18. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 How many hours did you work? 40 How much do you get paid per hour? 300 12000

  19. START END Display message “How many hours did you work?” Display message “How much do you get paid per hour?” Read Hours Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay Stepping Through the Flowchart Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 How many hours did you work? 40 How much do you get paid per hour? 300 12000

  20. Four Flowchart Structures • Sequence • Decision • Repetition • Case

  21. Sequence Structure • a series of actions are performed in sequence • The pay-calculating example was a sequence flowchart.

  22. X < Y ? B A Decision Structure • One of two possible actions is taken, depending on a condition. • In the flowchart segment below, the question “is x < y?” is asked. If the answer is no, then process A is performed. If the answer is yes, then process B is performed. Yes No

  23. YES x < y? A Repetition Structure • A repetition structure represents part of the program that repeats. This structure is commonly known as a loop. • In the flowchart segment, the question “is x < y?” is asked. If the answer is yes, then Process A is performed. The question “is x < y?” is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited.

  24. CASEyears_employed 1 3 Other 2 bonus = 100 bonus = 400 bonus = 800 bonus = 200 Case Structure • One of several possible actions is taken, depending on the contents of a variable. • The structure below indicates actions to perform depending on the value in years_employed.

  25. START END A A Connector • Sometimes a flowchart will not fit on one page length. • A connector (represented by a small circle) allows you to connect two flowchart segments on the same page. • The “A” connector indicates that the second flowchart segment begins where the first segment ends.

  26. START calc_pay Read Input. Call calc_pay function. Display results. END return Modules • The position of the module symbol indicates the point the module is executed. • A separate flowchart can be constructed for the module. net = rate * hour pay = net - tax

  27. Other common Symbols • Off-page connector • Used to connect remote flowchart portion on different page • Comment • Used to add description • Document • Used to represent a print out • Database • Used to represent a database Put any Comments Here.

  28. Pseudocode • Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and subroutines. (Wikipedia)

  29. Pseudocode • Pseudocode generally does not actually obey the syntax rules of any particular language. • There is no systematic standard form. • Pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other.

  30. A Guideline for Pseudocode • Write only one statement per line • Capitalize initial keyword • Indent to show hierarchy • End multiline structures • Keep statements language independent

  31. Example BEGIN Procedure_NetCalc READ name, grossPay, taxes IF taxes > 0 net = grossPay – taxes ELSE net = grossPay ENDIF WRITE name, net END Procedure_NetCalc

  32. 3 < Y ? X = 10 X = 5 Decision Structure • Pseudocode IF Y > 3 X = 5 ELSE X = 10 ENDIF Yes No

  33. Repetition Structure and Module BEGIN count = 0 WHILE count < 10 DO Process ENDWHILE WRITE “The End” END BEGIN Process ADD 1 to count WRITE count END Process Process Start Add 1 to count count = 0 Write count Yes Process Count < 10 Return No Count < 10 Write “The End” End

  34. Flowchart vsPseudocode

More Related