You are on page 1of 6

Lecture 7 ARRAYS

ARRAY

• Array is a fixed size sequenced collection of (subscripted) data storage locations (variables), each
having the same data type and the same name.

• An array is an indexed data structure to represent several variables having the same data type:
int y[100];
• An array is a group of consecutive memory locations with same name and data type.

• Each storage location in an array is called an array element.

• While the complete set of values is referred to as an array, an individual value is called element.

Suppose, you need to store years of 100 cars.


Will you define 100 variables?

int y1, y2,…, y100;

What if you need to store years of 100 cars for


each manufacturer?

int isuzu1,isuzu2,… isuzu100;

int honda1, honda2,… honda100;

int toyota1, toyota2,… toyota100;

Make use of an ARRAY.

Advantages / Uses of Arrays

• Arrays can store a large number of value with single name.


• Arrays are used to process many value easily and quickly.
• The values stored in an array can be sorted easily.
• The search process can be applied on arrays easily.

So, how is it different from simple variables?

Simple variable is a single memory location with unique name and a type. But an Array is collection of
different adjacent memory locations. All these memory locations have one collective name and type.

1 | Programming
OUTPUT:

Syntax:

data_type array_name[array_size];

where: Examples:
int numcollect[5]; Declares an array
• data_type –declares the base type of the
named numcollect with
array, which is the type of each element in char word[10];
the array 5 elements with no
• array_name- the name of the array long telnos[15]; values.
• array_size- a positive integer or expression int studentAge[4];
which defines the array size or the number studentAge[0] = 14;
of elements the array will hold. studentAge[1] = 13;
studentAge[2] = 15;
studentAge[3] = 16;
Syntax:

data_type array_name[array_size] = { List of values };

where: Examples:
int numcollect[5] = {12,15,36,18,9};
• List of values - Values to initialize the array

Elements of an array

The number in brackets that follows an array’s name is called subscript.


This number can identify the number of individual elements in the array.
Therefore:
numcollect[0] – first element of the array with a subscript of 0
numcollect[size-1] – last element of the array with a subscript of size -1

2 | Programming
Array elements may be used in programs just like any other C simple variables.
• Examples of executable statements that makes use of array elements:
a = number[0] + 10;
number[4] =number[0] + number[2];
number[2] = x[5] +y[10];
value[6] = number[i]*3;

Declares an array named numcollect with 5 elements with values


12,15,36,18 and 9
numcollect[0] – first element and will contain the value of 12

numcollect[1] -2nd element

numcollect[2] -3rd element

numcollect[3] -4th element

numcollect[4] – last element and will contain the value of 9

Once an array is created, its size is fixed. It cannot be changed.


➢ You cannot insert any number to numcollect[5] location because it is not initialized.
Can there be only one subscript for every array?

Types of arrays

Single Dimensional Array Syntax: Examples:


float expenses[12];
➢ Has only one subscript data_type array_name[array_size];
expenses [1]=89.95;
or expenses[i]=100;

data_type array_name[array_size] expenses[2+3]=100;


= { List of values };
expenses[a[2]]=10;

int a[10] = {1,2,3,4,5};

Multi-dimensional Syntax: Examples:


int arr[4][3]={ {12,5,22}, {95,3,41},
➢ Array-has more than data_type {77,6,53}, {84,59,62} };
one subscript. array_name[row_size][column_size];
➢ A two-dimensional int matrix[10] [10];
array has two
subscripts. for (i=0; i<10; i++)
or
➢ A three-dimensional for (j=0; j<10; j++)
array has three data_type
subscripts,…and so on. array_name[row_size][column_size] {
={
matrix[i] [j] = i * j;
{List of values in row 0},
}
{List of values in row 1},
float mat[5] [5];
{List of values in row n},

};

3 | Programming
Initialization of arrays can either be at:
• Compile time
• Run time
Compile time initialization
• Array elements are initialized in the source code before compilation.
Run time initialization
• Array elements are initialized after the source code is compiled and the program is ran.

Run time
initialization

OUTPUT:

Compile time
initialization

OUTPUT:

4 | Programming
Searching In Array

Searching is a process of finding the required data in the array. Searching becomes more important when the
length of the array is very large.
There are two techniques to searching elements in array as follows:
• Sequential search
• Binary search

Sequential Search
Sequential search is also known as linear or serial search. It follows the following step to search a value in array.
➢ Visit the first element of array and compare its value with required value.
➢ If the value of array matches with the desired value, the search is complete.
➢ If the value of array does not match, move to next element and repeat same process.

Binary search

Binary search is a quicker method of searching for value in the array. Binary search is very quick but it can only
search a sorted array. It cannot be applied on an unsorted array.

o It locates the middle element of array and compare with desired number.
o If they are equal, search is successful and the index of middle element is returned.
o If they are not equal, it reduces the search to half of the array.
o If the search number is less than the middle element, it searches the first half of array.

Otherwise it searches the second half of the array. The process continues until the required number is found or
loop completes without successful search.

Sorting Arrays

Sorting is a process of arranging the value of array in a particular order. An array can be sorted in two order.

Techniques of Sorting Array

There are two techniques of sorting array:

1. Selection Sort

Selection sort is a technique that sort an array. It selects an element in the array and moves it to its proper
position. Selection sort works as follows:

1. Find the minimum value in the list.


2. Swap it with value in the first position.
3. Sort the remainder of the list excluding the first value.

2. Bubble Sort

Bubble Sort is also known as exchange sort. It repeatedly visits the array and compares two items at a time. It
works as follows:

o Compare adjacent element. If the first is greater than the second, swap them.
o Repeat this for each pair of adjacent element, starting with the first two and ending with the last
two. (at this point last element should be greatest).
o Repeat the step for all elements except the last one.
o Keep repeating for one fewer element each time until there are no pairs to compare.

5 | Programming
Run time
initialization
OUTPUT:

Sequential
Search

OUTPUT:

6 | Programming

You might also like