Top Banner
CS 1400 Course Reader Introduction
28

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

Dec 17, 2015

Download

Documents

Tyler Franklin
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: CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.

CS 1400

Course Reader

Introduction

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

What is Programming?

• Designing an appropriate algorithm

• Coding that algorithm in a computer language

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

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

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

from a shampoo bottle…

lather

rinse

repeat

– algorithm critique:• too ambiguous• doesn’t end

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

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

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

What is a computer program?

• A computer program is just an algorithm expressed in computer language instructions

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

Program development…

• Steps:1. analyze the problem2. outline the solution3. develop an algorithm in pseudocode4. test the algorithm5. code the algorithm into a computer

language6. run and test the computer program7. document and maintain the program

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

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.

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

Types of user interfaces

• Graphical

• Console

this semester

next semester!

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

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

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

Keywords

Get Else

Print End If

And Repeat

Or End Repeat

If While

End While

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

Operators

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

= (assignment)

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

A. Basic Calculations

form: <variable> = <math expression>

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)

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

B. Displaying messages and values

form: Print “<message>”form: Print <variable>Examples:

Print “Hello!”(display the message “Hello” on the screen)

Print cost(display the current value of variable cost on

the screen)

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

C. Getting input

form: Get <variable> from user

• Examples:

Get price from user

(get the value for variable price from the user’s keyboard)

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

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

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

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.”

.

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

D. Conditions

form: If <comparison> <statements executed if true>

Else<statements executed if false>

End If

comparisons use: <, >, <=, >=, !=, ==, And, Or

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

Example

If age < 21Print “Sorry, you are too young”Print “Please try again next year”

ElsePrint “You may enter”

End If

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

D. Conditions (alternate)

form: If <comparison>

<statements executed if true>

End If

• Example:

If weight < 90

Print “Consider eating more protein”

End If

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

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.

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

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

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

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

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

Step 3: Algorithm in Pseudocode1. Print “Enter hours worked: “ 2. Get hours from user3. Print “Enter rate of pay: “ 4. Get payRate from user5. 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

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

Step 4: Test algorithm

Instr screen name hours payRate regularPay overtimePay

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

E. Repetition

form: Repeat <N> times

<statements to be repeated>

End Repeat

• Example:

Repeat 5 times

Print “Vote for Napoleon!”

End Repeat

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

E. Repetition (alternate)form: While <comparison>

<statements to be repeated if true>End While

• Example:Print “Enter a positive number: “Get number from userWhile number < 0

Print “Sorry, too low. Try again:”Get number from user

End While

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

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