0% found this document useful (0 votes)
91 views

Chapter 2: Basic Elements of Computer Program: Csc128 Fundamentals of Algorithm and Computer Problem Solving

The document provides an overview of basic elements of computer programs including program components, data types, operators, and statements. It discusses identifiers, variables, constants, standard data types, arithmetic operators and precedence, assignment statements, and mathematical functions. Examples are given to illustrate arithmetic expressions and assignments. The key lessons are understanding basic program structure and being able to write simple programs using variables, data types, operators, and statements.

Uploaded by

afiq8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Chapter 2: Basic Elements of Computer Program: Csc128 Fundamentals of Algorithm and Computer Problem Solving

The document provides an overview of basic elements of computer programs including program components, data types, operators, and statements. It discusses identifiers, variables, constants, standard data types, arithmetic operators and precedence, assignment statements, and mathematical functions. Examples are given to illustrate arithmetic expressions and assignments. The key lessons are understanding basic program structure and being able to write simple programs using variables, data types, operators, and statements.

Uploaded by

afiq8686
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PART 2

CHAPTER 2: BASIC ELEMENTS OF


COMPUTER PROGRAM

CSC128 FUNDAMENTALS OF ALGORITHM AND


COMPUTER PROBLEM SOLVING

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 1


LESSON OUTCOMES
Upon completion of this chapter, students should be
able to:
 Understand basic program components
 Define and use the identifier, variable, constant and
statement
 Understand the standard data type(int, float, double,
char,
string, const)
 Understand the use of mathematical predefined
function (sqrt(), abs(), pow() etc.)
 Use the arithmetic operator (+, -, *, /, %)
 Understand the assignment statement (=)
 Write simple programs
 Use the input and output statement, formatted
output
CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 2
ARITHMETIC
1. Arithmetic Operators
 The seven arithmetic operations supported by the
C++:
OPERATOR OPERATION
+ Addition
  - Subtraction
 
 
* Multiplication

/ Division
% Modulus Division

-- Decrement

++ Increment

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 3


ARITHMETIC
2. Arithmetic Precedence

Highest
 Operators on the same precedence
level are evaluated by the compiler
i. ++ -- from left to right.
ii. -  Of course parentheses may be used to
iii.* / % alter the order of evaluation.
Parentheses are treated by C++ in the
iv. + - same way that they are by virtually all
other computer languages: They
force an operation, or set of
Lowest operation to have a higher
precedence level.

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 4


ARITHMETIC
3. Arithmetic Expression

Example :  Formally, arithmetic expression is


constructed by using arithmetic
i. -5 operators and numbers.
ii. 8 – 7  The numbers appear in the
iii.3 – 4 expression are called operands.
iv. 2 + 3 * 5  Operators that have only one
v. 5.6 + 6.2 * operand are called unary
operators.
3
 Operators that have two operands
vi.x + 2 * 5 / are called binary operators.
y
CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 5
ARITHMETIC
Example of Arithmetic Operator and Arithmetic
Expression
OPERATOR ACTION EXPRESSION VALUE (a=7, b=2)
- Subtraction a-b 5

+ Addition a+b 9
 
 
* Multiplication a*b 14
  / Division a/b 3 (remainder is
discarded)
% Modulus a%b 1 (produces
Division remainder)

-- Decrement by 1 a -- 6 (subtracts 1 from


variable a)
++ Increment by 1 a ++ 8 (adds 1 to variable a)

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 6


(Additional)
 

Evaluate the numeric expression below where a=2, b=3 and c=4. All is the integer data type.

a. (a*b)+c

b. a+b*c

c. (c-a) % b

d. (a*a+b*b+c*c)/2

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 7


ASSIGNMENT
1. Assignment Operator

 C++ has several assignment operators. The most


commonly used assignment operator is =.
 Assignment operations using = has the general form:

identifier = expression

  Where identifier generally represent variable, and


expression represent a constant, a variable or a more
complex expression.
 
 

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 8


ASSIGNMENT
2. Assignment Statement

 An assignment statement is used to place a value into


another variable coded directly within the program. In other
words, the variable gets the value initially assigned to it in
the program (hard‐coded) and not from the value keyed in
by the user.
 Example:

i. quantity = 20;
ii. price = 5.50;
iii. amount = basic_pay + overtime_hours *
overtime_rate;
iv. deductions = socso + insurance_premium +
car_loan;
v. net_pay = gross_pay - deduction;
vi. current_counter = current_counter
CSC126 FUNDAMENTALS OF ALGORITHM ++;  
AND COMPUTER PROBLEM SOLVING 9
 
ASSIGNMENT
2. Assignment Statement

 Another example of writing statement:


i. counter++ or also can be written as counter = counter
+1
ii. sum = sum + num or also can be written as sum +=
num
  TASK USAGE
 More examples:
To assign a value for a numeric data int number;
type number = 50;
char kod;
To assign a value for char data type
kod = ‘X’;

string name;
To assign a value to string data type
name=“Hambali”

To assign a value from an total = no1 + no2 + no3;


expression total = 2 + 4 + 6; 10
ASSIGNMENT
2. Assignment Statement

 Some introduction to Mathematical predefine functions

 Note: Header file used is math.h (include <math.h>) 

 Some of the most commonly used functions are given below :

NAME DESCRIPTION
pow (x,y) Return x raised to the power of y
sqrt (x) Square root of x
abs (x) Absolute value |x|

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 11


ASSIGNMENT
2. Assignment Statement
Example
 The following program accepts a number, gets the square root
of the number and multiplies the number by using the sqrt( )
and pow( ).
OUTPUT

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 12


(Additional)
 
Write a C++ statement to do the following:
1) Declare a variable named drinks with character char data type.
2) Declare a variable named price and totalPrice with double data type.
3) Declare a variable cupsOfCoffee with integer data type.
4) Assign “coffee” to the variable drinks
5) Assign 2.50 to the variable price.
6) Assign 4 to the variable cupsOfCoffee.
7) Do a process statement to calculate price times cupsofCoffee to the variable
totalPrice.
8) Display the phrase “You’ve ordered 4 cups of coffee and cost you RM 10.00”.
hint:4 is from variable cupsOfCoffee and 10.00 from variable totalPrice

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 13


1. Give the most appropriate data type for each of the following
values:
 
 

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 14


2. Create a variable name for each of the following and provide
the
appropriate data type:
 
 

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 15


3. Convert each of the following mathematical formula to C++
expression: 
 

CSC126 FUNDAMENTALS OF ALGORITHM AND COMPUTER PROBLEM SOLVING 16

You might also like