Chapter One Array
Chapter One Array
C++
CHAPTER ONE
ARRAY
03/05/2025 1
What is array
• An array is a sequence of objects all of which have
the same type. The objects are called the elements
of the array and are numbered consecutively 0, 1, 2,
3, . . . .
• These numbers are called index values or subscripts
of the array. The term “subscript” is used because as
a mathematical sequence, an array would be
written with subscripts: a0, a1, a2, . . . .
03/05/2025 3
Memory Requirements of Arrays
• The amount of memory used by an array depends
on the array’s data type and the number of
elements.
• The hours array, defined here, is an array of six
shorts.
short hours[6];
• On a typical PC, a short uses two bytes of memory,
so the hours array would occupy 12 bytes. This is
shown in Figure below.
03/05/2025 4
Cont’d…
03/05/2025 5
Cont’d…
03/05/2025 6
Array declaration
• The syntax for an array declaration is
type array-name[array-size];
where type is the array’s element type and array-size is
its number of elements. The declaration
double a[SIZE];
• declares a to be an array of 5 elements, each of type
double. Standard C++ requires array-size to be a positive
integer constant. So it must be either a symbolic constant
or an integer literal like this:
double a[5]; int studentlist[90];
• Generally, it is better to use a symbolic constant since the
same
03/05/2025
size value is likely to be used in for loops that 7
Array Initialization
• Arrays may be initialized when they are defined.
• Like regular variables, C++ allows you to initialize an
array’s elements when you create the array. Here is
an example:
• int MONTHS = 12;
• int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
• The series of values inside the braces and separated
with commas is called an initialization list.
• These values are stored in the array elements in the
order they appear in the list.
03/05/2025 8
Cont’d…
• If you initialize an array when it is declared, you can
omit the size of the array, and the array will
automatically be declared to have the minimum size
needed for the initialization values.
• For example, the following declaration
int b[] = {5, 12, 11};
is equivalent to
int b[3] = {5, 12, 11};
03/05/2025 9
Cont’d…
// array initialization
#include <iostream.h>
int main()
{
int days[12];
days[0] = 31; // January
days[1] = 28; // February
days[2] = 31; // March
days[3] = 30; // April
days[4] = 31; // May
days[5] = 30; // June
days[6] = 31; // July
days[7] = 31; // August
days[8] = 30; // September
days[9] = 31; // October
days[10] = 30; // November
days[11] = 31; // December
for (int count = 0; count < 12; count++)
{
cout << "Month " << (count + 1) << " has ";
cout << days[count] << " days.\n";
} 03/05/2025 10
Cont’d…
After initializing an array, its elements are counted from left to
right. Each element of the array, also called a member of the
array, has a specific and constant position.
The first member of the array, the most left, has an index of 0.
03/05/2025 13
Cont’d…
// Accessing array element by using index
#include <iostream.h>
int main()
{
double score[] = {44.5, 72.52, 96.08, 68.78, 66.28};
03/05/2025 15
Types of Array
There are two types of array in c++ program.
1. One dimensional array
2. Two dimensional array
03/05/2025 28
Cont’d…
#include <iostream.h>
int main()
{
int SomeArray[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
for (int i = 0; i<5; i++)
for (int j=0; j<2; j++)
{
cout << "SomeArray[" << i << "][" << j << "]: ";
cout << SomeArray[i][j]<< endl;
}
return 0;
03/05/2025 29
Multidimensional array initialation
03/05/2025 32
Cont’d…
const int NUM_STUDENTS = 3; // Number of students
const int NUM_SCORES = 5; // Number of test scores
double total; // Accumulator is set in the loops
double average; // To hold each student's average
double scores[NUM_STUDENTS][NUM_SCORES] = {{88, 97, 79, 86, 94},
{86, 91, 78, 79, 84},
{82, 73, 77, 82, 89}};
// Get each student's average score.
for (int row = 0; row < NUM_STUDENTS; row++)
{
// Set the accumulator.
total = 0;
// Sum a row.
for (int col = 0; col < NUM_SCORES; col++)
total += scores[row][col];
// Get the average.
average = total / NUM_SCORES;
// Display the average.
cout << "Score average for student "
<<03/05/2025
(row + 1) << " is " << average <<endl; 33
Cont’d…
#include <iomanip.h>
#include<iostream.h>
main()
{
int i,j;
int nrows,ncols, X[10][10];
cout<<"No of rows : ";
cin>>nrows;
cout<<"No of Columns : ";
cin>>ncols;
for (i =0; i<nrows;i++)
{
for(j =0; j<ncols;j++)
{
X[i][j] = 2*i + 3*j;
}
}
cout<<" The matrix is given below "<<endl;
for (i =0; i<nrows;i++)
{
for(j =0; j<ncols;j++)
{
cout<<setw(5)<<X[i][j];
}
03/05/2025 34
cout<<endl;
Arrays of Strings
• A two-dimensional array of characters can be used
as an array of strings.
• Because strings are stored in single-dimensional
character arrays, an array of strings would be a
two-dimensional character array.
03/05/2025 35
cont`d
• Just as the name of an array represents the array’s
address, a two-dimensional array with only the row
subscript represents the address of that row.
• For instance, in the array defined above,
scientists[0] represents the address of row 0,
scientists[1] represents the address of row 1, and so
forth.
• The following cout statement will display the string
“Einstein” on the screen:
cout << scientists[3];
03/05/2025 36
Cont’d…
// two-dimensional character array to hold the names of the months and a one-dimensional
integer array to hold
// the number of days in each month.
#include <iostream.h>
int main(){
const int NUM_MONTHS = 12; // The number of months
const int STRING_SIZE = 10; // Maximum size of each string
// Array with the names of the months
char months[NUM_MONTHS][STRING_SIZE] =
{ "January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" };
// Array with the number of days in each month
int days[NUM_MONTHS] = {31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31};
// Display the months and their numbers of days.
for (int count = 0; count < NUM_MONTHS; count++){
cout << months[count] << " has ";
cout03/05/2025
<< days[count] << " days.\n"; } 37
Cont’d…
#include<iostream.h>
int main()
{
const int ROWS = 4;
const int COLS = 8;