What is array in C with example | What is array and its types in C | How do you define an array | How many types of array are there

INTRODUCTION
There are many times when we need to store a complete list of number or other data items. You could do this by creating as many individual variable as would be needed for the job, but this is a hard and tedious process. For example, suppose you want to read in five numbers and print them out in reverse order. You could do it the hard way as:
main()
{
Int a1,a2,a3,a4,a5;
scanf(“%d %d %d %d %d”, &a1,&a2,&a3,&a4,&a5);
Printf(“%d  %d %d %d %d”,a5,a4,a3,a2,a1);
}

Definition of an array

“An array is a series of elements of the some type placed in contiguous memory location that can be individually referenced by adding an index to a unique identifier.”

One dimensional array

An array with only on subscript is known as one dimensional array. A subscript is a number in brackets that follows an array’s name. This number can identify the number of individual elements in the array.

Declaration of one dimensional array

Like any other variable, array must be declared before they are used. The general from of declaring a one  dimensional array is:
Example:
data type - array name [size]
float rate [3];
Char name [30];
int marks[5]; 

Initialization of one dimensional arrays

Once we can declare an array, we can data into it. Just as we initialize ordinary variable, we can initialize one dimensional array also.
The general from of declaring one dimensional arrays is:
Data type array name [size]={value0, value1, ….valuesize-1};
int marks[5] = { 36, 80, 49, 76, 45 };

Processing of one dimensional array

You can use an array elements in any instruction where you would use a normal variable in input or output commands or in expressions. You always refer to the individual element by its subscripts number. Array elements are process using a loop (preferably for loop) 

Example : A PROGRAM TO DEMONSTRATE, HOW TO DECLARE AND INSERT THE VALUE IN AN ARRAY. 

#include< stdio.h >
#include< conio.h >
void main ()
{
int marks [5];
int index;
for(index=0; index<5; index++)
{
printf(“\n Enter Marks %d \t”, index);
Scanf(“%d”, &marks{index]);
}
getch();
}     
Output:
Enter Marks 0   45
Enter Marks 1   55
Enter Marks 2   75
Enter Marks 3   80
Enter Marks 4   40 

Multi dimensional arrays

Multi dimensional arrays can be described as “array of array“. In other words, an array with more than one subscript is generally called a multi dimensional array. For example, a bi dimensional array can be imagined as a bi dimensional table mode of elements, all of them of a same uniform data type.
TWO DIMENSIONAL ARRAYS
An array with two subscript is called two dimensional array. A two dimensional array enables us to store multiple rows of elements, that is, a table of value or matrix. The first subscripts refers to the row and the second subscripts refers to the column.

DECLARATION OF TWO DIMENSIONAL ARRAYS

The syntax of declaring a two dimensional array is:
data type array name [row size] [column size];

INITIALIZATION OF TWO ARRAYS

data type array name [row size] [column size]  ={initializer-list};
Example:
int marks [3][3]={25,45,70,67,34,56,67,78,23};
Output:
25  45  70
67  34  56
67  78  23

Addition of two matrix in C

¨#include <stdio.h>
¨#include<conio.h>
¨int main()
¨{
¨   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
¨ 
¨   printf("Enter the number of rows and columns of matrix\n");
¨   scanf("%d%d", &m, &n);
¨   printf("Enter the elements of first matrix\n");
¨ 
¨   for (c = 0; c < m; c++)
¨      for (d = 0; d < n; d++)
¨         scanf("%d", &first[c][d]);
¨ 
¨   printf("Enter the elements of second matrix\n");
¨ 
¨   for (c = 0; c < m; c++)
¨      for (d = 0 ; d < n; d++)
¨         scanf("%d", &second[c][d]);
¨   
¨   printf("Sum of entered matrices:-\n");
¨   
¨   for (c = 0; c < m; c++) {
¨      for (d = 0 ; d < n; d++) {
¨         sum[c][d] = first[c][d] + second[c][d];
¨         printf("%d\t", sum[c][d]);
¨      }
¨      printf("\n");
¨   }
¨
¨   getch();

¨}

Output of matrixaddition C program:


No comments

Powered by Blogger.