Declaration, Initialisation , Reading & Writing
of Arrays
• 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
• C supports a derived data type known as array.
• An array is a fixed-sized sequenced collection of
elements of the same data type thats shares a
common name.
• The common name a is the array name and each
individual data item is known as an element of the
array.
• The elements of the array are stored in the
subsequent memory locations starting from the
memory location given by the array name.
 Array can be classified as two types are,
1. One-Dimensional Array
2. Two-Dimensional or multidimensional
Array
 One-Dimensional Array
◦ A one-dimensional array can be used to represent a
list of data items. It is also known as a vector.
 Two-Dimensional Array
◦ A two dimensional array can be used to represent a
table of data items consisting of rows and columns.
It is also known as a matrix
 Every array must be declared before use like other
variables.
 The declarations of one-dimensional and
multidimensional arrays differ slightly.
 Declaration of One-dimensional array:
where
data_type refers to the data type of elements in the array.
It can be a primitive or derived data type.
name is an identifier which represents the array name.
size is an integer expression representing the total
number of elements in the array.
data_type name1[size1], name2[size2],…, namen[sizen];
 The base address of an array is the address of the
zeroth element (starting element) of that array.
 When an array is declared, the compiler allocates a base
address and reserves enough space in memory for all
the elements of the array.
 In C, the array name represents this base address.
 For example, float x[5]; is the declaration of a one-
dimensional array.
 It defines a float array x of size 5 that represents a
block of 5 consecutive storage regions.
 Each element in the array is referred to by the array
variables x[0], x[1], x[2] ,x[3] and x[4] where 0,1,2,3
and 4 represent subscripts or indices of the array.
 In general, x[i] refers to the ith element of the array.
 The array subscripts always start at zero in C and
they are integer expressions.
 Hence, x[0] refers to the starting element, x[1] refers
to the next element and x[4] refers to the last
element.
 The declaration double list[10], array[15]; declares
two arrays, named list and array having 10 and 15
elements respectively of double precision data type.
 The elements of an array may be assigned with the values using
initialisation instead of reading them by the I/O functions.
 An array can be initialised in its declaration only.
 The list of initialisers (values) are enclosed in braces.
 The initialisers are separated by commas and they must be
constants or constant expressions.
 One-dimensional array can be initialised as given below.
int a[4]={3,5, -8, 10};
 The values within the braces are scanned from left end and
assigned to a[0], a[1] and so on.
 A semicolon should be placed after the closing brace.
“For “ loops used extensively for array processing
Example 1: write a program to read an array and
print.
#include<stdio.h>
void main()
{
int i,a[10];
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for(i=0;i<10;i++)
Printf(“n %d”,a[i]);
}
#include<stdio.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]);
}
OUTPUT:
Enter the inputs:3
6
The value in x[0] is 3
The value in x[1] is 6
/*Determines the length of a message */
#include<stdio.h>
main()
{
char ch;
int len=0;
printf(“Enter a message : ”);
ch=getchar();
while (ch!=‘n’) {
len++;
ch=getchar();
}
printf(“Your message was %d character(s) long. n”,
len);
return 0;
}
 The c languages treats strings as arrays of characters
 The compiler terminates the character strings with an
additional null (‘0’) character.
 String is a one dimensional array of characters in c.
 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>
void main()
{
int i=0;
char a[]="abcd";
while(a[i]!='0')
{
printf("t%c",a[i]);
i++;
}
}
OUTPUT:
a b c d
 There are different way to initialize a character array variable.
char name[10]=“StudingTonight”; //valid initialization
char name[10]={‘L’,’E’,’S’,’S’,’O’,’N’,’S’,’0’}; // valid Init
Some example for illegal Initialization of character array are,
char ch[3]=“hello”; // Illegal
char str[4];
str=“hello”; //illegal
Format:
data_type array_name[row_size1][column_size1];
 The first subscript represents the row size and the second subscript
represents the column size.
 The value in the ith row and jth column is referred to by name[i][j].
 The total number of elements in a two dimensional array is calculated
by multiplying the number of rows by the number of columns.
 In two-dimensional arrays, the values are stored row by row.
 int x[3][2];
 For example, the declaration int a[3][2] ,b[7][4]; defines
