![Page 1: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/1.jpg)
CS 1400
Jan 10, 2007
![Page 2: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/2.jpg)
What is it?
• Computer Science is the art and science of writing programs– Users use programs– Programmers create these programs
• A program is a logical sequence of instructions that leads to a correct solution to a problem.
• Programmers are story-problem solvers!
![Page 3: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/3.jpg)
Successful Problem SolversSuccessful Problem Solvers
• have or acquire the necessary subject matter knowledge • have a positive, objective attitude, viewing the activity as a learning
opportunity rather than a “test” of their intelligence• are active• begin with what they understand • draw on other information and sources of knowledge• refer to the problem statement frequently• employ sequential analysis when a question is initially unclear• ask themselves questions and think out loud – (we call this "self
talk")• brainstorm• use physical aids to thinking, such as drawing pictures• relate problems to familiar / concrete experiences or to previous
problems• carefully proceed through a series of steps to reach a conclusion
![Page 4: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/4.jpg)
Less Successful Problem SolversLess Successful Problem Solvers
• lack necessary subject-matter knowledge and are willing to allow gaps of knowledge to exist
• have a poor attitude: an attitude of indifference towards achieving a complete, accurate comprehension of situations and relations
• demonstrate a lack of objectivity in dealing with problems • are passive in their thinking• have great difficulty in ascertaining what they are required to do• grope blindly toward a solution• fail to complete a chain of reasoning• use one-shot thinking, rather than building an extended, sequential
path to understanding• rush through or skip instructions• take little time to consider a question or break it down into its
component parts• are mentally careless and superficial• select an answer on the basis of a feeling or guess
![Page 5: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/5.jpg)
Example Program… washing hair
• 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 into hair until lather covers hair• Rinse hair until all lather is removed• Repeat the previous eight steps one more time
![Page 6: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/6.jpg)
Programs vs. Algorithms
• A program contains an algorithm
• A program must be in a certain format– -- think of a business letter!
![Page 7: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/7.jpg)
Algorithms in Computer Science
1. The instructions must be well ordered.
2. The algorithm must be unambiguous -- clear and incapable of being misunderstood.
3. The process must eventually end.
4. The requested action must be "doable.“
5. The algorithm must produce a result.
![Page 8: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/8.jpg)
Program DevelopmentStep 1 - Analyze the problem• InputInput: : Identify what information the program will have
to retrieve or will be provided to the program by the user.
• Values given or known values: Identify any values that appear in the problem statement. These may or may not be used in your solution. Also watch for values or formulas that are not given but that will need to be used to complete the solution.
• Processing: What will need to be done to produce the desired output? It is important at this stage to focus on what needs to be done, not how it will be accomplished.
• Output : Identify what is to be produced by this program. This will be a list of the desired results.
![Page 9: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/9.jpg)
Example…
“Write a program to calculate the volume of a box, using dimensions provided by the user.”
Input: height, width, depth of box
Given values: none
Processing: calculate volume
Output: volume of the box
![Page 10: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/10.jpg)
Step 2 – Outline the solution
• List steps to be performed in correct order – not concerned with details at this point.
Get height, width and depth of the box from user
Calculate the volume of the box
Output the dimensions entered and the volume of the box
![Page 11: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/11.jpg)
Step 3 – Develop an algorithm in pseudocode
Print “Enter the height of the box: “
Get height from user
Print “Enter the width of the box: “
Get width from user
Print “Enter the depth of the box: “
Get depth from user
volume = height * width * depth
Print “The volume of the box is “
Print volume
![Page 12: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/12.jpg)
Step 4 – Test the algorithm
• Pretend you are the computer – hand tracing
Step 5 – Code the algorithm into C++
Step 6 – Run and test the program on the computer
Step 7 – Document the program
![Page 13: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/13.jpg)
Programming Language ElementsProgramming Language Elements
• Punctuation and spelling
• Syntax and grammar
• Semantics
Pseudocode allows us to start writing Pseudocode allows us to start writing algorithms without worrying about these algorithms without worrying about these issues.issues.
![Page 14: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/14.jpg)
English-like Pseudocode
Key Words – these words have special meaningKey Words – these words have special meaning• Get • Print• And• Or• If• Else• End If• Repeat• End Repeat• While• End While
![Page 15: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/15.jpg)
English-like Pseudocode
• VariablesVariables – one-word name representing a value or a place where a value can be stored
Think of a mailbox: – The name on the mailbox represents a
variable name, – The contents of the mailbox represent the
variable value.A variable only holds one value at a time!A variable only holds one value at a time!
![Page 16: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/16.jpg)
English-like Pseudocode
• OutputOutputPrint “message”
Print variable
• InputInputGet variable from user
![Page 17: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/17.jpg)
English-like Pseudocode
• OperatorsOperatorsArithmetic: Arithmetic: **, , ++, , --, , //
Assignment: Assignment: ==
• CalculationsCalculationsVariable = arithmetic expression
(Arithmetic expressions may only use known values and variables)
![Page 18: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/18.jpg)
Example:
“Write a program to calculate the volume of a box, using dimensions provided by the user.”
Print “Enter height, width, and length of box:”
Get height from user
Get width from user
Get length from user
volume = height * width * length
Print “Volume is: “
Print volume
![Page 19: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/19.jpg)
More Examples:
• “Write a program to calculate a paycheck, using hours worked and a payrate provided by the user”
• “Write a program to convert a temperature provided by the user in degrees Fahrenheit into degrees Centigrade”
• “Write a program to calculate the circumference of a circle provided by the user”
![Page 20: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/20.jpg)
Control Structures
• Sequence
• Selection (or decision)
• Repetition
![Page 21: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/21.jpg)
Selection
If condition
Statements…
ElseStatements…
End If
![Page 22: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/22.jpg)
Conditions
• A condition is a comparison question whose answer is either true or false (Boolean).
• The comparison operators are;< less than > greater than
<= less than or equal >= greater than or equal
== equal != not equal
![Page 23: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/23.jpg)
Example Selections
If age < 18
Print “you cannot vote”
Else
Print “you can vote!”
End If
If quantity >= 26
Print “sufficient quantity is available: ”
Print quantity
End If
![Page 24: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/24.jpg)
Example algorithm• Write a program to calculate an employee
paycheck using time-and-a-half for overtime
Input: hours, payrate
Given values: over 40 hours is overtime, overtime rate is 1.5 of
payrate
Processing: calculate regular pay, overtime pay, and total pay
Output: total pay
![Page 25: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/25.jpg)
OutlineGet hours_worked and payrate from user
If there was overtime, calculate pay using payrate for first 40 hours and 1½ payrate for additional hours
Otherwise, calculate pay at payrate
Print pay
![Page 26: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/26.jpg)
PseudocodePrint “Enter hours worked: “Get hours from userPrint “Enter pay rate: “Get payrate from userOvertime_rate = payrate * 1.5If hours > 40
pay = 40 * payrate + (hours-40) * over_timerateElse
pay = hours * payrateEnd IfPrint pay
![Page 27: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/27.jpg)
Repetition
• The counted loopRepeat N times
Statements…End Repeat
• The condition-controlled loopWhile condition
Statements…End While
![Page 28: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/28.jpg)
Examples
• “Write a program to output the squares of each number input by the user until the user inputs a negative number”
• “Write a program to accept 10 test scores from the user and output the counts of passing (>=60) and failing scores”
• “Write a program to output the squares of the integers 1 through 5”
![Page 29: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/29.jpg)
“Write a program to output the squares of the integers 1 through 5”
Inputs: none
Given values:integers 1 through 5
Processing: calculation squares
Outputs: squares of integers 1 through 5
![Page 30: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/30.jpg)
Outline…
Start N as 1
As long as N is less than or equal to 5, repeat;
Calculate and print the square of n
Add 1 to n
![Page 31: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/31.jpg)
N = 1
While N <= 5
square = N * N
Print “The square of “, N, “ is “, square
N = N+1
End While
“Write a program to output the squares of the integers 1 through 5”
![Page 32: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/32.jpg)
Print “The square of 1 is 1”
square2 = 2 * 2
Print “The square of 2 is “, square2
square3 = 3 * 3
Print “The square of 3 is “, square3
square4 = 4 * 4
Print “The square of 4 is “, square4
square5 = 5 * 5
Print “The square of 5 is “, square5
Why not this?
![Page 33: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/33.jpg)
“Write a program to accept 10 test scores from the user and output the counts of
passing (>=60) and failing scores”
Inputs: a list of 10 test scores
Give values: a count of 10, a passing score of 60
Processing: find a count of passing and failing test scores
Outputs: count of passing/failing scores
![Page 34: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/34.jpg)
Outline…
Start the counts of passing and failing scores as 0
Repeat the following 10 times;
prompt the user and get a score
if this score is passing
add 1 to the passing count
otherwise
add 1 to the failing count
Print the counts of passing and failing scores
![Page 35: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/35.jpg)
passing = 0failing = 0Repeat 10 times
Print “Enter a test score: “Get score from userIf score >= 60
passing = passing + 1Else
failing = failing + 1End If
End RepeatPrint “Passing: “, passing, “ Failing: “, failing
![Page 36: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/36.jpg)
“Write a program to output the squares of each number input by the user until the user inputs a
negative number”
Inputs: a list of positive numbers followed by a negative number
Given values:none
Processing: square of each positive number
Outputs: calculated squares
![Page 37: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/37.jpg)
Outline…
Prompt the user and get the first number
Repeat as long as the number is positive;
calculate the square of this number
Print the square
Prompt the user and get the next number
![Page 38: CS 1400 Jan 10, 2007. What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A](https://reader036.vdocuments.net/reader036/viewer/2022062516/56649d7e5503460f94a61e61/html5/thumbnails/38.jpg)
Print “Enter the first number: “
Get number from user
While number > 0
Square = number * number
Print “Square is: “, square
Print “Enter the next number: “
Get number from user
End While
“Write a program to output the squares of each number input by the user until the user inputs a
negative number”