You are on page 1of 4

Lesson 3: Arrays data structures

3.1.Introduction
An array is a data structure that stores variables of the same type in contiguous
memory locations. The data items in array are ordered. It removes the cumbersome
task of defining separate variables for each data item. A single variable is therefore used
to store one or more data items.
3.2.Lesson objectives
By the end of this lesson the learner will be able to:
Define an array
Declare and access array elements
Describe advantages and disadvantages of arrays
3.3. Lesson outline
This lesson is organized as follows:
3.1. Introduction
3.2. Lesson objectives
3.3. Lesson outline
3.4. Declaration
3.5. Initialization
3.6. Accessing array elements
3.7. Classifying arrays
3.8. Array operations
3.9. Revision questions
3.10. Summary
3.11. Suggested reading
3.4. Declaration
The general form of the single dimension array is:-
Type Array_name [size];
The type is the base type of the array and is the type of each of the element of the
array. The size is the number of the elements in the array. The Array_name is the
name of the identifier holding the array. For example,
int age [10];
3.5. Initialization
There are three ways to initialize an array.
a) Declare and initialize array in one statement.
b)Declare and initialize array separately.
c) Declare and initialize array in one statement without specifying the size.
Examples:
a)Declaring and initializing array in one statement
int age [5] = {30, 22, 33, 44, 25};
b) Declaring and initializing array separately
int age [5];
age [0]=30; age
[1]=22; age
[2]=33; age
[3]=44; age
[4]=25;
c)Declare and initialize array in one statement without specifying the size.
int Age [] = {30, 22, 33, 44, 25};
3.6.Accessing array elements
We can access array elements by index, and first item in array is at index 0. First
element of array is called lower bound and it’s always 0. Highest element in array is
called upper bound.
To locate the value of the ith. member of the array, type Array_name [index]. For
example age[3] will access the fourth member of the array age[10] in example above.
Once you can locate a member of the array, you can display its value using cout.
3.7.Classification of arrays
Arrays can be classified according to:
[a]. Dimensions
One-dimensional arrays stores values in a straight line.

Example int num[7] Example:


//program to calculate the average score in 10 scores
//using arrays
#include<iostream> using
namespace std; void
main()
{ int i; float
score[10];
float sum,avg;
sum=0;
for(i=0;i<10;i++)
{
cout<<"Enter score number "<<i+1<<endl;
cin>>score[i];
sum=sum+score[i];
}
avg=sum/10;
cout<<"The average score is "<<avg<<endl;
}
Two dimensional arrays stores values in rows and columns. Each column represents
one category of data that every row shares with the other rows.
Example int num[5][4]

Example
//program to print out multiplication table
//using arrays
#include<iostream> using
namespace std; void
main()
{ int
i,j;
float num[3][5]; //3x5 table
for(i=0;i<3;i++) //iterates through rows
{
for(j=0;j<5;j++)//iterate throgh columns
{
num[i][j]=(i+1)*(j+1); //multiply row value by column value
//then assign to array index
}
}
for(i=0;i<3;i++) //iterate through rows
{
for(j=0;j<5;j++) //iterate through columns
{
cout<<num[i][j]<<" , "; //print out values inside array index
}
cout<<endl; //new line
}
}
[b]. Memory allocation strategy
Static array: The memory size allocation is done during compilation and cannot change
during running time.
Dynamic array: The memory size allocation can be done during running time.
3.8. Array operations.
Just like any other variable arrays can be used in calculations. Once you declare and/or
initialize the elements you can access them.
Some of the operations are subtraction, addition, multiplication and division of
elements.
3.9. Revision questions
[i]. Define array data structure
[ii]. Describe any two advantages and two disadvantages of array data structure
[iii]. You are required to develop an algorithm to compute total scores of 200 students.
Use a pseudo code to develop the algorithm.
[iv]. Implement the algorithm above using c++
[v]. Explain how you can reference array elements in 1D array.
3.10. Summary
In this lesson, we have learnt how to declare, initialize and reference array elements.
We have also discussed ways of classifying arrays. Finally we have looked at some of
the operations you can perform with arrays.
3.11. Suggested reading
[1]. Data structures using C and C++, 2nd Edition by Yedidyah Langsam, Aaron J.Augenstein
and Aaron M.Tenebaum: Pubslisher: Pearson.
[2]. Data structures and algorithms in c++ by Michael T.Goodrich,Robertio Tamassia and David
Mount: Publisher: Wiley
[3]. Fundamentals of data structures in c++ by Ellis Horowitz,Sartaj Sahni and Dinesh Mehta.
Publisher: Galgotia
[4]. Introduction to data structures and algorithms with c++ by Glenn W.Rowe . Publisher:
Prentice Hall.

You might also like