2 - the problem-solving process
DESCRIPTION
SOLVING C++TRANSCRIPT
![Page 1: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/1.jpg)
Fundamentals of Computer Programming
(CS110)
BESE-2
2 – The Problem-Solving Process
Dr. Kashif Mahmood Rajpoot
Assistant Professor,
DoC, NUST-SEECS.
http://seecs.nust.edu.pk/faculty/kashif.html
![Page 2: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/2.jpg)
Problem-solving o Humans are basically problem-solving machines
• Animals are not, they typically remain in a situation they are born in. Humans learn to improve their situation by solving problems at hand.
o We use problem-solving process every day in our daily lives. • How to get to school?
• What to do when you are hungry?
• What to do if you get low grades in a subject?
o For simpler problems, we do it unconsciously. For more complex problems, we have to go through a thought process. • Imagine you got admission in SEECS and your parents were
against you moving to out-city. What to do?
o Programming is used to solve problems by computers. 2
![Page 3: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/3.jpg)
Creating Computer Solutions to Problems
o You basically devise a solution using a computer program to solve a problem
• Salary payment system at a large organization
o Steps to devise a computer solution
1. Analyze the problem
2. Plan the algorithm
3. Desk-check the algorithm
4. Code the algorithm
5. Desk-check the program
6. Evaluate and modify (if required) the program
3
![Page 4: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/4.jpg)
1. Analyze the Problem
o You cannot solve a problem unless you
understand it
o You cannot understand a problem unless you
analyze it
o Identify important components of a problem
o Two most important components of a problem
are:
• Input – the items needed to achieve the goal
• Output – the goal of solving the problem
4
![Page 5: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/5.jpg)
1. Analyze the Problem
o To identify the outputs, ask the question:
• What does the user want to see displayed on the
screen, printed on the paper, or stored in a file?
o To identify the inputs, ask the question:
• What information will the computer need to know to
determine the outputs?
o Draw an IPO chart
• Input, Processing, Output
5
![Page 6: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/6.jpg)
Hints for Analyzing Problems
o Analysis step is the most difficult of problem-solving steps
o To analyze the problem in order to understand it: • Try reading the problem description several times
• Ask the user for more information
o The more you understand the problem, the easier it is to solve the problem and devise the solution
o Cross out unimportant information, focus only on the important information
6
![Page 7: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/7.jpg)
1. Analyzing the Problem
o Instances where the input may not be explicitly
stated:
o There may be instances where some
information may not be available:
7
![Page 8: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/8.jpg)
1. Analyze the Problem
8
![Page 9: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/9.jpg)
2. Plan the Algorithm
o Plan the algorithm to transform the available
input into the desired output
• This is the processing that is done on the input
data.
• Planning the algorithm: finding the step-by-step set
of instructions to solve the problem
• We can use pseudocode or flowchart to develop the
algorithm.
9
![Page 10: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/10.jpg)
2. Plan the Algorithm
o Complete the IPO chart, using pseudocode
10
![Page 11: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/11.jpg)
2. Plan the Algorithm
o Complete the IPO chart, using the flowchart
11
![Page 12: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/12.jpg)
2. Plan the Algorithm
o Minor modification in the algorithm
o Processing items: items that hold intermediate
values in the processing from input to output
12
![Page 13: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/13.jpg)
3. Desk-Check the Algorithm
o Once the algorithm has been developed, before
coding it in a computer language, we need to
verify whether it works correctly or not
o Desk-checking: programmer reviews the
algorithm by sitting at his desk
o A set of sample data is chosen, fed to the
algorithm as input, and output data from the
algorithm is verified
13
![Page 14: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/14.jpg)
3. Desk-Check the Algorithm
o First of all, manually determine the solution (i.e.
not using an algorithm)
o Desk-check table:
o Do the processing, compute total bill without liquor
o Do the processing, compute the tip amount
14
![Page 15: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/15.jpg)
4. Code the Algorithm
o Once the algorithm has been developed and has been desk-checked for correctness, one can move to the 4th step of coding the algorithm in a specific language (e.g. C++)
o Use the information in the IPO chart to code • You know the inputs
• You know the processing to be performed
• You know the outputs
o Assign a descriptive name to each input, processing, or output item • You have to assign a data type to each item
• Often, it is advised to initialize each item with a value.
o The name and data type are used to store the input in computer’s memory while the program is executed.
15
![Page 16: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/16.jpg)
Internal Memory
o Internal memory, the RAM, is holding the instructions and data to do any processing
o Each memory location has an address
o Each location can hold one item at a time
o Each location can hold one type of data (number, text, character, whole-number, etc)
16
![Page 17: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/17.jpg)
Internal Memory
o Some of the memory locations are automatically filled (OS instructions, program instructions, etc)
o Programs can request to reserve some memory locations to store input, processing, or output data items
o In C++, a memory location is reserved (declaration) by assigning a name, data type, and (optionally) an initial value to it
o The name allows us to access one or more memory locations using a memorable descriptive name
o The data type indicates the type of data to be stored in the memory location: for example, number or text
17
![Page 18: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/18.jpg)
Internal Memory
o There are two types of memory locations a program can reserve
o Variable
• A memory location whose value can vary during runtime as the program executes and progresses
• Most of the memory locations declared in a program are variable
o Named constant
• A memory location whose value is initially set and cannot change over time during program execution
• E.g. Value of Pi, earth’s gravity
18
![Page 19: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/19.jpg)
Selecting a Name for a Memory Location
19
![Page 20: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/20.jpg)
Revisiting the Treyson Mobley Problem
o Note how many memory locations we need to reserve?
20
![Page 21: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/21.jpg)
Revisiting the Treyson Mobley Problem
21
![Page 22: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/22.jpg)
Internal Memory
22
![Page 23: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/23.jpg)
Select a Data Type for a Memory Location
o Memory locations come in different types and sizes
o The type we choose depends upon what we want to store in that location
o Each of this data type is a keyword
23
![Page 24: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/24.jpg)
Select a Data Type for a Memory Location
o double is a keyword used to create variables of
double data type
24
![Page 25: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/25.jpg)
How Data is Stored in Internal Memory
o Knowing this will help you understand the importance of memory location’s data type
o Numbers are stored in computer memory using the binary (or base 2) number system • This is considerably different than decimal number system
o Let’s recall the decimal number system • Numbers are stored in the units of increasing power of the base
(10)
25
![Page 26: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/26.jpg)
How Data is Stored in Internal Memory
o Similar to the decimal number system, we have
units increasing in the powers of base (2) in the
binary number system
26
![Page 27: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/27.jpg)
How Data is Stored in Internal Memory
o Unlike numeric data, character data
represented in internal memory in the form of
ASCII (pronounced ASK-ee) codes
• American Standard Code for Information
Interchange
o ASCII code system assigns a specific numeric
code to each LETTER on your keyboard
27
![Page 28: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/28.jpg)
How Data is Stored in Internal Memory
28
![Page 29: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/29.jpg)
How Data is Stored in Internal Memory
29
![Page 30: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/30.jpg)
Selecting an Initial Value for a Memory Location
o In addition to assigning name and data type to
a memory location, it is advised to assign it an
initial value
• Memory locations are initialized using literal
constants
• Literal constant is not a memory variable
• Literal constant is a specific value of a specific data
type
30
![Page 31: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/31.jpg)
Selecting an Initial Value for a Memory Location
o Treyson Mobley problem
31
![Page 32: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/32.jpg)
Declaring a Memory Location
o Memory locations are reserved by using a specific C++ statement • This is also called creation or declaration of variable
o Note the use of reserved keywords
32
![Page 33: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/33.jpg)
Declaring a Memory Location
o Once a variable has been declared, you can
now refer to the variable name to use it later in
a program for input, processing, or output
33
![Page 34: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/34.jpg)
Declaring a Memory Location
o Note the use of reserved keyword const
34
![Page 35: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/35.jpg)
Declaring a Memory Location
35
![Page 36: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/36.jpg)
Finishing Step-4 in Problem-Solving Process
o We have identified the input, processing, and
output items in the IPO chart
o We have declared/reserved memory locations
for these items, specifying data types and
initializing values
o The status of IPO chart now is:
36
![Page 37: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/37.jpg)
Finishing Step-4 in Problem-Solving Process
37
o Having declared the memory locations, let us focus on coding the algorithm
![Page 38: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/38.jpg)
Getting Data from the Keyboard
o In C++, objects are used for standard input and output operations
o Objects handling input/output are called stream objects, because they handle streams
o Stream – a sequence of characters
o Standard input stream object – cin (see in)
o cin – tells the computer to pause program execution till the input is entered
• The cin object temporarily holds input to itself
o Extraction operator >> is used to extract (transfer) the input from stream to memory location
38
![Page 39: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/39.jpg)
Getting Data from the Keyboard
o Input is received by the cin object, extracted by the >> operator, transferred to the memory location
o Extraction operator stops reading input from cin object once it reads a white-space character (newline, tab, space) • Cin is mainly used to input numeric and character data, but
not for string data with spaces (e.g. Computer programming)
39
![Page 40: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/40.jpg)
Getting Data from the Keyboard
40
![Page 41: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/41.jpg)
Getting Data from the Keyboard
41
o Treyson Mobley problem: calculating the tip
o The program, written so far, can read the input items
o Importance of ‘prompt’
![Page 42: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/42.jpg)
Displaying Message on Computer Screen
o Similar to cin object, there is a stream object
cout (see out) for sending output to the display
o cout is used with the insertion operator << to
display information on computer screen
o The information sent out to display can be any
combination of literal constants, variables, and
text messages
42
![Page 43: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/43.jpg)
Displaying Message on Computer Screen
o Note the mixing of variables and strings
o Note the use of multiple insertion << operators
o endl – the stream manipulator
43
![Page 44: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/44.jpg)
Treyson Mobley Problem
44
o Note the display
prompt
statements
before each cin
statement
o This is to inform
the user about
the item to be
entered
![Page 45: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/45.jpg)
Getting Input and Displaying Output in C++
45
![Page 46: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/46.jpg)
Arithmetic Operators in C++
o In the Treyson Mobley pseudocode, instructions 2 and 3 require arithmetic operations to calculate tip
o Arithmetic calculations are performed by writing arithmetic expressions that contain arithmetic operators
o Standard arithmetic operators in C++ are:
o Unary and binary operators
o Modulus operator 46
![Page 47: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/47.jpg)
Type Conversion in Arithmetic Expressions
o Computer makes implicit type conversion when
processing arithmetic expressions
o Performing arithmetic operations with two
values having different data type, the value
with lower-ranking type is always promoted,
temporarily, to the higher-ranking type
o Value is returned to its original type after the
operation is completed
47
![Page 48: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/48.jpg)
Type Conversion in Arithmetic Expressions
48
![Page 49: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/49.jpg)
Type Conversion in Arithmetic Expressions
o What happens when an integer is divided by another integer
• E.g. 24/5, the result is an integer as well
• How to get the quotient as a real number?
o Convert one of the integers in the operation to a real number
• 24.0/5 or 24/5.0, the result is a real number
o What if the numbers to be operated on are variables?
o Explicit type conversion
49
![Page 50: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/50.jpg)
The static_cast operator
o static_cast
operator
explicitly
converts data
from one data
type to
another
o firstNum: 5
o secondNum: 2
50
![Page 51: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/51.jpg)
The static_cast operator
51
![Page 52: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/52.jpg)
Assignment Statement o After arithmetic operation, the result is often
stored (assigned) to a variable
• We do this by using assignment statement
52
![Page 53: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/53.jpg)
Assignment Statement
53
![Page 54: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/54.jpg)
Treyson Mobley Problem
54
![Page 55: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/55.jpg)
Type Casting and Assignment Expression
55
![Page 56: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/56.jpg)
5. Desk-Check the Program
o Once the algorithm has been coded, we can
desk-check it to verify the correctness
o We can use the same sample data used to
desk-check the algorithm
56
![Page 57: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/57.jpg)
5. Desk-Check the Program
57
![Page 58: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/58.jpg)
5. Desk-Check the Program
58
![Page 59: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/59.jpg)
6. Evaluate and Modify the Program
o Enter C++ programs into computer and execute
it
o Enter the same sample input, verify the results
o If the results differ, it indicates program
contains errors – bugs
o Bugs must be located and removed –
debugging
o Logical errors
59
![Page 60: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/60.jpg)
6. Evaluate and Modify the Program
o C++ instructions are entered in a text editor
• The instructions entered are called source code
• Source code is saved as a file with extension .cpp
o Compile the source code
• Translated into 0s and 1s (machine code, binary code, object code)
• Output of compiler is called object file
o Linker combines the source and required library code
• Produces an executable file (.exe extension)
60
![Page 61: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/61.jpg)
6. Evaluate and Modify the Program
61
o IDE – integrated
development
environment,
includes:
• Editor
• Compiler
• Linker
• Debugger
o Visual C++
o Dev C++
![Page 62: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/62.jpg)
6. Evaluate and Modify the Program
62
Comments,
ignored by
compiler
Include the contents of iostream (library responsible for input and
output) in this program
Tells the program where to find, in the library, the definition of
standard keywords and classes. Namespace is a grouping of code.
Function, collection of piece of code. Execution of a C++ program
starts from the main function. ‘int’ is the data type of value returned.
![Page 63: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/63.jpg)
6. Evaluate and Modify the Program
63
Comments
‘0’ indicates successful termination.
![Page 64: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/64.jpg)
Arithmetic Assignment Operator
o Abbreviates an assignment statement that contains an arithmetic operator
o Shorthand notation
64
![Page 65: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/65.jpg)
65
![Page 66: 2 - The Problem-Solving Process](https://reader034.vdocument.in/reader034/viewer/2022042717/55cf92f5550346f57b9aa471/html5/thumbnails/66.jpg)
Reference to Book
o Chapters 2, 3, and 4; An Introduction to
Programming with C++ - Diane Zak
66