You are on page 1of 26

ARRAYS

INTRODUCTION
• We have used the fundamental data types, char,
int, float, double.
• These types are constrained by the fact that a
variable of these types can store only one value at
any given time
• In many applications, we need to handle large
volumes of data
• To process such large amounts of data, we need a
powerful data types that would facilitate efficient
storing, accessing and manipulation of data items
INTRODUCTION
• C supports a derived data type known as array.
• An array is a fixed-sized sequenced collection of
elements of the same data type.
• It is simply a grouping of like-type data
• Example:
– List of numbers
– List of names
– List of employees in an organization
– List of products and their cost sold by a store and
so on
• Array provides an convenient structure for
representing data, it is classified as one of the
INTRODUCTION

An array is a group of related data items that share a
common name

Can define an array name salary to represent a set of
salaries of a group of employees

A particular value is indicated by writing a number
or subscript in brackets after the array name

Example: salary[10] – represent salary of 10th
employee

The complete set of values is referred as an array

The individual values are called elements

Arrays can be of any data type
ONE-DIMENSIONAL ARRAY

A list of items can be given one variable name using
only one subscript and such a variable is called a
single-subscripted variable or a one-dimensional
array

In C, single subscripted variable xi can be expressed
as x[1], x[2], x[3],…..x[n]

The subscript can begin with the number 0, that is
x[0] is allowed
ONE-DIMENSIONAL ARRAY DECLARATION

Syntax:
data_type variable_name[size];

The size indicates the maximum number of
elements that can be stored inside the array
Example: int x[3];
ASSIGNING ELEMENTS TO AN ARRAY

The values to the array elements can be
assigned as
X[0] = 5
X[1] = 3
X[2] = 7
CHARACTER ARRAY

The c languages treats strings as arrays of characters

The compiler terminates the character strings with an
additional null (‘\0’) character

The element name[10] holds the null character ‘\0’ at the end

When declaring character arrays, we must always allow one
extra elements space for the null terminator
#include<stdio.h>
EXAMPLE
#include<conio.h>
void main()
{
int i=0;
char a[]="abcd";
clrscr();
while(a[i]!='\0')
{
printf("\t%c",a[i]);
i++;
}
}

OUTPUT:
a b c d
INITIALIZATION OF ARRAYS

Initialization of arrays can be done in two method

At compile time

At run time
COMPILE TIME INITIALIZATION

Syntax:
data_type array_name[size]={variables};

Example: int x[3]={5,3,7};

The size may be omitted. In such case the compiler allocates
enough space for all initialized elements

Example: Char name[] = {‘J’,’O’,’H’,’N’};
INITIALIZATION OF ARRAYS
RUN TIME INITIALIZATION

Array can also be initialized at the run time.

Example:
while(i<10)
{
if(i<5)
sum[i]=0;
else
sum[i]=sum[i]+i;
}

Input using scanf()
Example: scanf(“%d%d”,&a[0],&a[1]);
EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int x[2],i;
printf("\nEnter the inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("\nThe value in x[%d] is %d",i,x[i]);
getch();
}
OUTPUT:
Enter the inputs:3
6

The value in x[0] is 3


The value in x[1] is 6
EXAMPLE
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
printf("\nThe value in x[%d] is %c",i,x[i]);
getch();
}
OUTPUT:
The value in x[0] is a
The value in x[1] is b
The value in x[2] is c
The value in x[3] is d
The value in x[4] is e
TWO-DIMENSIONAL

ARRAY
There will be situations where a table of values will
have to be stored

C allows us to define such tables of items by using
two-dimensional arrays

Syntax:
data_type array_name[row_size] [col_size];

Example: int x[3][2];
INITIALIZING TWO-DIMENSIONAL
ARRAYS

Syntax:
data_type array_name[row_size][col_size];={variables};

Example:
int x[2][2]={1,50,2,75};
(or)
int x[2][2]={ {1,50}, {2,75} };
(or)
int x[ ][2]={ {1,50},
{2,75}
};
DYNAMIC ARRAYS
• An array created at compile time by specifying
size in the source code has a fixed size and cannot
be modified at run time
• The process of allocating memory at compile
time is known as static memory allocation
• The array that receive static memory allocation
are called static arrays
• This approach works as long as we know exactly
what our data requirements are
DYNAMIC ARRAYS
• Consider a situation where we want to use an
array that can vary greatly in size
• In C it is possible to allocate memory to array at
run time.
• This feature is known as dynamic memory
allocation.
• The arrays created at run time are called dynamic
arrays
DYNAMIC ARRAYS
• Dynamic arrays are created using pointer
variables and memory management functions
malloc, calloc and realloc.
• These functions are included in the header file
<stdlib.h>
• The concept of dynamic arrays is used in creating
and manipulating data structure such as linked
lists, stacks and queues
#include<stdio.h>
EXAMPLE
#include<conio.h>
void main()
{
int i,j;
int x[2][2]={ {1,50},
{2,75}
};
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}

