CS 1400 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS 1400

Description:

Write a program that gets an employee's information from a user and calculates ... The input includes the employee total hours worked this week and their hourly wage. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 29
Provided by: scottc50
Category:

less

Transcript and Presenter's Notes

Title: CS 1400


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
  • instructions must be well ordered
  • instructions must be unambiguous
  • the process must eventually end
  • the actions must be doable
  • the algorithm must produce the required result

4
from a shampoo bottle
  • lather
  • rinse
  • repeat
  • algorithm critique
  • too ambiguous
  • doesnt 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
  • analyze the problem
  • outline the solution
  • develop an algorithm in pseudocode
  • test the algorithm
  • code the algorithm into a computer language
  • run and test the computer program
  • 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

next semester!
this 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
  • Get Else
  • Print End If
  • And Repeat
  • Or End Repeat
  • If While
  • End While

12
Operators
  • (addition)
  • - (subtraction)
  • (multiplication)
  • / (division)
  • (assignment)

13
A. Basic Calculations
  • form ltvariablegt ltmath expressiongt
  • 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 ltmessagegt
  • form Print ltvariablegt
  • 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 ltvariablegt from user
  • Examples
  • Get price from user
  • (get the value for variable price from the users
    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 ltcomparisongt
  • ltstatements executed if truegt
  • Else
  • ltstatements executed if falsegt
  • End If
  • comparisons use
  • lt, gt, lt, gt, !, , And, Or

19
Example
  • If age lt 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 ltcomparisongt
  • ltstatements executed if truegt
  • End If
  • Example
  • If weight lt 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 gt 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
Instr screen name hours payRate regularPay overtimePay








26
E. Repetition
  • form Repeat ltNgt times
  • ltstatements to be repeatedgt
  • End Repeat
  • Example
  • Repeat 5 times
  • Print Vote for Napoleon!
  • End Repeat

27
E. Repetition (alternate)
  • form While ltcomparisongt
  • ltstatements to be repeated if truegt
  • End While
  • Example
  • Print Enter a positive number
  • Get number from user
  • While number lt 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
Write a Comment
User Comments (0)
About PowerShow.com