CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

46
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah

description

Introduction to C++ USEG20/Saidatul Rahah 3 Modular Program: a program consisting of interrelated segments arranged in a logical and understandable form  Easier to develop, correct, and modify than other kinds of programs Module: a small segment which is designed to perform a specific task  A group of modules is used to construct a modular program

Transcript of CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Page 1: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

CHAPTER 2PROBLEM SOLVING USING C++

1

C++ Programming

PEG200/Saidatul Rahah

Page 2: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Objectives

Introduction to C++ : Identifier, keyword, mnemonic

Programming StyleData TypesArithmetic OperationsVariables and Declaration Statements

2

USEG20/Saidatul Rahah

Page 3: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++

USEG20/Saidatul Rahah

3

Modular Program: a program consisting of interrelated segments arranged in a logical and understandable form Easier to develop, correct, and modify than other

kinds of programsModule: a small segment which is designed

to perform a specific task A group of modules is used to construct a modular

program

Page 4: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++(continued)

USEG20/Saidatul Rahah

4

Modules in C++ can be classes or functions: Function: accepts an input and produces an output

by processing the input in some fashion A function’s processing is encapsulated and hidden

within the function Class: contains both data and functions used to

manipulate the data Function: encapsulates a set of operations, while a class

encapsulates data plus one or more sets of operations

Page 5: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++:Identifier

USEG20/Saidatul Rahah

5

Identifier: a name given to an element of the language, such as a class or function

Rules for forming identifier names: First character must be a letter or underscore Only letters, digits, or underscores may follow the

initial letter (no blanks allowed) Keywords cannot be used as identifiers Max length of an identifier = 1024 characters

Use underscores to separate multiple words in a name, or capitalize the first letter of each word.

Page 6: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++:Keyword

USEG20/Saidatul Rahah

6

Keyword: a reserved name that represents a built-in object or function of the language

Page 7: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++:Identifier (continued)

USEG20/Saidatul Rahah

7

Identify valid C++ identifiers below: degToRad 1AB3 addNums while slope findMax E*6

valid

valid

valid

valid

invalid begins with a number

invalid contains a special character

invalid this is a keyword

Page 8: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++:Mnemonic

USEG20/Saidatul Rahah

8

Function names Require a set of parentheses at the end Can use mixed upper and lower case Should be meaningful, or be a mnemonic

Mnemonic: a word designed as a memory aidExamples of function names:areaCircle() calAverage() main()

Note that C++ is a case-sensitive language!

Page 9: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++: The main() Function

USEG20/Saidatul Rahah

9

Overall structure of a C++ program contains one function named main(), called the driver function

All other functions are invoked from main()Each statement inside the function must be

terminated with a semicolonreturn: a keyword causing the appropriate

value to be returned from the functionreturn 0 in the main() function causes the

program to end

Page 10: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++: The main() Function (continued)

USEG20/Saidatul Rahah

10

The structure of a main() function.

Page 11: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Introduction to C++: The cout Object

USEG20/Saidatul Rahah

11

cout object: an output object that sends data to a standard output display device

Page 12: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Objectives

Introduction to C++ : Identifier, keyword, mnemonic

Programming StyleData TypesArithmetic OperationsVariables and Declaration Statements

12

USEG20/Saidatul Rahah

Page 13: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Programming Style

USEG20/Saidatul Rahah

13

• Although more than one C++ statement can be on a single line, good style calls for one statement per line

• Opening and closing braces {} for the function body should each be on separate lines

• Statements in the function body should be indented

Page 14: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Programming Style: Comments

USEG20/Saidatul Rahah

14

Comments: explanatory remarks in the source code added by the programmer

Line comment: begins with // and continues to the end of the line Line comment can be on a line by itself, or at the end

of a line of code Line comment cannot be longer than one line

Block Comment: a comment that spans across two or more lines Block comment begins with /* and ends with */

Example:/* This is a block comment that

spans across three lines */

Page 15: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Programming Style: Comments(continued)

USEG20/Saidatul Rahah

15

Page 16: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Objectives

Introduction to C++ : Identifier, keyword, mnemonic

Programming StyleData TypesArithmetic OperationsVariables and Declaration Statements

16

USEG20/Saidatul Rahah

Page 17: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types

USEG20/Saidatul Rahah

17

• Data type: a set of values and the operations that can be applied to these values

• Two fundamental C++ data groupings:– Class data type (a class): created by the

programmer– Built-in data type (primitive type): part

of the C++ compiler

Page 18: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types (continued)

USEG20/Saidatul Rahah

18

Numerical Data Types

Built-in data types

Integer Types Floating-PointTypes

Page 19: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer

USEG20/Saidatul Rahah

19

Page 20: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

20

int data type: whole numbers, optionally with + or – sign Example: 2

char data type: individual character; any letter, digit, or special character enclosed in single quotesExample: ‘A’

Character values are usually stored in ASCII code

Page 21: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

21

Page 22: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

22

Escape character: the backslash, \; indicates an escape sequence

Escape sequence: tells compiler to treat the following characters as special instruction codes

Page 23: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

USEG20/Saidatul Rahah

23

Refer Table 2.4: Escape Character. p54

Page 24: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

24

bool data type: represents Boolean (logical) data; restricted to two values: true or false

sizeof operator: shows the number of bytes used to store values of any data type

Values returned by sizeof are compiler dependent

Page 25: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

25

Page 26: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

26

Signed data type: one that permits negative, positive, and zero values

