CS 1400 Course Reader Introduction
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
from a shampoo bottle…
lather
rinse
repeat
– algorithm critique:• too ambiguous• doesn’t end
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
What is a computer program?
• A computer program is just an algorithm expressed in computer language instructions
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
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.
Types of user interfaces
• Graphical
• Console
this semester
next semester!
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
Keywords
Get Else
Print End If
And Repeat
Or End Repeat
If While
End While
Operators
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
= (assignment)
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)
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)
C. Getting input
form: Get <variable> from user
• Examples:
Get price from user
(get the value for variable price from the user’s keyboard)
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
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.”
.
D. Conditions
form: If <comparison> <statements executed if true>
Else<statements executed if false>
End If
comparisons use: <, >, <=, >=, !=, ==, And, Or
Example
If age < 21Print “Sorry, you are too young”Print “Please try again next year”
ElsePrint “You may enter”
End If
D. Conditions (alternate)
form: If <comparison>
<statements executed if true>
End If
• Example:
If weight < 90
Print “Consider eating more protein”
End If
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.
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
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
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
Step 4: Test algorithm
Instr screen name hours payRate regularPay overtimePay
…
E. Repetition
form: Repeat <N> times
<statements to be repeated>
End Repeat
• Example:
Repeat 5 times
Print “Vote for Napoleon!”
End Repeat
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
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