the two-dimensional integer arrays a and b.
 These arrays are arrays of integer arrays.
 Six (rows * columns) elements of a are stored row by row
in the consecutive memory locations.
 This method of storing the elements in the memory is
known as row major order storage representation
 The zeroth element of array a is denoted by a[0][0] and the
last element in that array is denoted by a[2][1].
 Similarly, for the array b, the zeroth element and the last
element are represented by b[0][0] and b[6][3] respectively.
Starting element a[0][0]
a[0][1]
a[1][0]
a[1][1]
a[2][0]
Last element a[2][1]
Row 0
Row 1Row 1
Row 2
Storage representation of matrix a
Syntax:
data_type array_name[row_size][col_size]={variables};
 A two-dimensional array can be initialised as given below.
int a[3][2] = {20, 25, -3, 8, -5, 7};
 Here, the initialised values are assigned to the array elements
according to the order of the storage in the memory.
 But if the initialisers are enclosed within the inner braces as given
below
int a[3][2] = {{20,25}, {-3, 8}, {-5,7}};
 The initialised values within the inner braces are assigned to each row.
 To enforce the assignment of each row, it is essential to use the inner
braces.
 If the number of values initialised for an array is less than the size
mentioned, the missing elements are assigned zero. In the initialisation
int a[3][4] = { {1,2},{4,8,15} };
 the elements a[0][0] and a[0][1] of the zeroth row and a[1][0], a[1][1] and
a[1][2] of the first row are initialised with the values 1,2,4,8 and 15
respectively.
 All the other elements are initialised to zero.
 If it is given as
int a[3][4] = {1,2,3,8,15};
 the values are assigned from the left end to a[0][0], a[0][1], a[0][2],
