0% found this document useful (0 votes)
13 views

Unit 04

Uploaded by

Dr D Suneetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit 04

Uploaded by

Dr D Suneetha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter-4

Arrays: Concept, Declaration and Initialization of Arrays, Accessing Individual Elements of Array.
Use of Arrays in Sorting, Searching. Concept of 2-D array (Matrix),
Passing arrays to functions, Examples.

Aims and Objectives


##############
Introduction
###############
In C programming for accessing of multiple variables of same datatype the declaration is
as follows
Let five integer variables
int a1,a2,a3,a4,a5;
Reading the values for all the five elements the statement can be written as
Scanf(“ %d %d %d %d %d”,&a1, &a2, &a3, &a4, &a5);
What, if the number of values is more than 10, we need to declare all the variables
and need to clearly specify all the variables in the corresponding scnaf( ) statement, it will
become hard.
Solution for the above problem is the main aim of implementing arrays.
Defnition: An array is a collection of dataitems of same datatypes are sharing a common
name and stored in contiguous memory locations.
Arrays Concepts
###############
Declaration of Arrays
###############
Initialization of Arrays
###############
Storing Array Elements
###############
Calculating the Length of the Array
###############
USING ARRAYS IN C
#####################
Performing Operations on Arrays
#####################

Arrays Limitations
#####################

Department of BS&H Page 77


NRI Institute of Technology, Pothavarappadu
Types of arrays
Depending on the number of subcripts used, arrays are categorized as follows
1. One-dimensional Arrays
2. Two -dimensional Arrays
3. Multi-dimensional Arrays
One -Dimensional Arrays
An array with only one subscript is called one -dimensional Arrays or 1-D array.
Expand the explanation
#################
Declaration:
The general form of declaring a one -dimensional Array is
Syntax:
Data_type Array_name[size];
Where,
Data_type refers to any datatype supported by C,
Array_name should be a valid idenfier and
Size indicates the maximum number of elements that can be stored inside the
array. it must be a positive integer constant.
Example:
int A[6];
Here, A is declared to be an array of int type and size is 6. Six contiguous memory
locations get allocated as shown below to store 6 integer values
element1 element 2 element 3 element 4 element 5 element 6
A[0] A[1] A[2] A[3] A[4] A[5]
Each data item in an array A is identified by the array name A followed by a pair of
square brackets enclosing a subscript value.the subscript value is indexed from 0 to 5.
i.e,
A[0] indicates element1
A[1] indicates element2
A[2] indicates element3
A[3] indicates element4
A[4] indicates element5

A[5] indicates element6 Size of an array:


The number of bytes allocated for an array is calculated using the formula
Total size = (sizeof(datatype)*array size)Bytes
For the above example
Total size = 2*6
= 12 Bytes
i.e every location occupies 2byes of memory.

Initialization:
Syntax: Data_type Array_name[size]={list of values};
Ex: int x[6]={1,2,3,4,5,6};
Department of BS&H Page 78
NRI Institute of Technology, Pothavarappadu
As a result , memory locations of X filledup as follows:

1 2 3 4 5 6
X[0] X[1] X[2] X[3] X[4] X[5]

Note:
1. if the number of values in initializer is less than the size of an array, only
those
many first locations of the array are assigned the value. The remaining locations are
assigned as zeros.
Example:
int x[6]={1,2,3};
Size of array = 6,
Values initialized =3.
Then the initialization is as follows

1 2 3 0 0 0
X[0] X[1] X[2] X[3] X[4] X[5]

2. if the number of values in initializer is greater than the size of an array ,


compiler raises an error.
Example:
int x[6]={1,2,3,4,5,6,7,8};
Size of array = 6,
Values initialized =8.
Now, the compiler raises an error.
3. If a static array is declared without initializer list then all locations are set to zeros .

Example:
Static int X[6];

0 0 0 0 0 0
X[0] X[1] X[2] X[3] X[4] X[5]

Department of BS&H Page 79


NRI Institute of Technology, Pothavarappadu
4. If size is ommited in a 1-D array declaration which is initialized, the compiler willl
supply this value by examining the number of values in the initializer-list.

