Professional Documents
Culture Documents
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
General Syntax:
datatype arrayname [index] ;
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
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:
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>
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