Date post: | 10-Apr-2018 |
Category: |
Documents |
Upload: | meljun-cortes-mbampa |
View: | 219 times |
Download: | 0 times |
of 46
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
1/46
C H A P T E R 2P R O B L E M S O L V I N G U S I N G C + +
1
C++ Programming
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
2/46
Objectives
y Introduction to C++ : Identifier, keyword, mnemonic
y Programming Style
y Data Types
y Arithmetic Operations
y Variables and Declaration Statements
2
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
3/46
Introduction to C++
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
3
y Modular Program: a program consisting ofinterrelated segments arranged in a logical andunderstandable form Easier to develop, correct, and modify than other kinds of
programs
y Module: a small segment which is designed toperform a specific task A group of modules is used to construct a modular program
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
4/46
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
5/46
Introduction to C++:Identifier
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
5
y Identifier: a name given to an element of thelanguage, such as a class or function
y 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 cannotbe used as identifiers
Max length of an identifier = 1024 characters
y Use underscores to separate multiple words in aname, or capitalize the first letter of each word.
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
6/46
Introduction to C++:Keyword
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
6
y Keyword: a reserved name that represents a built-inobject or function of the language
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
7/46
Introduction to C++:Identifier (continued)
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
7
y Identify valid C++ identifiers below: degToRad
1AB3
addNumswhile
slope
findMax
E*6
valid
valid
valid
valid
invalid begins with a number
invalid contains a specialcharacter
invalid this is a keyword
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
8/46
Introduction to C++:Mnemonic
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
8
y Function names Require a set of parentheses at the end
Can use mixed upper and lower case
Should be meaningful, or be a mnemonic
y Mnemonic: a word designed as a memory aid
Examples of function names:
areaCircle() calAverage() main()
y
Note that C++ is a case-sensitive language!
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
9/46
Introduction to C++:Themain() Function
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
9
y Overall structure of a C++ program contains onefunction namedmain(), called the driver
function
y All other functions are invoked frommain()y Each statement inside the function mustbe
terminated with a semicolon
y return: a keyword causing the appropriate value to
be returned from the function
y return 0 in themain() function causes the
program to end
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
10/46
Introduction to C++:Themain() Function (continued)
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
10
y The structure of amain() function.
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
11/46
Introduction to C++:The cout Object
USEG20/Saidatul Rahah
11
y cout object: an output object that sends data to a
standard output display device
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
12/46
Objectives
y Introduction to C++ : Identifier, keyword, mnemonic
y Programming Style
y Data Types
y Arithmetic Operations
y Variables and Declaration Statements
12
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
13/46
Programming Style
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
13
Although more than one C++ statement can be ona single line, good style calls for one statement perline
Opening and closing braces {} for the functionbody should each be on separate lines
Statements in the function body should beindented
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
14/46
Programming Style: Comments
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
14
y Comments: explanatory remarks in the source code addedby the programmer
y Line comment:begins with // and continues to the end ofthe 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
y Block Comment: a comment that spans across two or morelines Block comment begins with /* and ends with */
Example:/* This is a block comment that
spans
across three lines */
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
15/46
Programming Style: Comments(continued)
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
15
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
16/46
Objectives
y Introduction to C++ : Identifier, keyword, mnemonic
y Programming Style
y Data Types
y Arithmetic Operations
y Variables and Declaration Statements
16
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
17/46
Data Types
MELJUN CORTES,BSCS,ACSMELJUN CORTES,BSCS,ACS
17
Data type: a set of values and the operations thatcan be applied to these values
Two fundamental C++ data groupings:
Class data type (a class): created by theprogrammer
Built-in data type (primitive type): part of theC++ compiler
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
18/46
Data Types (continued)
USEG20/Saidatul Rahah
18
Numerical Data
Types
y Built-in data types
Integer Types Floating-PointTypes
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
19/46
Data Types: Integer
USEG20/Saidatul Rahah
19
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
20/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
20
y int data type: whole numbers, optionally with + or signExample: 2
y
char data type: individual character; any letter,digit, or special character enclosed in single quotesExample: A
y Character values are usually stored inASCII code
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
21/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
21
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
22/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
22
y Escape character: the backslash, \; indicates anescape sequence
y Escape sequence: tells compiler to treat the
following characters as special instruction codes
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
23/46
USEG20/Saidatul Rahah
23
Refer Table2.4: EscapeCharacter.
p61
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
24/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
24
ybool data type: represents Boolean (logical) data;restricted to two values: true or false
y sizeof operator: shows the number of bytes used to
store values of any data typey Values returned bysizeof are compiler dependent
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
25/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
25
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
26/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
26
y Signed data type: one that permits negative,positive, and zero values
y Unsigned data type: permits only positive and
zero valuesy An unsigned data type provides essentially double
the range of its signed counterpart
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
27/46
Data Types: Integer (continued)
USEG20/Saidatul Rahah
27
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
28/46
Data Types: Floating-Point Types
USEG20/Saidatul Rahah
28
y Floating-point number (real number): zero orany positive or negative number containing adecimal point
Examples: +10.625 5. -6.2
y No special characters are allowed
y Three floating-point data types in C++: float (single precision)
double (double precision)
long double
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
29/46
Data Types: Floating-Point Types(continued)
USEG20/Saidatul Rahah
29
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
30/46
Objectives
y Introduction to C++ : Identifier, keyword, mnemonic
y Programming Style
y Data Types
yArithmetic Operations
y Variables and Declaration Statements
30
USEG20/Saidatul Rahah
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
31/46
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
expressionOperation Operator
Addition +
Subtraction -
Multiplication *Division /
Modulus(remainder)
%
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
32/46
Arithmetic Operations:Expression Types
USEG20/Saidatul Rahah
32
y Expression: any combination of operators and operandsthat can be evaluated to yield a value
y Mixed-mode expression: contains integer and floating-point operands; yields a double-precision value
15 + 5 = 20
operatoroperands
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
33/46
Arithmetic Operations :Summary of Operators
USEG20/Saidatul Rahah
33
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
34/46
Arithmetic Operations (continued)
USEG20/Saidatul Rahah
34
IntegerData Type
7 + 3 = 10
7 3 = 4
7 * 3 = 21
7 / 3 = 2
7 % 3 = 1
Floating Point DataType
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 INTEGERIf ONE operand is floating point dataType, output will be FLOATING POINT
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
35/46
Arithmetic Operations:Operator Precedence & Associativity
USEG20/Saidatul Rahah
35
y 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
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
36/46
Arithmetic Operations:Operator Precedence & Associativity (continued)
USEG20/Saidatul Rahah
36
y Expressions with multiple operators are evaluated byprecedence of operators
y Associativity: the order in which operators of the sameprecedence are evaluated
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
37/46
Arithmetic Operations:Operator Precedence & Associativity (continued)
USEG20/Saidatul Rahah
37
y Example:
8 + 5 * 7 % 2 * 4
= 8 + 35 % 2 * 4
= 8 + 1 * 4
= 8 + 5
=12
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
38/46
Objectives
y Introduction to C++ : Identifier, keyword, mnemonic
y Programming Style
y Data Types
y Arithmetic Operations
yVariables and Declaration Statements
38
USEG20/Saidatul Rahah
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
39/46
Variables and Declaration Statements
USEG20/Saidatul Rahah
39
Variable: symbolic identifier for a memory address wheredata can be held
Use identifier naming rules for variable names
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
40/46
Variables and Declaration Statements(continued)
USEG20/Saidatul Rahah
40
yAssignment statement: used to store a value intoa variable
y Value of the expression on the right side of the = is
assigned to the memory location of the variable onthe left side of the =
Examples:
num1 = 45;
num2 = 12;total = num1 + num2;
45
num1
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
41/46
Variables and Declaration Statements(continued)
USEG20/Saidatul Rahah
41
y Declaration statement: specifies the data typeand identifier of a variable; sets up the memorylocationSyntax: ;
y A variable must be declaredbefore it is used!
y Data type is any valid C++ data typeExample:
int sum;
int num1, num2, num3;
float pi = 3.142;
Initialized indeclaration
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
42/46
Problem Solving
USEG20/Saidatul Rahah
42
y Write a C++ program to calculate the average of twodouble-precision numbers.Apply Software Development Procedure:
Step 1: Analyze the problem
Output - average
Inputs - 2 numbers ( num1, num2)
Step 2: Develop a solutionAssign values to num1 and num2
Calculate and displayaverageStep 3: Code the solution
Step 4: Test and correct the program
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
43/46
Problem Solving (continued)
USEG20/Saidatul Rahah
43
y Flowchart:start
Input 2numbers
Calculateaverage
Displayaverage
end
#include
int main(){
double num1, num2, average;
num1 = 45.5;num2 = 10.2;average = (num1+num2)/2;
cout
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
44/46
Common Programming Errors
USEG20/Saidatul Rahah
44
y Missing parentheses aftermain
y Missing or incorrect braces around function body
y Misspelling a reserved word
y Missing ending double quotes on string literal
y Missing semicolon at end of statement
y Adding a semicolon at end of#include statement
y
Missing \n to indicate new liney Substituting letter O for zero and vice versa
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
45/46
Common Programming Errors (continued)
USEG20/Saidatul Rahah
45
y Failing to declare all variables
y Storing incorrect data type into a variable
y Attempting to use a variable with no value
y Dividing integer values incorrectly
y Mixing data types in the same expression
8/8/2019 MELJUN CORTES--IT102 C++ Fundamental BASICS
46/46
Short Quiz
USEG20/Saidatul Rahah
46
y Write down your name, SID no, class name on apiece 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..