computer programming tcp1224 chapter 3 completing the problem-solving process and getting started...
TRANSCRIPT
![Page 1: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/1.jpg)
Computer ProgrammingTCP1224
Chapter 3Completing the Problem-Solving Process and Getting Started with C++
![Page 2: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/2.jpg)
Week 2 – What we talked about•Analysis of a problem
•Input-Process-Output (IPO) Chart
•Algorithm
•Pseudocode and Flowchart
2
![Page 3: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/3.jpg)
Comments about tutorial• I am glad that many of you tried it, but many seem to be
unsure of what you were doing. From my limited observation;
▫ Many are probably thinking “what does this have to do with programming?”
▫ Some have already sounded out that programming may be difficult! (utter nonsense, it is very easy).
• What is programming?
▫ Syntax that is hard to remember?
▫ The need to learn another communication language?
▫ It’s that sequence, repetition and selection thing-a-mie-bob?
3
![Page 4: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/4.jpg)
UN-official view of programming• It is a set of simple to follow instructions for a dumb piece
of machinery that has to be spelt out step by step in simple to understand language.
▫ Most of us explain an issue or answer questions for people who have some intelligence and will understand our answers. E.g.,
Do you drink? Meaning “do you drink any alcoholic beverage”?
Where were you? Usually meaning, “I was looking for you all over and could not
find you, where were you at noon when I came to you room?”
4
![Page 5: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/5.jpg)
UN-official view of programming• Now, imagine that you need to explain to someone who
has no sign of intelligence.
▫ You will need to speak to that person sloooowwwwlyyyyyy … or actually, you need to speak to that person, word by word.
▫ So, same as how you would tell the computer, as in programming. You need to tell step by step … it is really dumb!
▫ So, back to the lecture … a computer can only execute instructions in sequence, is able to repeat (repetition) and you can tell it how to decide (selection).
5
![Page 6: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/6.jpg)
So, what’s this IPO chart for?• For you to tell the “intelligent-less” machine, what it
can work with (the input) to produce the results (output).
• The “processing” component is the step by step instructions for the computer.
▫ A computer has no intelligence, so even a simple thing like obtaining the input has to be told. E.g. “Enter the loan period”. It is better to maybe even say, “get input from keyboard for the loan period in months”.
▫ You will need to specify these steps.
▫ In your mathematics classes, you have often been told to answer showing the steps you used to derive the results.
6
![Page 7: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/7.jpg)
Math exampleA very popular math solving step …
Let x = y
x ( – y + y ) = y
x – y + y / ( x – y ) = y / ( x – y )
1 + y / ( x – y ) = y / ( x – y )
1 = ( y / ( x – y ) ) - ( y / ( x – y ) )
1 = 0
7
![Page 8: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/8.jpg)
Algorithm• In general, most of you need to be more detailed in your
algorithm.
• Clearly state where does the values used originate from and what each notation mean.
• Proof read it by making yourself “intelligent-less” and determine if what you wrote can produce the output required.
8
![Page 9: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/9.jpg)
9
![Page 10: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/10.jpg)
This week’s objectives?• Code an algorithm into a program (How to)
• Desk-check a program (as oppose to Desk-check the algorithm)
• Evaluate and modify a program
• Understand the components of a C++ program
• Create a C++ program
10
![Page 11: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/11.jpg)
Concept Lesson• More on the Problem-Solving Process
• Coding the Algorithm into a Program
• Desk-Checking the Program
• Evaluating and Modifying the Program
• Creating a C++ Program
11
![Page 12: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/12.jpg)
More on the Problem-Solving Process
12
![Page 13: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/13.jpg)
Coding the Algorithm into a Program
13
![Page 14: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/14.jpg)
Assigning Names, Data Types, and Initial Values to the IPO Items• To code algorithm (i.e. to be able to transform from the
algorithm to a program), first assign a name to each input, processing, and output item in IPO chart. By convention;▫ Names can contain only letters, numbers, and _ ▫ Cannot contain punctuation characters or spaces▫ Examples:
raise newPay
▫ Each input/processing/output item must have a data type
▫ You may initialize each item
14
usually in lowercase letters
use camel case if name contains multiple words
![Page 15: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/15.jpg)
Assigning Names, Data Types, and Initial Values to the IPO Items• Camel case
▫ The words are written without any spaces in between them, and the first letter of the 2nd and following words are capitalized.
▫ This creates humps on the words and hence are known as camel case.
▫ E.g. thisLectureIsBoring• Data type
▫ A computer stores information in bits and bytes and have limited variations. In each and every name given, the computer has to be told how to store them and how to use them.
▫ E.g. if it is a real number, the C++ language calls it a float or double. Why the difference?
• Initialize▫ Each time you create a new “variable”, it is just a name. More
often than not, you need to put something in there.
15
![Page 16: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/16.jpg)
Assigning Names, Data Types, and Initial Values to the IPO Items
16
double is a keyword
this is a statement
all C++ statements must end with a semicolon
![Page 17: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/17.jpg)
Translating the Algorithm into Code
17
cout: standard output streamcin: standard input stream<<: insertion operator>>: extraction operator
stream: sequence of characters, to perform standard I/O operations
a stream manipulator
![Page 18: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/18.jpg)
Review: The core C++ code
18
![Page 19: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/19.jpg)
Desk-Checking the Program
19
![Page 20: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/20.jpg)
Desk-Checking the Program (continued)
20
![Page 21: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/21.jpg)
Desk-Checking the Program (continued)
21
![Page 22: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/22.jpg)
Evaluating and Modifying the Program• Testing is running the program, with sample
data
▫Results should agree with desk-check ones!
• Debugging is locating/removing errors in program
▫Program errors are called bugs
22
![Page 23: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/23.jpg)
Evaluating and Modifying the Program• A syntax error occurs if an instruction violates
the programming language’s syntax (set of rules)
▫E.g., cout < "Hello";
• A logic error occurs if an instruction does not give the expected results
▫E.g., average = number1 + number2 / 2;
23
![Page 24: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/24.jpg)
Creating a C++ Program
24
source file: Ch3Lab2.cpp
object file: Ch3Lab2.obj
executable file: Ch3Lab2.exe
created using an IDE or a general-purpose editor
![Page 25: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/25.jpg)
An Introduction to Programming with C++,
Fifth Edition
25
![Page 26: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/26.jpg)
Summary• Fourth step in the problem-solving process is
to code the algorithm into a program• In C++, you perform standard I/O operations
using streams (sequences of characters)▫cout and cin▫Insertion operator (<<) sends data to output
stream▫Extraction operator (>>) gets data from input
stream• After coding the algorithm, you desk-check
program• Final step in the problem-solving process is
to evaluate and modify (if necessary) the program
26
![Page 27: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/27.jpg)
Summary (continued)• Some programs have errors, called bugs
▫Syntax error occurs when an instruction violates one of the rules of the programming language’s syntax
▫Logic error occurs when you enter an instruction that does not give you the expected results
▫You debug to locate and remove errors• To create and execute a C++ program, you
need to have a text editor and a C++ compiler▫Compiler translates source code into object
code▫Linker produces executable file
27
![Page 28: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/28.jpg)
Summary (continued)• Comments are internal documentation of
programs• C++ programs typically include at least one
directive• using statements tell compiler where it can
find the definition of certain keywords• A function is a block of code that performs a
task▫main() is where the execution of program
begins• The first line in a function is called the
header▫After the header comes the body, enclosed in
braces
28
![Page 29: Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++](https://reader036.vdocuments.us/reader036/viewer/2022070410/56649ebd5503460f94bc64ce/html5/thumbnails/29.jpg)
29