a[0][3], and a[[1][0] according to the order of the storage representation.
 If the number of initialisers exceeds the size of the array, it is an error.
 The size of a one-dimensional array need not be mentioned in its
initialisation.
 In this case, the compiler will count the values assigned and take it
as the size of that array.
 In multidimensional arrays the leftmost subscript may be omitted
and all the others must be specified. The initialisation
int x[ ] = {2,4,6,8,10};
 makes the array x having 5 elements
float a[ ][2] = {{3.2,4.8}, {5.3,3.7}};
 makes the array a having 2 rows and 2 columns.
#include<stdio.h>
void main()
{
int i,j;
int x[2][2]={ {1,50},
{2,75}
};
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]);
}
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>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
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]);
}
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
/* PROGRAM TO FIND THE MINIMUM VALUE AND ITS POSITION */
#include<stdio.h>
#define MAX 5
main( )
{
int a[MAX],i,min;
int pos = 0; /* Fixing position if the first number is
the minimum value */
printf("Enter the array elementsn");
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
min=a[0];
for(i=1;i<MAX;i++)
if(a[i] < min)
{
min=a[i];
pos=i; /* Fixing the minimum value position*/
}
printf(“ MINIMUM VALUE = %dn”,min);
printf(“ POSITION = %dn”,pos);
}
SAMPLE INPUT AND OUTPUT
Enter the array elements
14 29 37 25 45
MINIMUM VALUE = 14
POSITION = 0
#define MAXROW 2
#define MAXCOL 4
main( )
{
int s[MAXROW][MAXCOL],t[MAXCOL][MAXROW];
int i,j;
printf("Enter the values of the matrixn");
for(i=0;i<MAXROW;i++)
for(j=0;j<MAXCOL;j++)
scanf("%d",&s[i][j]);
printf("nGiven Matrixnn");
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOL;j++)
printf("%dt",s[i][j]);
printf("n");
}
for(i=0;i<MAXCOL;i++)
for(j=0;j<MAXROW;j++)
t[i][j] = s[j][i];
printf("nTranspose of the matrixnn");
for(i=0;i<MAXCOL;i++)
{
for(j=0;j<MAXROW;j++)
printf("%dt",t[i][j]); printf("n");
}
}
SAMPLE INPUT AND OUTPUT
Enter the values of the matrix
10 20 30 40 50 60 70 80
Given Matrix
10 20 30 40
50 60 70 80
Transpose of the matrix
10 50
20 60
30 70
40 80
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
//step1:
printf("n Enter the size of matrix
A:");
scanf("%d%d",&r1,&c1);
printf("n Enter the size of matrix
B: ");
scanf("%d%d",&r2,&c2);
if((c1==c2)&&(r1==r2))
goto step2;
else
goto step1;
//step2:
printf("n Enter the elements of matrix
A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix
B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+a[i][j]+b[i][j];
}
}
printf("n The resultant matrix
after addition of A & B isn");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%dt",c[i][j]);
printf("n");
}
getch();
}
OUTPUT:
Enter the size of matrix A: 2
2
Enter the size of matrix B: 2
2
Enter the elements of matrix A
2
2
2
2
Enter the elements of matrix B
3
3
3
3
The resultant matrix after addition
of A&B is
5 5
5 5
/* PROGRAM TO REMOVE THE DUPLICATE ELEMENTS IN AN ARRAY */
#define MAX 5
main( )
{
int a[MAX];
int i,j,k,n=MAX,p;
for(i=0;i<n;i++)
{
printf("Enter the value of a[%d]:n",i);
scanf("%d",&a[i]);
}
printf("Array before deleting duplicate elementsn");
for(i=0;i<n;i++)
printf("%dt",a[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)

{
if(a[i] == a[j])
{
p = i ;
for(k=j;k<n;k++)
a[k] =a[++j];
j = p; n--;
}
}
}
putchar('n');
printf("Array after deleting duplicate elementsn");
for(i=0;i<n;i++)
printf("%dt",a[i]);
putchar('n');
}
SAMPLE INPUT AND OUTPUT
Enter the value of a[0]:
5
Enter the value of a[1]:
8
Enter the value of a[2]:
5
Enter the value of a[3]:
12
Enter the value of a[4]:
8
Array before deleting duplicate elements
5 8 5 12
8
Array after deleting duplicate elements
5 8 12
/* PROGRAM TO SORT AN ARRAY */
#define MAX 5
main( )
{
int a[MAX],i,j,temp;
printf("Enter the array elementsn");
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
for(i=0;i<MAX;i++)
for(j=i+1;j<MAX;j++)
{
if(a[i] >a[j])
Example Programs
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf("nAscending ordern");
for(i=0;i<MAX;i++)
printf("%dt",a[i]);
printf("nDescending ordern");
for(i=MAX-1;i>=0;i--)
printf("%dt",a[i]);
}
SAMPLE INPUT AND OUTPUT
Enter the array elements
11 15 20 45 15
Ascending order
11 15 15 20 45
Descending order
45 20 15 15 11
Example Programs
/*PROGRAM TO CONVERT A 2-D ARRAY TO 1-D ARRAY */
#define MAXROW 3
#define MAXCOL 2
main( )
{
int a[MAXROW][MAXCOL],b[MAXROW*MAXCOL];
int i,j,k=0;
printf("Enter the matrix elements in row ordern");
for(i=0;i<MAXROW;i++)
for(j=0;j<MAXCOL;j++)
{
scanf("%d",&a[i][j]);
Example Programs
b[k++] = a[i][j];
}
printf("Given two-dimensional arrayn");
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXCOL;j++)
printf("%dt",a[i][j]);
printf("n");
}
printf("Equivalent one-dimensional arrayn");
for(i=0;i<MAXROW*MAXCOL;i++)
printf("%dt",b[i]);
}
SAMPLE INPUT AND OUTPUT
Enter the matrix elements in row order
10 15 20 25 30 35
Given two-dimensional array
10 15
20 25
30 35
Equivalent one-dimensional array
10 15 20 25 30 35
Example Programs
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional

