You are on page 1of 19

Arrays

by Dr. Karuna
BASIC CONCEPT OF ARRAYS
• Consider a program to find average
temperature in a week.
• Here we need to store 7 floating point numbers.
• If we use simple variable and data type
concepts, then we need 7 variables of float data
type
• Now let's assume we have to compute average
temperature of an year , so what is next? Are
we going to use 365 variables?
BASIC CONCEPT OF ARRAYS
• Thus we can conclude here that:
– It's difficult to program with too many
declarations.
– It's not recommended way of programming.
– It's not the right way of programming.
• To handle such situation, C language provides
a concept called the arrays.
ARRAY
• Array is a collection of homogeneous elements, stored sequentially
one after the other in memory.
• RULES TO BE FOLLOWED WHILE USING ARRAYS:
– The array should be declared with some data type.
– All the arrays elements should be of same data type.
– The size of the array is finite and it should be specified at the
time of its declaration
– In an array elements are stored sequentially that is in contiguous
memory locations
– Only one element can be added or removed from the array at a
time.
– The subscript of first item is always zero.
– Each data item is accessed using the name of the array but with
different subscripts
– The index of the array is always an integer.
Storing Values in Arrays
• The values can be stored in an array
using following two methods:
• 1) Compile time Initialization:
– i. Initialization .
– ii. Assigning values
• 2) Run Time Initialization:
– i. Input values from keyboard
Storing Values in Arrays
1) Compile time Initialization:
i. Initialization of One-Dimensional Array
• The syntax is as shown below:
– data_type array_name[array_size] = {v1, v2, v3,..., vN};
• where v1, v2, v3, ..., vN are values
• Ex: int a[5] = { 21, 45, 34, 56, 67 };
• The above code can be pictorially represented as shown
below:

• Note: Here even the declaration int a[ ] = { 21, 45, 34, 56,
67 }; is correct because based on number of elements
specified the size will be computed automatically
Storing Values in Arrays
1) Compile time Initialization:
Storing Values in Arrays
1) Compile time Initialization:
ii. Assigning values to One-Dimensional Array
• Example:
int age[5];
age[0] = 22;
age[1] = 44;
age[2] = 34;
age[3] = 35;
age[4] = 47;
Storing Values in Arrays
2) Run Time Initialization:
i. Input values from keyboard
• Example:
int age[5]
scanf("%d", &age[0]);
scanf("%d", &age[1]);
scanf("%d", &age[2]);.
scanf("%d", &age[3]);
scanf("%d", &age[4]);
Accessing of Array elements
Example: Program to illustrate assigning values to and accessing values of
one-dimensional array.
#include<stdio.h>
void main()
{
int age[5];
age[0] = 22; age[1] = 44; age[2] = 34; age[3] = 35; age[4] = 47;
printf("Value in array age[0] : %d \n", age[0]);
printf("Value in array age[1] : %d \n", age[1]);
printf("Value in array age[2] : %d \n", age[2]);
printf("Value in array age[3] : %d \n", age[3]);
printf("Value in array age[4] : %d ",age[4]);
}
Note: Not an efficient method to access the elements from arrays
Storing and Accessing of Array elements
using looping statements:
Example: Program to illustrate reading/writing to one-dimensional array.
#include<stdio.h>
void main()
{
int age[5], i;
printf("enter 5 numbers:\n”);
for(i=0;i<5;i++) //
{
scanf("%d", &age[i]);
}
for(i=0;i<5;i++)
{
printf("Value in array age[%d] : %d \n ", i, age[i]);
}
}
Example: Program to find maximum
element in an array of n integer elements
#include<stdio.h>
int main ()
{
int a[50],n,max, i;
printf(“\n\tEnter the number of elements: “);
scanf(“%d”,&n);
printf(“\n\tEnter %d Elements of array: “, n);
for ( i = 0; i < n; i++ )
scanf(“%d”,&a[i]);
max = a[0];
for (i = 1; i < n; i++ )
{
if(a[i] > max)
max = a[i];
}
printf(“ Maximum Element of an array =%d ", max );
return 0;
}
Searching Problem
• This is the problem of finding the given key element in a list of
elements. When the key element is found in the list we call
Search as Successful Search otherwise Unsuccessful Search.
Two simple Searching Methods which are commonly used
are:
• Linear / Sequential Search
• Binary Search

• Linear / Sequential Search


• It compares each elements of the array with key element
Sequentially / Linearly (one element at a time) and it STOPS
when Key is Found and declare Successful Search, It also
STOPS when Array exhaust and declare Unsuccessful Search.
Searching Problem
• The Pseudocode of Linear/Sequential Search is:
• Algorithm : Linear_Search( )
Begin
read the number of elements n
read n elements of an array a
read the key element to be searched
flag = FALSE // Assume key is not found
for i = 0 to n-1 do //compare key with all the elements
if a[i] == key then
flag = TRUE break
end if
end for
if flag == TRUE then
print “Successful Search”
else
print “UnSuccessful Search”
end if
End
Example: Program to find given key element in an
array of n integer elements using Linear Search
#inclde<stdio.> for (i = 0; i < n; i++ )
int main () {
{ if(a[i] == key)
int a[50], n, min, i, flag = 0,key; {
printf(“\n\tEnter the number of flag = 1;
elements: “); break;
scanf(“%d”, &n); /* 1 ≤ n ≤ 50 */ }
printf(“\n\tEnter %d Elements of }
array: “, n); if( flag == 1)
for ( i = 0; i < n; i++ ) printf(“Successful Search - Key found ”);
scanf(“%d”, &a[i]); else
printf(“\n\tEnter the Key element to printf(“Unsuccessful Search - Key NOT
be Searched: “); found”);
scanf(“%d”, &key); return 0;
}

You might also like