2 - the problem-solving process

Post on 25-Dec-2015

35 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

SOLVING C++

TRANSCRIPT

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

• 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

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

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

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

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

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

1. Analyze the Problem

8

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

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 Minor modification in the algorithm

o 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

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

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

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

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

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

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

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 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

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 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

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

o 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

o 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

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

Finishing Step-4 in Problem-Solving Process

37

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

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

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

Getting Data from the Keyboard

40

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’

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

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

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

Getting Input and Displaying Output in C++

45

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

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

Type Conversion in Arithmetic Expressions

48

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

The static_cast operator

o static_cast

operator

explicitly

converts data

from one data

type to

another

o firstNum: 5

o 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

o 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 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

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

6. Evaluate and Modify the Program

61

o IDE – integrated

development

environment,

includes:

• Editor

• Compiler

• Linker

• Debugger

o Visual C++

o Dev C++

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.

6. Evaluate and Modify the Program

63

Comments

‘0’ indicates successful termination.

Arithmetic Assignment Operator

o Abbreviates an assignment statement that contains an arithmetic operator

o Shorthand notation

64

65

Reference to Book

o Chapters 2, 3, and 4; An Introduction to

Programming with C++ - Diane Zak

66

top related