0% found this document useful (0 votes)
6 views19 pages

Module3 - Arrays

BPOPS_ARRAYS

Uploaded by

bhavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views19 pages

Module3 - Arrays

BPOPS_ARRAYS

Uploaded by

bhavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

Module3

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

One dimensional array:

An array which has only one subscript is known as one dimensional array.

Declaring an one dimensional array:

Syntax

datatype array_name[size];
where,

datatype : It indicates the type of data stored in it. It can be int,float,char,double.


array_name : It is the name of the array and it should be an valid identifier.
Size : An integer constant that indicates the total number of elements in array.
Here, index starts with 0 and ends with (size-1).

Example : int a[5];

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

Mrs. Bhavya P S.,CSE dept. Page 1


Module3
1. Compile time initialization
a) Initializing all elements of an array.

 In this type of array initialization, initialize all the elements of specified memory size.
 Example : int a[5]={2,4,34,3,4};

b) Partial array initialization:


 If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
 The remaining elements will be initialized to zero automatically by the compiler.
 Example : int a[5]={10,20};

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]

d) Assigning Values to individual elements of an array.


 Using assignment operators, we can assign values to individual elements of arrays.
 For example:
int a[3];
a[0]=10;
a[1]=20;
a[2]=30;
10 20 30
a[0] a[1] a[2]

2. Run time initialization:


 An array can be filled by inputting values from the keyboard.
 Example: initializing using for loop
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);

Mrs. Bhavya P S.,CSE dept. Page 2


Module3

Accessing elements of an array :

 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].

Calculating the length of the array:

 The formula to calculate length of the array is


Length = upper_bound-lower_bound+1
Where,
upper_bound = index of last element in the array
lower bound= index of first element in the array.

 Example : 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, length= 4-0+1=5.

Memory occupied by 1D array:

Total memory occupied is given by

Total memory = array_size * size of data_type


 Example : Example : int a[5]={10,11,12,13,14,15}

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

Address of a[4] is 1008

Mrs. Bhavya P S.,CSE dept. Page 3


Module3

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.

 The element to be searched is called key element.

 There are two methods of searching:

1] Linear search.
2] Binary search

Mrs. Bhavya P S.,CSE dept. Page 4


Module3

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.

Mrs. Bhavya P S.,CSE dept. Page 5


Module3

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”);

Mrs. Bhavya P S.,CSE dept. Page 6


Module3

Advantages of binary search


1. Simple technique
2. Very efficient searching technique
Disadvantages
1. The elements should be sorted.

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]);
}
}

Mrs. Bhavya P S.,CSE dept. Page 7


Module3

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]);
}

Two dimensional arrays

Declaration and initialization :


An array which has only one subscript is known as one dimensional array.

Declaring a two dimensional array:

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.

Mrs. Bhavya P S.,CSE dept. Page 8


Module3
Row_size : number of values stored in rows and ranges from ‘0’ to ‘row_size-1’.
Column_size : number of values stored in columns and ranges from ‘0’ to ‘column_size-1’.

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

0 a[0][0] a[0][1] a[0][2] ………. a[0][n-1]

1 a[1][0] a[1][1] a[1][2] ………. a[1][n-1]

2 a[2][0] a[2][1] a[2][2] ………. a[2][n-1]

. . . . .
. . . . ………. .
. . . . .
m-1 a[m-1][0] a[m-1][1] a[m-1][2] ………. a[m-1][n-1]

Example : int a[2][3];

Initialization of 2D 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

a) Initializing all elements of an array:


 In this type of array initialization, initialize all the elements of specified memory size.
 Example : int a[2][2]={2,4,5,6};
This can also be written as
int a[2][2]={{2,4},{5,6}};

Mrs. Bhavya P S.,CSE dept. Page 9


Module3
2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]

b) Partial array initialization:


 If the number of values to be initialized is less than the size of array then it is called as partial
array initialization.
 The remaining elements will be initialized to zero automatically by the compiler.
 Example : int a[2][2]={{2,4}};

2 4
a[0][0] a[0][1]
0 0
a[1][0] a[1][1]

c) Initialization without size:


 In two dimensional array the first dimension can be omitted.
 Example: int a[ ][2]={2,4,5,6};
 In the above example the first dimension is set to ‘2’ by the compiler.

2 4
a[0][0] a[0][1]
5 6
a[1][0] a[1][1]

d) Assigning Values to arrays


 Using assignment operators, we can assign values to individual elements of arrays.
 For example:

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]

2. Run time initialization


 An array can be filled by inputting values from the keyboard.
 Example: initializing using for loop
for(i=0;i<5;i++)
for(j=0;j<5;j++)

Mrs. Bhavya P S.,CSE dept. Page


10
Module3
scanf(“%d”,&a[i][j]);

Example: Write a C program to read and display two dimensional matrix