Unsigned data type: permits only positive and zero values

An unsigned data type provides essentially double the range of its signed counterpart

Page 27: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Integer (continued)

USEG20/Saidatul Rahah

27

Page 28: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Floating-Point Types

USEG20/Saidatul Rahah

28

Floating-point number (real number): zero or any positive or negative number containing a decimal pointExamples: +10.625 5. -6.2

No special characters are allowedThree floating-point data types in C++:

float (single precision) double (double precision) long double

Page 29: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Data Types: Floating-Point Types (continued)

USEG20/Saidatul Rahah

29

Page 30: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Objectives

Introduction to C++ : Identifier, keyword, mnemonic

Programming StyleData TypesArithmetic OperationsVariables and Declaration Statements

30

USEG20/Saidatul Rahah

Page 31: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations

USEG20/Saidatul Rahah

31

• Arithmetic operators are binary operators• Binary operators: require two operands• Different data types can be used in the same

arithmetic expression

Page 32: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations: Expression Types

USEG20/Saidatul Rahah

32

Expression: any combination of operators and operands that can be evaluated to yield a value

Mixed-mode expression: contains integer and floating-point operands; yields a double-precision value

15 + 5 = 20

operatoroperands

Page 33: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations :Summary of Operators

USEG20/Saidatul Rahah

33

Page 34: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations (continued)

USEG20/Saidatul Rahah

34

Integer Data Type

7 + 3 = 10

7 – 3 = 4

7 * 3 = 21

7 / 3 = 2

7 % 3 = 1

Floating Point Data Type

7.0 + 3.0 = 10.0

7 .0– 3.0 = 4.0

7.0 * 3.0 = 21.0

7.0 / 3.0 = 2.33•If both operands are integer data type, output will be INTEGER•If ONE operand is floating point data Type, output will be FLOATING POINT

Page 35: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations: Operator Precedence & Associativity

USEG20/Saidatul Rahah

35

Rules for writing arithmetic expressions: Never place two consecutive binary arithmetic

operators side by side Use parentheses to form groupings; contents within

parentheses are evaluated first You may nest parentheses within other parentheses;

evaluated from innermost to outermost Use the * operator for multiplication, not parentheses

Page 36: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations: Operator Precedence & Associativity

(continued)

USEG20/Saidatul Rahah

36

Expressions with multiple operators are evaluated by precedence of operators

Associativity: the order in which operators of the same precedence are evaluated

Page 37: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Arithmetic Operations: Operator Precedence & Associativity

(continued)

USEG20/Saidatul Rahah

37

Example:8 + 5 * 7 % 2 * 4= 8 + 35 % 2 * 4= 8 + 1 * 4= 8 + 5=12

Page 38: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Objectives

Introduction to C++ : Identifier, keyword, mnemonic

Programming StyleData TypesArithmetic OperationsVariables and Declaration Statements

38

USEG20/Saidatul Rahah

Page 39: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Variables and Declaration Statements

USEG20/Saidatul Rahah

39

• Variable: symbolic identifier for a memory address where data can be held

• Use identifier naming rules for variable names

Page 40: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Variables and Declaration Statements (continued)

USEG20/Saidatul Rahah

40

Assignment statement: used to store a value into a variable

Value of the expression on the right side of the = is assigned to the memory location of the variable on the left side of the =Examples:

num1 = 45;num2 = 12;total = num1 + num2;

45num1

Page 41: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Variables and Declaration Statements (continued)

USEG20/Saidatul Rahah

41

Declaration statement: specifies the data type and identifier of a variable; sets up the memory locationSyntax: <dataType> <variableName>;

A variable must be declared before it is used!

Data type is any valid C++ data typeExample:

int sum;int num1, num2, num3;float pi = 3.142; Initialized in

declaration

Page 42: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Problem Solving

USEG20/Saidatul Rahah

42

Write a C++ program to calculate the average of two double-precision numbers. Apply Software Development Procedure:

Step 1: Analyze the problemOutput - average Inputs - 2 numbers ( num1, num2)

Step 2: Develop a solution Assign values to num1 and num2 Calculate and display average

Step 3: Code the solution Step 4: Test and correct the program

Page 43: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Problem Solving (continued)

USEG20/Saidatul Rahah

43

Flowchart:start

Input 2 numbers

Calculate average

Display average

end

#include <iostream.h>

int main(){ double num1, num2, average; num1 = 45.5; num2 = 10.2; average = (num1+num2)/2;

cout<<“Average is: “<<average;

return 0;}

Variable declaratio

n

Assignment statement

output

Page 44: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Common Programming Errors

USEG20/Saidatul Rahah

44

Missing parentheses after mainMissing or incorrect braces around function bodyMisspelling a reserved wordMissing ending double quotes on string literalMissing semicolon at end of statementAdding a semicolon at end of #include statementMissing \n to indicate new lineSubstituting letter O for zero and vice versa

Page 45: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Common Programming Errors (continued)

USEG20/Saidatul Rahah

45

Failing to declare all variablesStoring incorrect data type into a variableAttempting to use a variable with no valueDividing integer values incorrectlyMixing data types in the same expression

Page 46: CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.

Short Quiz

USEG20/Saidatul Rahah

46

Write down your name, SID no, class name on a piece of paper and answer below question.

1.List 4 rules for forming identifier names.2.Solve expression given below.

12 + 5 % 2 * 8 – 3

Thank You for Listening…..