Module3 - Arrays
Module3 - Arrays
MODULE 3
ARRAYS
Introduction :
An array is a collection of similar data elements of same data type stored at consecutive
memory locations.
All the elements of the array share a common name .
Each element in the array can be accessed by the subscript(or index) and array name.
The arrays are classified as:
1. One dimensional array
2. Two dimensional array
3. Multidimensional array
An array which has only one subscript is known as one dimensional array.
Syntax
datatype array_name[size];
where,
Here, we declared an array ‘a’ of integer type and its size is ‘5’, meaning that it can hold five integer
values. It allocates 5*2=10 Bytes of memory for the array.
Initialization of 1D array:
To store the values in array, there are mainly two methods.
1. Compile time initialization
a) Initializing all elements of an array.
b) Partial array initialization.
c) Initialization without size.
d) Assigning Values to individual elements of an arrays.
2. Run time initialization
In this type of array initialization, initialize all the elements of specified memory size.
Example : int a[5]={2,4,34,3,4};
10 20 0 0 0
a[0] a[1] a[2] a[3] a[4]
c) Initialization without size:
If the size of an array is not initialized, then the compiler will set the size based on the number
of initial values.
Example: int a[ ]={10,20,30,40,50};
In the above example the size of an array is set to 5.
10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
For accessing an individual element of the array, the array subscript must be used.
For example, consider
int a[5]={10,11,12,13,14,15}
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Here, in order to access fourth element we must write a[3].
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Here, length= 4-0+1=5.
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
Total memory = 5*2=10 bytes.
Problem : given an array int a[5]={10,11,12,13,14,15}, calculate the address of a[4] if base address is
1000.
10 11 12 13 14
a[0] a[1] a[2] a[3] a[4]
1000 1002 1004 1006 1008
Example : Write a C program to read N elements from keyboard and to print N elements on screen.
(Write a C program to read and print 1D array)
#include<stdio.h>
void main()
{
int i,n,a[10];
printf(“enter number of array elements\n”);
scanf(“%d”,&n);
printf(“enter array elements\n”);
for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“array elements are\n”);
for(i=0; i<n;i++)
printf(“%d”,a[i]);
}
Example 2: Write a C program to find sum of n array elements .
#include
void main()
{
int i,n,a[10],sum=0;
printf(“enter number of array elements\n”);
scanf(“%d”,&n);
printf(“enter array elements\n”);
for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“array elements are\n”);
for(i=0; i<n;i++)
printf(“%d”,a[i]);
for(i=0; i<n;i++)
sum=sum+ a[i];
printf(“sum is %d\n”,sum):
}
1. Searching
The process of finding a particular item in the large amount of data is called searching.
1] Linear search.
2] Binary search
A. Linear search:
Linear search is also known as sequential search is a simple searching technique.
In this technique we search for a given key item in linear order i.e, one after the other
from first element to last element.
The search may be successful or unsuccessful.
If key item is present, the search is successful, otherwise unsuccessful search.
Example : Program to implement linear search.
#include <stdio.h>
void main()
{
int i,n,a[30],key,flag=0;;
printf(“enter size of the array\n”);
scanf(“%d”,&n);
printf(“enter array elements\n”);
for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“enter the key element\n”);
scanf(“%d”,,&key);
for(i=0; i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf(“successful search”);
else
printf(“unsuccessful search”);
}
Advantages
Very simple Approach.
Works well for small arrays.
Used to search when elements are not sorted.
Disadvantages
Less efficient if array size is large.
If the elements are already sorted.
linear search is not efficient.
B. Binary Search:
Binary search is a simple and very efficient searching technique which can be applied if
the items are arranged in either ascending or descending order.
In binary search first element is considered as low and last element is considered as high.
Position of middle element is found by taking first and last element is as follows.
mid=(low+high)/2
Mid element is compared with key element, if they are same, the search is successful.
Otherwise if key element is less than middle element then searching continues in left part
of the array.
If key element is greater than middle element then searching continues in right part of the
array.
The procedure is repeated till key item is found or key item is not found.
Example : C program to implement binary search.
#include<stdio.h>
void main()
{
int i, n, low, high, mid,a[50],key;
printf(“enter the number of elements\n”);
scanf(“%d”,&n);
printf(“enter the elements\n”);
for(i=0; i<n;i++)
scanf(“%d”,&a[i]);
printf(“enter the key element\n”);
scanf(“%d”,,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
flag=1;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==1)
printf(“successful search”);
else
printf(“unsuccessful search”);
3. Sorting :
It is the process of arranging elements in the list according to their values in ascending or
descending order.
Ex: Bubble Sort, Selection Sort, Insertion Sort, Shell Sort, Merge Sort, Quick Sort.
A. Selection sort:
Selection sort will start by comparing the first element with other elements and finds
minimum element and swaps, then it starts comparing by taking second element with other
elements and so on.
Example :
#include<stdio.h>
void main()
{
int n, a[100], i, j, temp;
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements of array\n");
for(i=0; i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0; i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Printf(“the sorted elements are\n”);
for(i=0; i<n;i++)
{
printf(“%d”,a[i]);
}
}
B. Bubble sort:
Bubble sort will start by comparing the first element of the array with the second element, if
the first element is greater than the second element, it will swap both the elements, and then
move on to compare the second and the third element, and so on.
Example:
#include<stdio.h>
void main( )
{
int n, a[100], i, j, temp;;
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements of array\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if( a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Syntax
datatype array_name[row_size][column_sze];
where,
datatype : It indicates the type of data stored in it.
array_name : It is the name of the array and it should be an valid identifier.
Consider a 2D array,’a’ of integer data_type which contains ‘m’ rows and ‘n’ columns and can be shown
as int a[m][n], where,
‘m’ represents row, ranges from ‘0’ to ‘m-1’.
‘n’ represents column, ranges from ‘0’ to ‘n-1’.
0 1 2 ………. n-1
. . . . .
. . . . ………. .
. . . . .
m-1 a[m-1][0] a[m-1][1] a[m-1][2] ………. a[m-1][n-1]
Initialization of 2D array:
2 4
a[0][0] a[0][1]
0 0
a[1][0] a[1][1]
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
int a[2][2];
a[0][0] = 2
a[0][1] = 4
a[1][0] = 5
a[1][1] = 6
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]
Example Programs:
Two matrices with same number of rows and columns can be added together thereby storing the result in
third matrix.
Program :
#include <stdio.h>
int main()
{
int a[100][100], b[100][100], c[100][100], i, j, m, n;
// Reading matrix A
printf("\nEnter elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
// Displaying matrix A
printf("Elements of matrix A are\n");
for (i = 0; i < m; i++)
Mrs. Bhavya P S.,CSE dept. Page
11
Module3
{
for (j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf(“\n”);
}
// Reading matrix B
printf("Enter elements of matrix B\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &b[i][j]);
// Displaying matrix B
printf("Elements of matrix B are\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d", a[i][j]);
}
}
OR
#include <stdio.h>
int main()
{
int a[100][100], b[100][100], c[100][100], i, j, m, n;
// Reading matrix A
printf("\nEnter elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
// Reading matrix B
printf("Enter elements of matrix B\n");
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
scanf("%d", &b[i][j]);
Two matrices with same number of rows and columns can be subtracted together thereby storing the
result in third matrix.
Program :
#include <stdio.h>
int main()
{
int a[100][100], b[100][100], c[100][100], i, j, m, n;
// Reading matrix A
printf("\nEnter elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
// Reading matrix B
printf("Enter elements of matrix B\n");
for (i = 0; i < m; ++i)
Two matrices can be multiplied with each other if the number of columns in the first matrix is equal to
the number of rows in the second matrix.
Program :
#include<stdio.h>
int main(){
int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
if(n=!p)
{
printf("matrix multiplication is not possible");
return 0;
}
printf("matrix multiplication=\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
Program:
#include <stdio.h>
int main()
{
int a[10][10],b[10][10],i, j, m, n;
Multidimensional arrays :
Declaration :
Program :
#include<stdio.h>
int main(){
int a[10][10][[10],m,n,p,i,j,k;
Application of Arrays:
Homework
Lab program(Module2)
Program:
#include<stdio.h>
int main()
{
int n, rows, cols, space, ans;
printf(“enter the number of rows”);
scanf(“%d”,&n);
for(rows=0;rows<n;rows++)
{
for(space=1;space<num-rows;space++)
printf(“ “);
for(col=0;col<=rows,col++)
{
if(col==0||rows==0)
ans=1;
else
ans=ans*(rows-col+1)/col;
printf(“%4d”,ans);
}
printf(“\n”);
}
Program:
#include<stdio.h>
int main()
{
int n, rows, cols, ans;
printf(“enter the number of rows”);
scanf(“%d”,&n);
for(rows=0;rows<n;rows++)
{
for(col=0;col<=rows,col++)
Mrs. Bhavya P S.,CSE dept. Page
18
Module3
{
if(col==0||rows==0)
binom=1;
else
binom=binom*(rows-col+1)/col;
printf(“%4d”,ans);
}
printf(“\n”);
}