CHAPTER TWO
Array and string
LEARNING OBJECTIVE
At the end of this lesson students able to answer
▪ What is an array?
▪ Types of array.
▪ One dimensional array
▪ How to declare
▪ Initialize
▪ How to access
▪ Two dimensional array
▪ How to declare
▪ Initialize
▪ How to access
▪ String
IN WHAT CIRCUMSTANCES WE NEED TO
USE ARRAYS?
▪ Consider the following problem.
▪ Assume you need a program that:
▪ Accepts 15 floating point numbers from the user
▪ calculates their average
▪ print out the numbers that are above the average.
▪ What would you do?
▪ It declare 15 variable?
What is an array?
✓An array is a sequence of variables that are similar data type
and share the same name.
✓Those sequence of variables referenced by using an index.
✓Arrays are useful to store a large number of values of the same
type.
CONT..
✓Arrays are Homogeneous – all elements are the same
data type.
✓Array is a consecutive group of memory location for all
of its elements.
✓Individual variables of an array are called elements.
CONT’D….
✓There are 2 types of Arrays:
1. Single Dimensional Arrays –for a sequence data either in a row
or a column
2. Multi Dimensional Arrays
▪ 2, 3 or more Dimensional
▪ Our focus in this course is only single and 2D Arrays
▪ 2D Arrays : a sequence data in a table form with m number
of rows and n number of Colum.
ONE DIMENSIONAL ARRAY
✓Index or position is used to access (fetch, retrieve, or take) a
particular values of the array.
✓The index must be an integer and indicates the position of the
elements in the array.
✓Thus the element of the array are ordered by the index which
starts from 0 to n-1 if it has n number of element.
Cont…
✓An individual element of an array is identified by its own
unique index(subscript).
✓Index is considered as the positions of the values.
0 1 2 3 4 5 6 7 8
50 60 70 80 90 100 40 30 20
✓The index for 50 is 0 and the index for 100 is 5
✓Number of element in array is 9.
Declaring Arrays 1D
▪ Arrays occupy space in memory when we declare them.
▪ Array declaration is a very similar to variable declaration.
DATA TYPE ARRAYNAME [ARRAY SIZE];
▪ The array Size must be an integer constant greater than zero
(>0) and data type can be any valid C++ data type.
▪ For example, to declare a 10-element array called balance of type
int,
▪ int balance [10];
▪ This declaration will cause the compiler to allocate space 10
consecutive intger variables in memory by the name balance .
CONT.…
▪ Once an array is declared, its size (Number of elements) will
remain the same.
▪ So, it is better to declare a constant SIZE variable and use it for
the size of the array as shown in the example bellow :
✓ const int size= 80;
✓ float num[size];
▪ It is very common to use a named constant to set the size of an
array. Because:
✓It can be used to control loops throughout program
✓It will be easy to change if size of the array needs to be
changed .
ASSIGN VALUE TO THE ARRAY
✓ You can initialize C++ array elements either one by one or using a single
statement.
✓ Initially Array can be initialized in similar manner of variable
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
✓ The number of values between braces { } cannot be larger than the number
of elements that we declare for the array between square brackets [ ].
✓ N.B. when we initialize an array, it is optional to give the size, or we can
omit the array size.
Eg:-int odd[]={1,3,5,7};
✓ This means the size of the array is 4
Cont’d….
✓ int odd[]={1,3,5,7,9,11};
✓ The size is 6 but giving the size of the array during initialization is optional,
but when the array is declared only the size of the array is a must.
int mark[];
int mark [5] ✓ correct
✓ You can assign an individual element in the array like the following.
✓ Eg:-
Cont…
✓ You can insert (read) value form keyboard for individual element of an array.
✓ A value can be read into an array element directly using cin.
cin>>mar[4];
✓ You can insert all the array data.
✓ Reading from the keyboard is performed using for loops.
✓ Example: to read 10 elements and store it in any array whose name is odd.
for( int i=0;i<=10;i++)
cin>>odd[i];
Accessing Array Elements
✓The 1st element in an array in C++ always has the index 0, and if
the array has n elements, the last element will have the index of
(n-1) i.e. 0 → n-1 index number.
✓An array element is accessed by writing the identifier of the
array followed by the subscript / index in square brackets ([]).
✓This is done by placing the index of the element within square
brackets after the name of the array.
✓ Generally to access an individual element: name[index];
✓For example:
double salary = balance[4];
Cont…
✓ You can perform any operation on the array data by accessing individual
elements.
✓ The whole elements of an array can be accessed using the looping
statements.
✓ Example: to display all the elements of an array whose name is odd which
has 8 elements are.
int odd[8]={10,45,56,78,23,56,23,45};
for( int i=0;i<=7;i++)
cout<<odd[i]<<endl;
Examples
✓See this example for initialize and accessing of array element.
#include <iostream>
using namespace std;
#include <iostream>
int main() {
using namespace std;
int X[3];
int main() {
X[0]=3;
int x[3]={3,4,5};
X[1]=4;
cout<<x[2]<<endl;
X[2]=5;
return 0;
cout<<X[2]<<endl;
}
return 0;
}
Exercises
✓ Write a c++ program which initializes the 1st 10 odd numbers and display
that number using array?
✓ Write a c++ program which stores or initializes the marks of five students
and display them?
✓ Write a c++ program which reads 10 integer values and display them using
array.
✓ Write a c++ program that reads 10 integer values and display elements that
are greater than 55.
PROGRAMING PRACTICE WITH ARRAY
1. Find the maximum in the list?
const int SIZE = 10;
float sales[ SIZE ] =
{544.53,952.77,222.00,809.89,2007.88,101.67,999.99,327.95,100
0.45,548.95};
float maxSale=sales[0];
for (int i = 1; i < =SIZE-1; i++) {
if(maxSale<sales[i])
maxSale=sales[i];
}
cout<<"Your maximum Sale is:\t"<<maxSale<<endl;
CONT..
2. Find the index of the element in the list that has the
maximum data?
int maxIndex=0;
for (int i = 1; i < =SIZE-1; i++) {
if(sales [maxIndex]<sales[i])
maxIndex=i;
}
cout<<"Your Minimum sales element is\t
sales["<<minIndex<<"]"<<endl;
TWO DIMENSIONAL ARRAY
CONT…
Initialize 2D
▪ You can assign in 2D array
▪ During declaration by using (=).
✓ It must give the size of array.
int the Array [5][3] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
▪ By using cin>> statment
✓ it need nested looping statement
CONT..
Int x[5][2];
for ( int i=0; i<5; i++)
for (int j = 0; j<2;j++)
{
cin>>x[i][j];
}
}
ACCESSING 2D ARRAY
CONT…
▪ When we needs to access all element at time it needs to a
nested looping statement.
▪ The first loop iterate on row and the second or inter loop
iterate based on the column.
1. summing up data in each row of a 2d array ?
int a[3][2];
int total, i, j;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++){
cout<<"enter num\t";
cin>>a[i][j];
}
cout<<"\n\n";
cout<<"Colun1\tColumn2\tSum\n";
for ( i = 0; i < 3; i++){
total=0;
for ( j = 0; j < 2; j++){
cout<<a[i][j]<<"\t";
total+=a[i][j];
}
cout<<total<<endl;
}
NEXT…
STRING