Arrays 1D and 2D , and multi dimensional

  • 1.
    Declaration, Initialisation ,Reading & Writing of Arrays
  • 2.
    • We haveused 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
  • 3.
    • C supportsa derived data type known as array. • An array is a fixed-sized sequenced collection of elements of the same data type thats shares a common name. • The common name a is the array name and each individual data item is known as an element of the array. • The elements of the array are stored in the subsequent memory locations starting from the memory location given by the array name.
  • 4.
     Array canbe classified as two types are, 1. One-Dimensional Array 2. Two-Dimensional or multidimensional Array
  • 5.
     One-Dimensional Array ◦A one-dimensional array can be used to represent a list of data items. It is also known as a vector.  Two-Dimensional Array ◦ A two dimensional array can be used to represent a table of data items consisting of rows and columns. It is also known as a matrix
  • 6.
     Every arraymust be declared before use like other variables.  The declarations of one-dimensional and multidimensional arrays differ slightly.  Declaration of One-dimensional array: where data_type refers to the data type of elements in the array. It can be a primitive or derived data type. name is an identifier which represents the array name. size is an integer expression representing the total number of elements in the array. data_type name1[size1], name2[size2],…, namen[sizen];
  • 7.
     The baseaddress of an array is the address of the zeroth element (starting element) of that array.  When an array is declared, the compiler allocates a base address and reserves enough space in memory for all the elements of the array.  In C, the array name represents this base address.  For example, float x[5]; is the declaration of a one- dimensional array.  It defines a float array x of size 5 that represents a block of 5 consecutive storage regions.
  • 8.
     Each elementin the array is referred to by the array variables x[0], x[1], x[2] ,x[3] and x[4] where 0,1,2,3 and 4 represent subscripts or indices of the array.  In general, x[i] refers to the ith element of the array.  The array subscripts always start at zero in C and they are integer expressions.  Hence, x[0] refers to the starting element, x[1] refers to the next element and x[4] refers to the last element.  The declaration double list[10], array[15]; declares two arrays, named list and array having 10 and 15 elements respectively of double precision data type.
  • 9.
     The elementsof an array may be assigned with the values using initialisation instead of reading them by the I/O functions.  An array can be initialised in its declaration only.  The list of initialisers (values) are enclosed in braces.  The initialisers are separated by commas and they must be constants or constant expressions.  One-dimensional array can be initialised as given below. int a[4]={3,5, -8, 10};  The values within the braces are scanned from left end and assigned to a[0], a[1] and so on.  A semicolon should be placed after the closing brace.
  • 10.
    “For “ loopsused extensively for array processing Example 1: write a program to read an array and print. #include<stdio.h> void main() { int i,a[10]; for(i=0;i<10;i++) scanf(“%d”,&a[i]); for(i=0;i<10;i++) Printf(“n %d”,a[i]); }
  • 12.
    #include<stdio.h> void main() { int x[2],i; printf("nEnterthe 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]); } OUTPUT: Enter the inputs:3 6 The value in x[0] is 3 The value in x[1] is 6
  • 13.
    /*Determines the lengthof a message */ #include<stdio.h> main() { char ch; int len=0; printf(“Enter a message : ”); ch=getchar(); while (ch!=‘n’) { len++; ch=getchar(); } printf(“Your message was %d character(s) long. n”, len); return 0; }
  • 14.
     The clanguages treats strings as arrays of characters  The compiler terminates the character strings with an additional null (‘0’) character.  String is a one dimensional array of characters in c.  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
  • 15.
    #include<stdio.h> void main() { int i=0; chara[]="abcd"; while(a[i]!='0') { printf("t%c",a[i]); i++; } } OUTPUT: a b c d
  • 16.
     There aredifferent way to initialize a character array variable. char name[10]=“StudingTonight”; //valid initialization char name[10]={‘L’,’E’,’S’,’S’,’O’,’N’,’S’,’0’}; // valid Init Some example for illegal Initialization of character array are, char ch[3]=“hello”; // Illegal char str[4]; str=“hello”; //illegal
  • 17.
    Format: data_type array_name[row_size1][column_size1];  Thefirst subscript represents the row size and the second subscript represents the column size.  The value in the ith row and jth column is referred to by name[i][j].  The total number of elements in a two dimensional array is calculated by multiplying the number of rows by the number of columns.  In two-dimensional arrays, the values are stored row by row.
  • 18.
  • 19.
     For example,the declaration int a[3][2] ,b[7][4]; defines the two-dimensional integer arrays a and b.  These arrays are arrays of integer arrays.  Six (rows * columns) elements of a are stored row by row in the consecutive memory locations.  This method of storing the elements in the memory is known as row major order storage representation  The zeroth element of array a is denoted by a[0][0] and the last element in that array is denoted by a[2][1].  Similarly, for the array b, the zeroth element and the last element are represented by b[0][0] and b[6][3] respectively.
  • 20.
    Starting element a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] Lastelement a[2][1] Row 0 Row 1Row 1 Row 2 Storage representation of matrix a
  • 21.
    Syntax: data_type array_name[row_size][col_size]={variables};  Atwo-dimensional array can be initialised as given below. int a[3][2] = {20, 25, -3, 8, -5, 7};  Here, the initialised values are assigned to the array elements according to the order of the storage in the memory.  But if the initialisers are enclosed within the inner braces as given below int a[3][2] = {{20,25}, {-3, 8}, {-5,7}};  The initialised values within the inner braces are assigned to each row.  To enforce the assignment of each row, it is essential to use the inner braces.
  • 22.
     If thenumber of values initialised for an array is less than the size mentioned, the missing elements are assigned zero. In the initialisation int a[3][4] = { {1,2},{4,8,15} };  the elements a[0][0] and a[0][1] of the zeroth row and a[1][0], a[1][1] and a[1][2] of the first row are initialised with the values 1,2,4,8 and 15 respectively.  All the other elements are initialised to zero.  If it is given as int a[3][4] = {1,2,3,8,15};  the values are assigned from the left end to a[0][0], a[0][1], a[0][2], a[0][3], and a[[1][0] according to the order of the storage representation.  If the number of initialisers exceeds the size of the array, it is an error.
  • 23.
     The sizeof a one-dimensional array need not be mentioned in its initialisation.  In this case, the compiler will count the values assigned and take it as the size of that array.  In multidimensional arrays the leftmost subscript may be omitted and all the others must be specified. The initialisation int x[ ] = {2,4,6,8,10};  makes the array x having 5 elements float a[ ][2] = {{3.2,4.8}, {5.3,3.7}};  makes the array a having 2 rows and 2 columns.
  • 24.
    #include<stdio.h> void main() { int i,j; intx[2][2]={ {1,50}, {2,75} }; 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]); } 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
  • 25.
    #include<stdio.h> void main() { int i,j; intx[][2]={ {1,50},{2,75},{3,65}}; 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]); } 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
  • 26.
    /* PROGRAM TOFIND THE MINIMUM VALUE AND ITS POSITION */ #include<stdio.h> #define MAX 5 main( ) { int a[MAX],i,min; int pos = 0; /* Fixing position if the first number is the minimum value */ printf("Enter the array elementsn"); for(i=0;i<MAX;i++) scanf("%d",&a[i]); min=a[0]; for(i=1;i<MAX;i++) if(a[i] < min) { min=a[i]; pos=i; /* Fixing the minimum value position*/ } printf(“ MINIMUM VALUE = %dn”,min); printf(“ POSITION = %dn”,pos); } SAMPLE INPUT AND OUTPUT Enter the array elements 14 29 37 25 45 MINIMUM VALUE = 14 POSITION = 0
  • 27.
    #define MAXROW 2 #defineMAXCOL 4 main( ) { int s[MAXROW][MAXCOL],t[MAXCOL][MAXROW]; int i,j; printf("Enter the values of the matrixn"); for(i=0;i<MAXROW;i++) for(j=0;j<MAXCOL;j++) scanf("%d",&s[i][j]); printf("nGiven Matrixnn"); for(i=0;i<MAXROW;i++) { for(j=0;j<MAXCOL;j++) printf("%dt",s[i][j]); printf("n"); }
  • 28.
    for(i=0;i<MAXCOL;i++) for(j=0;j<MAXROW;j++) t[i][j] = s[j][i]; printf("nTransposeof the matrixnn"); for(i=0;i<MAXCOL;i++) { for(j=0;j<MAXROW;j++) printf("%dt",t[i][j]); printf("n"); } } SAMPLE INPUT AND OUTPUT Enter the values of the matrix 10 20 30 40 50 60 70 80 Given Matrix 10 20 30 40 50 60 70 80 Transpose of the matrix 10 50 20 60 30 70 40 80
  • 29.
    #include<stdio.h> #include<conio.h> void main() { int i,j,k,r1,r2,c1,c2; inta[5][5],b[5][5],c[5][5]; clrscr(); //step1: printf("n Enter the size of matrix A:"); scanf("%d%d",&r1,&c1); printf("n Enter the size of matrix B: "); scanf("%d%d",&r2,&c2); if((c1==c2)&&(r1==r2)) goto step2; else goto step1; //step2: printf("n Enter the elements of matrix A n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf("%d",&a[i][j]); } } printf("n Enter the elements of matrix B n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("t%d",&b[i][j]); } }
  • 30.
    for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=0; c[i][j]=c[i][j]+a[i][j]+b[i][j]; } } printf("n The resultantmatrix after addition of A & B isn"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%dt",c[i][j]); printf("n"); } getch(); } OUTPUT: Enter the size of matrix A: 2 2 Enter the size of matrix B: 2 2 Enter the elements of matrix A 2 2 2 2 Enter the elements of matrix B 3 3 3 3 The resultant matrix after addition of A&B is 5 5 5 5
  • 31.
    /* PROGRAM TOREMOVE THE DUPLICATE ELEMENTS IN AN ARRAY */ #define MAX 5 main( ) { int a[MAX]; int i,j,k,n=MAX,p; for(i=0;i<n;i++) { printf("Enter the value of a[%d]:n",i); scanf("%d",&a[i]); } printf("Array before deleting duplicate elementsn"); for(i=0;i<n;i++) printf("%dt",a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++)
  • 32.
     { if(a[i] == a[j]) { p= i ; for(k=j;k<n;k++) a[k] =a[++j]; j = p; n--; } } } putchar('n'); printf("Array after deleting duplicate elementsn"); for(i=0;i<n;i++) printf("%dt",a[i]); putchar('n'); } SAMPLE INPUT AND OUTPUT Enter the value of a[0]: 5 Enter the value of a[1]: 8 Enter the value of a[2]: 5 Enter the value of a[3]: 12 Enter the value of a[4]: 8 Array before deleting duplicate elements 5 8 5 12 8 Array after deleting duplicate elements 5 8 12
  • 33.
    /* PROGRAM TOSORT AN ARRAY */ #define MAX 5 main( ) { int a[MAX],i,j,temp; printf("Enter the array elementsn"); for(i=0;i<MAX;i++) scanf("%d",&a[i]); for(i=0;i<MAX;i++) for(j=i+1;j<MAX;j++) { if(a[i] >a[j]) Example Programs
  • 34.
    { temp = a[i]; a[i]= a[j]; a[j] = temp; } } printf("nAscending ordern"); for(i=0;i<MAX;i++) printf("%dt",a[i]); printf("nDescending ordern"); for(i=MAX-1;i>=0;i--) printf("%dt",a[i]); } SAMPLE INPUT AND OUTPUT Enter the array elements 11 15 20 45 15 Ascending order 11 15 15 20 45 Descending order 45 20 15 15 11 Example Programs
  • 35.
    /*PROGRAM TO CONVERTA 2-D ARRAY TO 1-D ARRAY */ #define MAXROW 3 #define MAXCOL 2 main( ) { int a[MAXROW][MAXCOL],b[MAXROW*MAXCOL]; int i,j,k=0; printf("Enter the matrix elements in row ordern"); for(i=0;i<MAXROW;i++) for(j=0;j<MAXCOL;j++) { scanf("%d",&a[i][j]); Example Programs
  • 36.
    b[k++] = a[i][j]; } printf("Giventwo-dimensional arrayn"); for(i=0;i<MAXROW;i++) { for(j=0;j<MAXCOL;j++) printf("%dt",a[i][j]); printf("n"); } printf("Equivalent one-dimensional arrayn"); for(i=0;i<MAXROW*MAXCOL;i++) printf("%dt",b[i]); } SAMPLE INPUT AND OUTPUT Enter the matrix elements in row order 10 15 20 25 30 35 Given two-dimensional array 10 15 20 25 30 35 Equivalent one-dimensional array 10 15 20 25 30 35 Example Programs