Example: 1.
int x[]={1,2,3,4,5,6};
Values initialized =6
Hence, size of array =6
And sizeof(X)=12bytes.

1 2 3 4 5 6
X[0] X[1] X[2] X[3] X[4] X[5]

2.
int x[]={1,2,3,4};
Values initialized =4
Hence, size of array =4
And sizeof(X)=8bytes.

1 2 3 4
X[0] X[1] X[2] X[3]
5. Array elements can not be initialized selectivily

Example:
Int X[5]={ , 20}
An attempt to initialize only second location is illegal.

Accesing elements:
The syntax template for accessing an array component is
ArrayName[ IndexExpression ]

Example:
int x[6]={1,2,3,4,5,6};
in the above statement
x[0]=1
x[1]=2
x[2]=3
x[3]=4
x[4]=5
x[5]=6

example1:
/*example to access 1-D array*/

#include<stdio.h>

Department of BS&H Page 78


NRI Institute of Technology, Pothavarappadu
#include<conio.h>
void main()
{
int A[5]={1,2,3,4,5},i; /*compile-time initialization*/
clrscr();
for(i=0;i<5;i++)
{
printf("A[%d]=%d",i,A[i]);
printf("\n");
}
getch();
}
Ouput:
A[0]=1
A[1]=2
A[2]=3
A[3]=4
A[4]=5

Example2:
/*example to access 1-D array @ runtime initialization*/

