[go: up one dir, main page]

0% found this document useful (0 votes)
36 views20 pages

Chapter 3 - COMPONEN OF PROGRAMMING II (CSC425)

The document discusses assignment statements in C++, including assignment operators and increment/decrement operators. It also covers mathematical library functions, formatting output, and string manipulations. Examples and exercises are provided to demonstrate each concept.

Uploaded by

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

Chapter 3 - COMPONEN OF PROGRAMMING II (CSC425)

The document discusses assignment statements in C++, including assignment operators and increment/decrement operators. It also covers mathematical library functions, formatting output, and string manipulations. Examples and exercises are provided to demonstrate each concept.

Uploaded by

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

Introduction to Computer

Programming
(CSC425)

by
DR AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
Completing the Basics

Chapter 3

2
Contents
⚫ Assignment Operation
⚫ Mathematical Library Functions
⚫ Formatting Program Output
⚫ String Manipulation

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
Assignment Statement
⚫ Is the most basic C++ statement
⚫ Use to store the value of an expression in a variable
⚫ Use Assignment Operator (=)
⚫ Syntax:

variable = <variable|constant|expression>

⚫ Expression can be :
i. a constant
ii. another variable
iii. an arithmetic expression
iv. a function

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
Assignment Statement (cont.)

⚫ Examples:
length = oldLength;
width = 50;
area = length * width;
moredata = true; //a boolean variable

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
Assignment Statement (cont.)

⚫ Try to run :
- Exercise 4
- Exercise 8
- Exercise 13

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
Assignment Statement (cont.)

Variations of Assignment Operation :


⚫ We can have assignment statement like this :
sum = sum + 10;

⚫ This statement can be written using the following shortcut


assignment operators :

+= -= /= %= *=

⚫ Hence, the equivalent “sum = sum + 10”, would be :

sum += 10;
March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 7
PROGRAMMING
Assignment Statement (cont.)

⚫ Example :

Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3 count += 3
price *= rate + 1 price = price * (rate + 1)
but not
price = price * rate + 1

( See Exercise 14)

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
Assignment Statement (cont.)
⚫ For a variable to increase or decrease by 1, C++ provides two
unary operators :
(++) : increment operator
(--) : decrement operator

⚫ Example :
(See Exercise 15)

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 9


PROGRAMMING
Assignment Statement (cont.)
⚫ Increment & Decrement Operators:

Expression Equivalent to
i = i + 1 ⚫ ++i (prefix increment operator)
If x = 5; and y = ++x;
After the second statement both x and y are 6
⚫ i++ (postfix increment operator)
If x = 5; and y = x++;
After the second statement y is 5 and x is 6

i = i - 1 --i

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 10


PROGRAMMING
Assignment Statement (cont.)

Assignment #1

⚫ Write a program : Convert Length


Write a program that takes as input given lengths
expressed in feet and inches. The program should
then convert and out the length in centimeters.
Assume that the given lengths in feet and inches
are integers.

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
Mathematical Library Functions
⚫ Header file cmath (math.h)
⚫ used in the following form :
function_name(argument)

Example :
sqrt(4.5) //square root of x
fabs(2.3 * 4.6) //absolute value |x|
tan(x) //tangent of x
floor(x) //largest interger <= x

(See Exercise 16 & 17)

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
Formatting Program Output
⚫ Besides displaying correct results, it is
extremely important for a program to
present its output attractively.
⚫ To include manipulators within an output
display, must include the header file using
the preprocessor command as follows :
#include <iomanip>
⚫ Most commonly used manipulators in a
cout statement :
March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 13
PROGRAMMING
Formatting Program Output
Manipulator Action
endl Manipulator moves output to the beginning of the next
line
setfill(‘x’) Sets the fill character from its default of a blank space to
the character x
setprecision(n) Outputs decimal numbers with up to n decimal places
showpoint forces output to show the decimal point and trailing
zeros
scientific Use exponential notation on output
left Left justify output
fixed Outputs floating-point numbers in a fixed decimal format
setw Outputs the value of an expression in specific columns

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
Formatting Program Output
⚫ Example :
Exercise 19

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
String Manipulations
⚫ String input
- header file :
#include <cstring>
- can be declared and initialized as :
string name;
name = “Abdul”;

or to read a string from the keyboard


cout << “What is your name ?”;
cin >> name;

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
String Manipulations
⚫ String function :
string-name.length():
- to know the numbers of character in a
string
string-name.substr(m,n):
- to extract substring from a full string

⚫ String concatenation :
- joining two or more strings together to
become one long string.

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
Review
⚫ Declare the suitable storage to hold the following
data:
1. Number of days worked in a week
2. Student’s CGPA
3. Name of a person
4. Speed of a light

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
Review
⚫ Translate the following flow chart into source code:
Begin

Read totalScore

Read count

average = total / count

Display average

End

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
Review
⚫ Given the following declaration:

int a, b, c, v1, v2;


a = 8;
c = b = 3;
c = c – 1;
v1 = a / b;
v2 = a / c;

What is the final content of a, b, c, v1 and v2?

March 17, 2024 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING

You might also like