Array
Array
Introduction
• Array is a collection of variables of same data type known by a single
name.
• The array is an important concept and helps the programmer in managing
many variables of the same type, because all the elements of an array
share a single name.
• Date Types:
• Fundamental data types : int, float, char
• Derived data types: arrays, pointers
• User defined data types: structures, unions
• Arrays can be divided into three categories
1. Single dimensional array
2. Two-dimensional array
3. Multi-dimensional array
Single Dimensional Array
The syntax for declaring a single dimensional array is :
datatype arrayname[size];
Data type can be int, char, float , long int etc. The arrayname is the
name of a variable which represents the array, while size which is
represented in [ ] symbols represents the size of array.
int main()
{
int arr[100];
int i, x, pos, n;
scanf("%d ", &n);
for (i = 0; i < n; i++)
scanf("%d ", &arr[i]);
scanf("%d ", &x);
scanf("%d ", &pos);
n++;
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Two dimensional array
Sometimes we need to store data where more than one
dimensions are involved, like sales information of a company, or
for mathematical calculations we need to use matrix.
The syntax for two-dimensional array is :
datatype variablename[rowsize][colsize];
where, variablename represents the name of an array, rowsize
indicate the number of rows in table, colsize indicates number of
columns in an array.
Program – to add two matrices
#include <stdio.h>
#include <conio.h>
main()
{
int a[4][4], b[4][4],c[4][4]; /* matrix c stores result i.e c = a +b */
int m,n,p,q; /* m no.of rows in a, n no. of cols in a*/
int i,j; /* p no.of rows in b, q no. of cols in b*/
clrscr();
printf(“Give number of rows in first matrix\n”);
scanf(“%d”,&m);
printf(“Give number of columns in first matrix\n”);
scanf(“%d”,&n);
printf(“Give number of rows in second matrix\n”);
scanf(“%d”,&p);
printf(“Give number of columns in second matrix\n”);
scanf(“%d”,&q);
Program – to add two matrices (cont)
if( m!= p || n!= q) /* check size match or not */
{
printf(“Size do not match. Addition not possible\n”);
return 0;
}
printf(“Enter matrix A row-wise\n”);
for(i=0;i<m;i++) /* Get first matrix data */
{
for(j=0;j<n;j++)
{
printf(“a[%d][%d]= “,i,j);
scanf(“%d”,&a[i][j]);
}
printf(“\n”);
}
Program – to add two matrices (cont)
printf(“Enter matrix B row-wise\n”);
for(i=0;i<p;i++) /* Get second matrix data */
{
for(j=0;j<q;j++)
{
printf("b[%d][%d]= ",i,j);
scanf("%d“,&b[i][j]);
}
printf(“\n”);
}
printf(“Matrix A \n”);
for(i=0;i<m;i++) /* display first matrix row-wise */
{
for(j=0;j<n;j++)
printf(“%4d”,a[i][j]);
printf(“\n”);
}
Program – to add two matrices (cont)
printf(“Matrix B \n”);
for(i=0;i<p;i++) /* display second matrix row-wise */
{
for(j=0;j<q;j++)
printf("%4d“,b[i][j]);
printf("\n“);
}
printf(“\nSum Matrix C \n”);
for(i=0;i<m;i++) /* calculate sum and display result matrix */
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j] + b[i][j];
printf("%4d“,c[i][j]);
}
printf(“\n”);
}
}