#include<stdio.h>
#include<conio.h>
void main()
{
int A[5],i; /*array declaration*/
clrscr();
for(i=0;i<5;i++) /*reading values for array*/
{
printf("enter A[%d]\n”,i);
scanf(“%d”,&A[i]);
}
printf(“entered values are\n”);
for(i=0;i<5;i++) /*accessing the values of an
array*/
{
printf("A[%d]=%d\n”,i,A[i]);
}

getch();
}
Ouput:
Enter A[0]
10

Department of BS&H Page 79


NRI Institute of Technology, Pothavarappadu
Enter A[1]
20
Enter A[2]
30
Enter A[3]
40
Enter A[4]
50
entered values are
A[0]=10
A[1]=20
A[2]=30
A[3]=40
A[4]=50

Example3:
//Sum of the elemets in an array

#include<stdio.h>
#include<conio.h>
void main()
{
int A[5],i,sum=0; /*array declaration*/
clrscr();
for(i=0;i<5;i++) /*reading values for array*/
{
printf("enter A[%d]\n",i);
scanf("%d",&A[i]);
}
for(i=0;i<5;i++) /*accessing the values of an
array*/
{
sum=sum+A[i];
}
printf("Sum of elemets in array is %d",sum);
getch();
}
Ouput:
Enter A[0]
10
Enter A[1]
20
Enter A[2]
30
Enter A[3]

Department of BS&H Page


NRI Institute of Technology, Pothavarappadu 80
40
Enter A[4]
50
Sum of elements is 150

Example 4:
//minimum and maximum elemets in Array

#include<stdio.h>
#include<conio.h>
void main()
{
int min=0,max=0,i,a[5];
clrscr();
for(i=0;i<5;i++) //reading array elements
{
printf("Enter the A[%d] elements ",i);
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
if(a[i]<min) //checking for minimum element
min=a[i];
if(a[i]>max) //checking for maximum element
max=a[i];
}
printf("\n Minimum element is %d",min);
printf("\n Maximum element is %d",max);
getch();
}

Ouput:
Enter A[0]
10
Enter A[1]
20
Enter A[2]
30
Enter A[3]
40
Enter A[4]
50
Maximum element is 50
Minimum element is 10

Department of BS&H Page 81


NRI Institute of Technology, Pothavarappadu
Two-Dimensional arrays:
The array which is used to represent and store data in a tabular form is called as 'two
dimensional array.' Such type of array specially used to represent data in a matrix form.
The following syntax is used to represent two dimensional array.
Expand the explanation
#################

Syntax:

<data-type> <array_nm> [row_subscript][column-subscript];

Example:

int a[3][3];

In above example, a is an array of type integer which has storage size of 3 * 3 matrix. The
total size would be 3 * 3 * 2 = 18 bytes.
Memory Allocation :

Fig : Memory allocation for two dimensional array

Program :

/* Program to demonstrate two dimensional array.

#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
clrscr();
printf("\n\t Enter matrix of 3*3 : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
Department of BS&H Page 82
NRI Institute of Technology, Pothavarappadu
{

Department of BS&H Page 83


NRI Institute of Technology, Pothavarappadu
scanf("%d",&a[i][j]); //read 3*3 array
}
}
printf("\n\t Matrix is : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("\t %d",a[i][j]); //print 3*3 array
}
printf("\n");
}
getch();
}
Output :
Enter matrix of 3*3 : 3 4 5 6 7 2 1 2 3

Matrix is :
3 4 5
6 7 2
1 2 3
Limitations of two dimensional array :
o We cannot delete any element from an array.
o If we dont know that how many elements have to be stored in a memory in
advance, then there will be memory wastage if large array size is specified.
ARRAY APLICATIONS:
Matrix Addition:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter size of the matrix ");
scanf("%d",&n); //reading size of the matrix
printf("Enter Matrix 1 elements ");
for(i=0;i<n;i++) //for reading each row
{
for(j=0;j<n;j++) //for reading columns
{
scanf("%d",&a[i][j]); //reading the elements of matrix 1
}
}
printf("Enter Matrix 2 elements ");
for(i=0;i<n;i++) //for reading each row

Department of BS&H Page 84


NRI Institute of Technology, Pothavarappadu
{
for(j=0;j<n;j++) //for reading columns
{
scanf("%d",&b[i][j]); //reading the elements of matrix 2
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j]; //adding 1,2 matrices
}
}
printf("Sum of two matrices is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]); //displaying of matrices
}
printf("\n"); //It is for going to next line after each row printing
}
getch();
}
Matrix Multiplication:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter size of the matrix ");
scanf("%d",&n); //reading size of the matrix
printf("Enter Matrix 1 elements ");
for(i=0;i<n;i++) // for going to each row
{
for(j=0;j<n;j++) //for going to each column
{
scanf("%d",&a[i][j]); //reading the elements of matrix 1
}
}
printf("Enter Matrix 2 elements ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

Department of BS&H Page 90


NRI Institute of Technology, Pothavarappadu
{
scanf("%d",&b[i][j]); //reading the elements of matrix 2
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{ c[i]
[j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j]; //multiplying 1,2 matrices
}
}
}
printf("Sum of two matrices is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",c[i][j]); //displaying of matrices
}
printf("\n"); //It is for going to next line after each row printing
}
getch();
}
Symetricity Of A Matrix:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10][10],b[10][10],flag=0;
clrscr();
printf("Enter size of the matrix ");
scanf("%d",&n); //reading size of the matrix
printf("Enter Matrix elements ");
for(i=0;i<n;i++) //for reading each row
{
for(j=0;j<n;j++) //for reading columns
{
scanf("%d",&a[i][j]); //reading the elements of matrix 1
}
}
for(i=0;i<n;i++)
{

Department of BS&H Page 91


NRI Institute of Technology, Pothavarappadu
for(j=0;j<n;j++)
{
b[j][i]=a[i][j]; //creating transpose matrix
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=b[j][i]) //It transpose is not equal to the original
matrix
{
flag=1; //set the flag to 0 to print it is
not symmentric break; //break
the loop..
}
}
}
if(flag==1)
printf(" Matrices are not Symmetric ");
else
printf(" Matrices are Symmetric ");
getch();
}

Character Arrays
########################
Inter function communication
########################
Use of Arrays in Sorting, Searching. Concept of 2-D array (Matrix), Passing arrays to
functions, Examples.
########################

Department of BS&H Page 92


NRI Institute of Technology, Pothavarappadu
*********
Summary
################
Review Questions
################
Multiple Choice Questions
#####################
(From placement cell) minimum50 -100 bits as per syllabus

Department of BS&H Page 93


NRI Institute of Technology, Pothavarappadu

You might also like