OUTPUT:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
#include<stdio.h>
EXAMPLE
#include<conio.h>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<2;j++)
printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}

OUTPUT:
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
The value in x[2][0] is 3
The value in x[2][1] is 65
MULTI-DIMENSIONAL ARRAY

C allows arrays of three or more dimensions

The exact limit is determined by the compiler

SYNTAX:
 data_type variable_name[s1][s2][s3]….[sn];

EXAMPLE:
 int survey[3][5][12]; - 3 dimensional array that can
hold 180 elements
 float table[5][4][5][3]; - 4 dimensional array that
can hold 300 elements
Matrix Addition
#include<stdio.h> //step2:
#include<conio.h> printf("\n Enter the elements of matrix A \
void main() n");
{ for(i=0;i<r1;i++)
int i,j,k,r1,r2,c1,c2; {
int a[5][5],b[5][5],c[5][5]; for(j=0;j<c1;j++)
clrscr(); {
//step1: scanf("%d",&a[i][j]);
printf("\n Enter the size of matrix A:"); }
scanf("%d%d",&r1,&c1); }
printf("\n Enter the size of matrix B: "); printf("\n Enter the elements of matrix B \
scanf("%d%d",&r2,&c2); n");
if((c1==c2)&&(r1==r2)) for(i=0;i<r2;i++)
goto step2; {
else for(j=0;j<c2;j++)
goto step1; {
scanf("\t%d",&b[i][j]);
}
}
Matrix Addition
for(i=0;i<r1;i++) OUTPUT:
{ Enter the size of matrix A: 2
for(j=0;j<c1;j++) 2
{ Enter the size of matrix B: 2
c[i][j]=0; 2
c[i][j]=c[i][j]+a[i][j]+b[i][j]; Enter the elements of matrix A
} 2
} 2
printf("\n The resultant matrix after 2
addition of A & B is\n"); 2
for(i=0;i<r1;i++) Enter the elements of matrix B
{ 3
for(j=0;j<c1;j++) 3
printf("%d\t",c[i][j]); 3
printf("\n"); 3
} The resultant matrix after addition of
getch(); A&B is
} 5 5
5 5
Matrix Multiplication
#include<stdio.h> step2:
#include<conio.h> printf("\n Enter the elements of
matrix A \n");
void main()
for(i=0;i<r1;i++)
{
{
int i,j,k,r1,r2,c1,c2;
for(j=0;j<c1;j++)
int a[5][5],b[5][5],c[5][5];
{
clrscr();
scanf("%d",&a[i][j]);
step1:
}
printf("\n Enter the size of matrix A \n");
}
scanf("%d%d",&r1,&c1);
printf("\n Enter the elements of
printf("\n Enter the size of matrix B \n"); matrix B \n");
scanf("%d%d",&r2,&c2); for(i=0;i<r2;i++)
if(c1==r2) {
goto step2; for(j=0;j<c2;j++)
else {
goto step1; scanf("\t%d",&b[i][j]);
}
}
Matrix Multiplication
for(i=0;i<r1;i++) OUTPUT:
{ Enter the size of matrix A:2
for(j=0;j<c2;j++) 2
{ Enter the size of matrix B:2
c[i][j]=0; 2
for(k=0;k<c1;k++) Enter the elements of matrix A
{ 4
c[i][j]=c[i][j]+a[i][k]*b[k][j]; 4
} 4
} 4
} Enter the elements of matrix B
for(i=0;i<r1;i++) 4
{ 4
for(j=0;j<c2;j++) 4
printf("%d\t",c[i][j]); 4
printf("\n"); The resultant matrix is
} 32 32
getch(); 32 32
}
OUTPUT:
Matrix Multiplication
Enter the size of matrix A:2
3
Enter the size of matrix B:3
2
Enter the elements of matrix A
1
2
3
4
5
6
Enter the elements of matrix B
2
4
6
8
2
4
20 32
50 80

You might also like