Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

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
kashif.rajpoot@seecs.edu.pk

Problem-solving
o

Humans are basically problem-solving machines

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?

For simpler problems, we do it unconsciously. For more


complex problems, we have to go through a thought
process.

Animals are not, they typically remain in a situation they are


born in. Humans learn to improve their situation by solving
problems at hand.

Imagine you got admission in SEECS and your parents were


against you moving to out-city. What to do?

Programming is used to solve problems by computers.


2

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

Steps to devise a computer solution


1.
2.
3.
4.
5.
6.

Analyze the problem


Plan the algorithm
Desk-check the algorithm
Code the algorithm
Desk-check the program
Evaluate and modify (if required) the program
3

1. Analyze the Problem


o

o
o

You cannot solve a problem unless you


understand it
You cannot understand a problem unless you
analyze it
Identify important components of a problem
Two most important components of a problem
are:

Input the items needed to achieve the goal


Output the goal of solving the problem
4

1. Analyze the Problem

To identify the outputs, ask the question:

To identify the inputs, ask the question:

What does the user want to see displayed on the


screen, printed on the paper, or stored in a file?
What information will the computer need to know to
determine the outputs?

Draw an IPO chart

Input, Processing, Output


5

Hints for Analyzing Problems


o
o

Analysis step is the most difficult of problemsolving steps


To analyze the problem in order to understand it:

o
o

Try reading the problem description several times


Ask the user for more information

The more you understand the problem, the easier


it is to solve the problem and devise the solution
Cross out unimportant information, focus only on
the important information

1. Analyzing the Problem


o

Instances where the input may not be explicitly


stated:

There may be instances where some


information may not be available:

1. Analyze the Problem

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.

2. Plan the Algorithm


o

Complete the IPO chart, using pseudocode

10

2. Plan the Algorithm


o

Complete the IPO chart, using the flowchart

11

2. Plan the Algorithm


o
o

Minor modification in the algorithm


Processing items: items that hold intermediate
values in the processing from input to output

12

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
Desk-checking: programmer reviews the
algorithm by sitting at his desk
A set of sample data is chosen, fed to the
algorithm as input, and output data from the
algorithm is verified

13

3. Desk-Check the Algorithm

First of all, manually determine the solution (i.e.


not using an algorithm)
Desk-check table:

Do the processing, compute total bill without liquor

Do the processing, compute the tip amount

14

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++)
Use the information in the IPO chart to code

Assign a descriptive name to each input, processing, or


output item

You know the inputs


You know the processing to be performed
You know the outputs

You have to assign a data type to each item


Often, it is advised to initialize each item with a value.

The name and data type are used to store the input in
computers memory while the program is executed.
15

Internal Memory
o
o
o

Internal memory, the RAM, is holding the


instructions and data to do any processing
Each memory location has an address
Each location can
hold one item
at a time
Each location can
hold one type of
data (number,
text, character,
whole-number, etc)
16

Internal Memory
o
o
o

o
o

Some of the memory locations are automatically filled


(OS instructions, program instructions, etc)
Programs can request to reserve some memory
locations to store input, processing, or output data items
In C++, a memory location is reserved (declaration) by
assigning a name, data type, and (optionally) an initial
value to it
The name allows us to access one or more memory
locations using a memorable descriptive name
The data type indicates the type of data to be stored in
the memory location: for example, number or text

17

Internal Memory
o

There are two types of memory locations a


program can reserve
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

Named constant

A memory location whose value is initially set and


cannot change over time during program execution
E.g. Value of Pi, earths gravity
18

Selecting a Name for a Memory Location

19

Revisiting the Treyson Mobley Problem


o

Note how many memory locations we need to reserve?

20

Revisiting the Treyson Mobley Problem

21

Internal Memory

22

Select a Data Type for a Memory Location


o
o
o

Memory locations come in different types and sizes


The type we choose depends upon what we want to
store in that location
Each of this data type is a keyword

23

Select a Data Type for a Memory Location


o

double is a keyword used to create variables of


double data type

24

How Data is Stored in Internal Memory


o
o

Knowing this will help you understand the importance of


memory locations data type
Numbers are stored in computer memory using the binary
(or base 2) number system

This is considerably different than decimal number system

Lets recall the decimal number system

Numbers are stored in the units of increasing power of the base


(10)

25

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

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

ASCII code system assigns a specific numeric


code to each LETTER on your keyboard

27

How Data is Stored in Internal Memory

28

How Data is Stored in Internal Memory

29

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

Selecting an Initial Value for a Memory Location


o

Treyson Mobley problem

31

