Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.

Similar presentations


Presentation on theme: "CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language."— Presentation transcript:

1 CS 1400 Course Reader Introduction

2 What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language

3 What is an algorithm? Def: An algorithm is a logical sequence of instructions to accomplish a task. important points: 1.instructions must be well ordered 2.instructions must be unambiguous 3.the process must eventually end 4.the actions must be doable 5.the algorithm must produce the required result

4 from a shampoo bottle… lather rinse repeat –algorithm critique: too ambiguous doesn’t end

5 A more detailed algorithm for a shampoo bottle… Wet hair Pick up shampoo bottle Open the bottle Pour some shampoo on your hand Close the shampoo bottle Set bottle down Put shampoo on hair Rub shampoo through hair until lather covers hair Rinse hair until all lather is removed Repeat the previous eight steps one more time

6 What is a computer program? A computer program is just an algorithm expressed in computer language instructions

7 Program development… Steps: 1.analyze the problem 2.outline the solution 3.develop an algorithm in pseudocode 4.test the algorithm 5.code the algorithm into a computer language 6.run and test the computer program 7.document and maintain the program

8 Computers are not smart! While instructions to a friend may assume implied understanding and intelligence, instructions to a computer need to be simple, specific, and detailed. The computer will not attempt to determine the purpose of your instructions – it will only blindly carry them out.

9 Types of user interfaces Graphical Console this semester next semester!

10 Pseudocode flexible (no rigid syntax rules) English-like, but unambiguous contains: –keywords (words that have special meaning) –variables (representations of a saved value) –operators –punctuation

11 Keywords GetElse PrintEnd If AndRepeat OrEnd Repeat IfWhile End While

12 Operators + (addition) - (subtraction) * (multiplication) / (division) = (assignment)

13 A. Basic Calculations form: = Examples: cost = tax * price (calculate the sum of tax and price, save the result as cost) area = length * width (calculate the product of length and width, save the result as area)

14 B. Displaying messages and values form: Print “ ” form: Print Examples: Print “Hello!” (display the message “Hello” on the screen) Print cost (display the current value of variable cost on the screen)

15 C. Getting input form: Get from user Examples: Get price from user (get the value for variable price from the user’s keyboard)

16 Pseudocode Example 1: “Write a program to calculate the volume of a box, using dimensions provided by the user.” Print “Enter height, width, and depth of box:” Get height from user Get width from user Get depth from user volume = height * width * depth Print “Volume is: “ Print volume

17 Pseudocode Example 2: “Write a program to calculate a paycheck using regular hours, payrate, and overtime hours provided by the user. Assume overtime is paid at double.”.

18 D. Conditions form: If Else End If comparisons use:, =, !=, ==, And, Or

19 Example If age < 21 Print “Sorry, you are too young” Print “Please try again next year” Else Print “You may enter” End If

20 D. Conditions (alternate) form: If End If Example: If weight < 90 Print “Consider eating more protein” End If

21 Example Write a program that gets an employee's information from a user and calculates their pay for the week. The input includes the employee total hours worked this week and their hourly wage. The employee is to be paid their basic wage for the first 40 hours worked and time-and- a-half for all hours above 40. Output the regular pay, overtime pay, and total pay for the week to the screen. If the employee worked 40 hours or less, do not output any information about overtime pay.

22 Step 1: Analyze the problem Inputs: –hours worked, pay rate Given values: –over 40 hours is overtime –overtime is 1.5 of regular pay Calculations: –regular pay, overtime pay, total pay Outputs: –regular pay, overtime pay (if present), total pay

23 Step 2: Outline solution Get hours worked, and pay rate from a user If there was overtime, calculate regular pay on the first 40 hours and 1½ pay for additional hours Otherwise, calculate all hours worked at regular pay Print regular pay, If there was overtime, print the overtime pay Print total pay

24 Step 3: Algorithm in Pseudocode 1.Print “Enter hours worked: “ 2.Get hours from user 3.Print “Enter rate of pay: “ 4.Get payRate from user 5.If hours > 40 6.regularPay = 40 * payRate 7.overtimePay = (hours – 40) * payRate * 1.5 8. Else 9.regularPay = hours * payRate 10.overtimePay = 0 11. End If 12. totalPay = regularPay + overtimePay 13.Print "Regular Pay: ", regularPay 14. If overtimePay != 0 15.Print "Overtime Pay: ", overtimePay 16. End If 17. Print "Total for this week: ", totalPay

25 Step 4: Test algorithm InstrscreennamehourspayRateregularPayovertimePay …

26 E. Repetition form: Repeat times End Repeat Example: Repeat 5 times Print “Vote for Napoleon!” End Repeat

27 E. Repetition (alternate) form: While End While Example: Print “Enter a positive number: “ Get number from user While number < 0 Print “Sorry, too low. Try again:” Get number from user End While

28 Examples Write a program to output the squares of the numbers 1 through 100 Write a program to output the sum of 50 product costs entered by the user Write a program to output the sum of a list of positive numbers entered by the user


Download ppt "CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language."

Similar presentations


Ads by Google