Image

C Language - Arrays - Array In C

Array

Array is a collection of similar type of data item refer by common name allocate contagious memory allocation. array always start with zero and ends with n-1 allocation.

Types of array

C support two types of array

  1. 1 one dimensional array
  2. 2 Two dimensional array

1. One dimensional array

An array represent with single subscript variable is known as one dimensional array.

Syntax: arrayname[size];
EX:- int arr[10];
Char name [20];

Memory allocation of one dimensional array

The description of this array is listed below:

Name of the array : a
Data type of the array : integer
Number of elements : 5
Valid index values : 0, 1, 2, 3, 4
Value stored at the location a[0] : 200
Value stored at the location a[1] : 120
Value stored at the location a[2] : -78
Value stored at the location a[3] : 100
Value stored at the location a[4] : 0

Example of array

#include<stdio.h>
#include<conio.h>
main()
{
int arr[5],i;
for(i=0;i<=5;i++)
{
printf("
 Enter a value of array arr[%d]:",i);
scanf("%d",&arr[i]);
}
printf("
 The array element are:
");
for(i=0;i<=5;i++)
{
 printf("%d 	", arr[i]);
 printf("
");
}
getch();
}

2. Two dimensional array
A array represent two subscript values are called as two dimensional array. First subscript represent row and second represent column .
Syntax:- array-name [rows][col];
EX
int  a[3][4];
char name [10][15];

In memory, whether it is a one dimensional or a two dimensional array, the array elements are stored in one continuous chain.

The arrangement of array elements of a two dimensional array of students, which contains roll numbers in one column and the marks in the other (in memory) is shown below:


2. Program to print a multiplication table, using two dimensional array.
#define ROWS 5
#define COLUMNS 5
main( )
{
int row, column, product [ROWS] [COLUMNS];
int i, j;
printf ("MULTIPLICATION TABLE \N");
printf (" ");
for (j = 1; j < = COLUMNS; j++)
printf ("%4d", j);
printf ("\n");
for (i = 0; i < ROWS; i++)
{
row = i + 1;
printf ("%2d\n", row);
for (j = 1; j < = COLUMNS; j++)
{
column = j;
product [i] [j] = row *column;
printf ("%4d", product [i] [j]);
}
printf ("\n");
}
}
Output:
MULTIPLICATION TABLE

   1   2   3   4   5
1  1   2   3   4   5
2  2   4   6   8   10
3  3   6   9   12  15
4  4   8   12  16  20
5  5   10  15  20  25