You are on page 1of 51

Arrays

What is Array?
Array
Is a special type of variable which can contain
or hold one or more values of the same data
type with reference to only one variable
name.
It can be distinguished through a pair of
square brackets [ ]
Index (or element)
The number in square brackets
The index always start with 0
The array whose index is zero is the
first element in the array
for Loop

The for loop is ideally used to


manipulate an array.
Array Initialization

General Syntax:
datatype arrayname [index] ;

Example: int score [10] ;


float lenght [8] ;
Array Initialization
In C++, an array consisting of 5
variables of type int can be declared as
follows:

int score [5] ;


int data type of array variable (base
type)
score variable used for the array
[ 5 ] size of the array (declared size)
Array Initialization

Is also written as:

int score[0], score[1], score[2], score[3],


score[4];
Array Initialization
This array can be viewed in the
computer as:
One Dimensional Array Initialization
int score[3];
score[0] = 90;
score[1] = 95;
score[2] = 100;
int score[3] = {90, 95, 100};
int score[ ] = {90, 95, 100};
Indexed variables not provided with
initializers are initialized to zero.
One-Array Initialization
Source Code
Array Initialization Output
One Dimensional
Array Initialization
One Dimensional
Array Initialization
Sample Program
(without array)

Program No. 1
1. Create a C++ program that will input
five numbers and display the sum.
Sample Program
(without array)
Sample Program
(using one dimensional array)
Program No. 2

A student has 10 quizzes in Comp2. He


wants to write a program to compute the
average of 10 quizzes. One option is for
the programmer to declare 10 individual
variables to store the scores of the 10
quizzes. Write a program not using
array that will compute the average
quiz.
Source Code (without array)
Source Code
(without array) cont.
From Program No. 2

Re-write the C++ program that will


accept 10 quizzes and compute the
average quiz using one dimensional
array.
Source Code (with array)
Output (with array)
Knowledge Check
What is the problem with the first
approach not using an array?
What is the advantage of the second
approach using an array?
Program No. 3
3. Write a C++ program using one
dimensional array that determines the
highest value among the five input
values from the keyboard and display
the difference of each value from the
highest value.
Sample Output
Problem No. 3 (Source Code)
What is Two-Dimensional
Array?
Two-Dimensional Arrays

Arrays are also useful in forming tables or


matrices.
It can define one array for multiple sets of
data.
Two-Dimensional Arrays

It is like a table in a spreadsheet (has rows


and columns)
Use two size (index) declarators in definition
int number[4][3];

Number Number
of rows of columns
Two-Dimensional Array
Representation
int table[4][3];

Columns
exams[0][0] exams[0][1] exams[0][2]
R
exams[1][0] exams[1][1] exams[1][2]
o
w exams[2][0] exams[2][1] exams[2][2]
s
exams[3][0] exams[3][1] exams[3][2]
Two-Dimensional Array
Initialization
int score [2] [3];

int score [2] [3] = { {80, 70, 100}, {50, 60, 85} };
Two-Dimensional Array
Initialization
This array can be viewed in the computer
as:

Col1 Col2 Col3

row1 80 70 100
row2 50 60 85
Two-Array Initialization
Sample Output
To input an array values
Two-Array Initialization
Source Code
Two-Array Initialization
Sample Output
To display an array values
Two-Array Initialization
Source Code
Program No. 5
Create a C++ program using two-
dimensional array that will input three
quiz score for four students and display
the average score of each student.
Sample Output
#include <iomanip>

A header file for input/output


manipulator
Is a library that is used to manipulate
the output of our program
Some Manipulators
Used in the Program

setiosflags includes:
ios::fixed
ios::showpoint
setprecision
setw
Some Manipulators
Used in the Program

ios::fixed
Display floating point values using
normal notation (will not use a scientific
notation)
Therefore well never see a number
displayed as 1.06e+12 or something
similar.
Some Manipulators
Used in the Program

ios::showpoint
Display a decimal and extra zeros, even
when not needed.
Some Manipulators
Used in the Program
The proper way to invoke these two
statements are as follows:

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
Some Manipulators:
(used in the program)
setprecision
It is a statement that specifies how
many decimal points to print out.
Example if we have the number
54.271123 and used
cout.precision(2)- it would print
out: 54.27 to the console.
Some Manipulators:
(used in the program)
setw
Sets the field width to be used on output
operation
Example: cout<< setw(20)
Source Code
Source Code: continued
Thank You

Prepared by: Prof. LRQ Natividad


That in all things,
God may be glorified!
Reminder
Practical Quiz next meeting
Coverage: Array

You might also like