0% found this document useful (0 votes)
22 views22 pages

Ch2p1 Sequential

Uploaded by

2022885922
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)
22 views22 pages

Ch2p1 Sequential

Uploaded by

2022885922
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/ 22

CSC126 FUNDAMENTALS OF ALGORITHMS &

COMPUTER PROBLEM SOLVING

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING | JUHAIDA ISMAIL 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, char[ ], const)

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 2


A simple C++ program has the following form:

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 3


EXAMPLE : A PROGRAM THAT CONVERTS MILES
INTO KILOMETERS ( 1 Kilometer=1.609 miles)

Input : miles
(I) PROBLEM Output: kilo
DEFINITION Formula : kilo = miles * 1.609

1. Get input
Read miles
2. Calculate kilo (II) ALGORITHM
kilo = miles * 1.609 DESIGN
3. Display output
Print “Miles in kilometer is ” kilo

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 4


5

EXAMPLE : A PROGRAM THAT CONVERTS


MILESINTO KILOMETERS (
1Kilometer=1.609 miles)
IMPLEMENTATION
(III) ALGORITHM
BASIC PROGRAM COMPONENTS

LINE EXPLANATION
Lines 1-6 ▪ These are introductory comments.
▪ The comments are not executed by the compiler
and they are used only to allow the readability of
the program.

Line 8 ▪ The #include directive is a special instruction for the


C++ compiler that tells the compiler to include the
contents of another file, in this case the iostream
file. Files that are included in the programs are
called include files or header files.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 6


BASIC PROGRAM COMPONENTS

LINE EXPLANATION
Lines 8 (cont.)
▪ In the C++, the iostream file contains the instruction
(source code) needed to handle input and output
operation, such as inputting data form keyboard and
outputting information on the computer screen.
▪ C++ programs typically include at least one directive,
and most include many directives.

Line 10 ▪ The word main, which must be typed in lowercase


letters, is the name of function. A function is simply a
block of code that performs a task. The entire line of
code, int main() is referred to as function header,
because it marks the beginning of a function.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 7


BASIC PROGRAM COMPONENTS
LINE EXPLANATION
Lines 11 ▪ The beginning brace { marks the beginning of the code
block that comprises the function. Everything between the
opening and closing set of braces ({ }) belongs to this main
function and is referred to as the function body.

Line 12,16,20,23 ▪ These are the comments that tell the purpose of the
program’s section.
Line 13,14 ▪ These instructions declare or reserve two variables, which
are simply memory locations that the program uses while
it is running. The keyword float tells the compiler that the
variable or the memory location can store a number with a
decimal place.
▪ The instruction to declare a variable is considered a
statement in C++, therefore it ends with a semicolon(;).

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 8


BASIC PROGRAM COMPONENTS
LINE EXPLANATION
Lines 17 ▪ The cout object and the insertion operator (<<) indicate that
the message in the enclosed quotation marks (“ ”) will be
sent to the console (screen).

Line 18 ▪ The cin object and the extraction operator (>>) will accepts a
value from the keyboard and store the value into the
memory location miles.

Line 21 ▪ An instruction to multiply miles by 1.609 and stores the


result into the memory location kilo.

Line 24-25 ▪ Display the message “Miles in kilometers is ” and the value
stored in the variable kilo to the screen. This statement end
with ‘;’ can be written in two lines.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 9


BASIC PROGRAM COMPONENTS

LINE EXPLANATION
Lines 26 ▪ return 0 – this keyword indicate that program terminate in
normal condition. return 0 must be written if the main()
function has data type integer (int).

Line 27 ▪ Closing brace } marks the end of the program body.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 10


▪ Identifier is simply references to memory location which
can hold data and used to represent variables,
constants and name of functions (sub‐modules) in
computer programs.

Rules for naming identifiers are :


THE 1ST LETTER MUST BE ALPHABET OR UNDERSCORE

1 Example of VALID identifiers:


form1
Example of INVALID identifiers
3number
_2nd_class 8tvi
side_3 1_to_0

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 11


Rules for naming variables are:

CAN BE A COMBINATION OF ALPHABET LETTER, DIGIT


AND UNDERSCORE

2 Example of valid identifiers:


milesToKilo kilo_to_miles
Example of invalid identifiers
total$ km/hour comm%

CANNOT USE C++ RESERVED WORD

3
CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 12
Rules for naming variables are:

NO BLANKS OR WHITE SPACE CHARACTERS

4 Example of VALID identifiers:


form1 _2nd_class side_3
Example of INVALID identifiers
number one first number

CASE SENSITIVE (THE LOWERCASE AND UPPERCASE


ARE TREATED AS DIFFERENT CHARACTERS)

5 ▪ Example: firstNumber, firstnumber, FIRSTNUMBER,


FirstNumber and Firstnumber are treated as different
identifiers.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 13


IDENTIFIERS
1. VARIABLE

▪ Variable is an identifier that refers to memory location, which can


hold values. Variable can only store one value at one time. Thus the
content of variable may change during the program execution.

▪ Before it is used, it must be declared with its data type.

▪ Syntax: data_type variable_name


▪ Example: int sum, float price, char name

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 14


IDENTIFIERS
1. VARIABLE

▪ Once a variable is declared, it contains none useful value (garbage). To


make it useful, the variable must be given a value, either by the
assignment statement or input from the user, before it is used. The
value serves as the initial value to the variable.
▪ Example of variable initialization

num=10; (set the initial value of 10 into variable num using


assignment statement (=))

cin >> number;


(set the initial value into the variable number using input
statement called extraction (>>))

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 15


IDENTIFIERS
1. VARIABLE

▪ SINGLE declaration:
▪ Example: int num1;
int num2;
int sum;
▪ MULTIPLE declarations – variables having the same data type can be
grouped and declared using the single declaration statement.
▪ For example, single declaration as shown above can
be declared as a single statement.
✓ Example: int num1, num2, sum;
✓ Example of initialization: int num1 = 10, num2 = 20, sum = 0;
▪ The following example gives all the variable the value 0,
✓ Example: score1 = score2 = score3 = 0;

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 16


IDENTIFIERS
2. CONSTANT

▪ An identifier whose value does not change throughout program


execution.
▪ Allow you to give name to a value that is used several times in a
program.
▪ Usually, constant name is capital to distinguish from other variables.

▪ 2 ways of declaring constant:

a. Using constant keywords: const float PI=3.142;


b. Using preprocessor directives: #define PI 3.142

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 17


IDENTIFIERS
2. CONSTANT

▪ 2 ways of declaring constant:

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 18


ESCAPE SEQUENCES

▪ The combination of backslash (\) and specific characters are called


escape sequences.
▪ Escape sequences change the meaning of the character that
follows it.
▪ Those escape sequences must be within quotation marks ( “ ” ).

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 19


ESCAPE SEQUENCES

▪ Table 2 illustrates some escape


sequences.

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 20


BASIC DATA TYPES

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 21


▪ List TWO(2) rules for naming an identifier.
▪ What is the difference between variable and constant?
▪ Determine which of the following are valid identifiers?

CSC126 FUNDAMENTALS OF ALGORITHMS & COMPUTER PROBLEM SOLVING 22

You might also like