Declaring a Memory Location


o

Memory locations are reserved by using a specific


C++ statement

This is also called creation or declaration of variable

Note the use of reserved keywords

32

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

Declaring a Memory Location


o

Note the use of reserved keyword const

34

Declaring a Memory Location

35

Finishing Step-4 in Problem-Solving Process


o

We have identified the input, processing, and


output items in the IPO chart
We have declared/reserved memory locations
for these items, specifying data types and
initializing values
The status of IPO chart now is:

36

Finishing Step-4 in Problem-Solving Process


o

Having declared the memory locations, let us focus on coding the algorithm

37

Getting Data from the Keyboard


o
o
o
o
o

In C++, objects are used for standard input and


output operations
Objects handling input/output are called stream
objects, because they handle streams
Stream a sequence of characters
Standard input stream object cin (see in)
cin tells the computer to pause program
execution till the input is entered

The cin object temporarily holds input to itself

Extraction operator >> is used to extract (transfer)


the input from stream to memory location
38

Getting Data from the Keyboard


o
o

Input is received by the cin object, extracted by the >>


operator, transferred to the memory location
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

Getting Data from the Keyboard

40

Getting Data from the Keyboard


o

Treyson
Mobley
problem:
calculating
the tip
The program,
written so far,
can read the
input items
Importance of
prompt

41

Displaying Message on Computer Screen


o

Similar to cin object, there is a stream object


cout (see out) for sending output to the display
cout is used with the insertion operator << to
display information on computer screen
The information sent out to display can be any
combination of literal constants, variables, and
text messages

42

Displaying Message on Computer Screen


o
o
o

Note the mixing of variables and strings


Note the use of multiple insertion << operators
endl the stream manipulator

43

Treyson Mobley Problem


o

Note the display


prompt
statements
before each cin
statement
This is to inform
the user about
the item to be
entered
44

Getting Input and Displaying Output in C++

45

Arithmetic Operators in C++


o
o

o
o

In the Treyson Mobley pseudocode, instructions 2


and 3 require arithmetic operations to calculate tip
Arithmetic calculations are performed by writing
arithmetic expressions that contain arithmetic
operators
Standard arithmetic operators in C++ are:

Unary and binary operators


Modulus operator
46

Type Conversion in Arithmetic Expressions


o

Computer makes implicit type conversion when


processing arithmetic expressions
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
Value is returned to its original type after the
operation is completed

47

Type Conversion in Arithmetic Expressions

48

Type Conversion in Arithmetic Expressions


o

What happens when an integer is divided by


another integer

Convert one of the integers in the operation to


a real number

o
o

E.g. 24/5, the result is an integer as well


How to get the quotient as a real number?

24.0/5 or 24/5.0, the result is a real number

What if the numbers to be operated on are


variables?
Explicit type conversion
49

The static_cast operator


o

o
o

static_cast
operator
explicitly
converts data
from one data
type to
another
firstNum: 5
secondNum: 2
50

The static_cast operator

51

Assignment Statement
o

After arithmetic operation, the result is often


stored (assigned) to a variable

We do this by using assignment statement

52

Assignment Statement

53

Treyson Mobley Problem

54

Type Casting and Assignment Expression

55

5. Desk-Check the Program


o

Once the algorithm has been coded, we can


desk-check it to verify the correctness
We can use the same sample data used to
desk-check the algorithm

56

5. Desk-Check the Program

57

5. Desk-Check the Program

58

6. Evaluate and Modify the Program


o

o
o

Enter C++ programs into computer and execute


it
Enter the same sample input, verify the results
If the results differ, it indicates program
contains errors bugs
Bugs must be located and removed
debugging
Logical errors
59

6. Evaluate and Modify the Program


o

C++ instructions are entered in a text editor

Compile the source code

The instructions entered are called source code


Source code is saved as a file with extension .cpp
Translated into 0s and 1s (machine code, binary
code, object code)
Output of compiler is called object file

Linker combines the source and required


library code

Produces an executable file (.exe extension)


60

6. Evaluate and Modify the Program


o

IDE integrated
development
environment,
includes:

o
o

Editor
Compiler
Linker
Debugger

Visual C++
Dev C++
61

6. Evaluate and Modify the Program


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.

62

6. Evaluate and Modify the Program

0 indicates successful termination.


Comments

63

Arithmetic Assignment Operator


o

Abbreviates an
assignment
statement that
contains an
arithmetic
operator
Shorthand
notation

64

65

Reference to Book
o

Chapters 2, 3, and 4; An Introduction to


Programming with C++ - Diane Zak

66

You might also like