#include<stdio.h>
int main()
{
int a[10][10],m,n,i,j;

printf("enter the order of matrix A");


scanf("%d%d",&m,&n);

printf("Enter the elements of the matrix\n");


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);

printf("The given matrix A is \n");


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf(" %d\t", a[i][j]);
}
printf("\n");
}

Example Programs:

1. Write a C program to find sum of two matrices.

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 row_size and column_size


printf("Enter the row_size and column_size of matrix A and B");
scanf("%d%d", &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]);
}
}

// adding two matrices


for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];

// printing the result


printf("\n resultant matrix is");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}

OR
#include <stdio.h>
int main()
{
int a[100][100], b[100][100], c[100][100], i, j, m, n;

// reading row_size and column_size


printf("Enter the row_size and column_size of matrix A and B");
scanf("%d%d", &m,&n);

// Reading matrix A
printf("\nEnter elements of matrix A\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)

Mrs. Bhavya P S.,CSE dept. Page


12
Module3
scanf("%d", &a[i][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]);

// adding two matrices


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}

// printing the result


printf("\n resultant matrix is");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}

2. Write a C program to subtract two matrices.

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 row_size and column_size


printf("Enter the row_size and column_size of matrix A and B");
scanf("%d%d", &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)

Mrs. Bhavya P S.,CSE dept. Page


13
Module3
for (j = 0; j < n; ++j)
scanf("%d", &b[i][j]);

// subtracting two matrices


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
c[i][j] = a[i][j] - b[i][j];
}
}

// printing the result


printf("\n resultant matrix is");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}

3. Write a C program to find product of two matrices.

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;

printf("enter the number of rows and columns of matrix A");


scanf("%d %d",&m,&n);

printf("enter the number of rows and columns of matrix B");


scanf("%d %d",&p,&q);

if(n=!p)
{
printf("matrix multiplication is not possible");
return 0;
}

printf("enter the elements of matrix A=\n");


for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);

printf("Elements of matrix A are\n");


for (i = 0; i < m; i++)
{

Mrs. Bhavya P S.,CSE dept. Page


14
Module3
for (j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf(“\n”);
}

printf("enter the second matrix element=\n");


for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);

printf("Elements of matrix B are\n");


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d\t", b[i][j]);
}
printf(“\n”);
}

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];
}
}
}

printf("resultant matrix is");


for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}

4. Write a C program to find transpose of a matrix.

Transpose of a mxn matrix A can be given as a nxm matrix B, where Bi,j=Aj,i .

Program:
#include <stdio.h>
int main()
{
int a[10][10],b[10][10],i, j, m, n;

Mrs. Bhavya P S.,CSE dept. Page


15
Module3
printf("Enter the order of the matrix A \n");
scanf("%d %d", &m, &n);

printf("Enter the elements of the matrix\n");


for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
scanf("%d", &a[i][j]);

printf("The given matrix A is \n");


for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", a[i][j]);
}
printf("\n");
}

for (i = 0;i < m;i++)


{
for (j = 0; j < n; j++)
{
b[j][i] = a[i][j];
}
}

// printing the transpose of a matrix


printf("Transpose of matrix is \n");
for (i = 0; i< n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d\t ", b[i][j]);
}
printf("\n");
}

Multidimensional arrays :

A multidimensional array is an array with more than two dimension.

Declaration :

Data_type array_name [size1][size2]…..[sizeN];

Mrs. Bhavya P S.,CSE dept. Page


16
Module3

Program :
#include<stdio.h>
int main(){
int a[10][10][[10],m,n,p,i,j,k;

printf("enter the order of matrix A");


scanf("%d %d%d",&m,&n,&p);

printf("Enter the elements of the matrix\n");


for (i = 0; i < m; i++)

for (j = 0; j < n; j++)


for(k=0;k<p;k++)
scanf("%d", &a[i][j][k]);

printf("The given matrix A is \n");


for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
for (k= 0; k < p; k++)
printf(" %d\t", a[i][j][k]);
}
printf("\n");
}

Application of Arrays:
Homework

Lab program(Module2)

Example: write a c program to generate pascal’s triangle

Mrs. Bhavya P S.,CSE dept. Page


17
Module3
Pascal’s Triangle Properties

 Each number is the sum of the two numbers above it.
 The outside numbers are all 1.
 The triangle is symmetric.
 The first diagonal shows the counting numbers.
 The sums of the rows give the powers of 2.
 Each row gives the digits of the powers of 11.
 Each entry is an appropriate “choose number.”
 And those are the “binomial coefficients.”
 The Fibonacci numbers are there along diagonals.
Pascal’s triangle formula

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”);
}

write a c program to compute binomial coefficients.


Binomial coefficient (n, k) is the order of choosing ‘k’ results from the given ‘n’ possibilities. The value
of binomial coefficient of positive n and k is given by

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”);
}

Mrs. Bhavya P S.,CSE dept. Page


19

You might also like