Share this post on:

Array in C is a collection of similar type of data items and each data items is called an element of the array. Data types of an elements may be any valid data types like char, int or float. Elements of array share the same variable name but each elements has a different index number known ad subscript.

Array can be single dimensional or multidimensional. The number of subscripts determines the dimension of array. A one dimensional array has one subscript, two dimensional array has two subscripts and so on. The one-dimensional array are known as vectors and two dimensional array are known as metrices.

One dimensional Array in C

One dimensional declaration Syntax : data_type array_name[size];

In above syntax array_name denotes the name of the array and it can be any valid c identifier, data_type is the data type of elements of array. The size of array specifies the number of elements that can be stores in the array. Array may be positive integer constant or constant integer expression.

Example of Array declaration :-

int age[100]

float salary[15];

char grade[20];

The initial value of loop variable is taken 0 since array subscripts start from zero. The loop variable is increased by 1 each time so that we can access and process the next elements in the array. The total number of passes in the loop will be equal to the number of elements in the array and in each pass we will process one element.

Example:

#include<stdio.h>

int main(void)
{
int arr[5],i;
for(i=0;i<5;i++)
{
printf(“enter a value for arr[%d]: “,i);
scanf(“%d”,&arr[i]);
}
printf(“The array elements are : \n”);
for(i=0;i<5;i++)
printf(“%d\t”,arr[i]);
printf(“\n”);
return ;
}

Output:

Enter a value for arr[0] : 12

Enter a value for arr[1] : 45

Enter a value for arr[2] : 59

Enter a value for arr[3] : 98

Enter a value for arr[4] : 21

The array elements are:

12 45 59 98 21

Example : Write a Program to find the largest and smallest number in an integer array?

#include<stdio.h>

int main(void)
{
int i,arr[10]={2,5,4,1,8,9,11,6,3,7};
int small,large;
small=large=arr[0];
for(i=1;i<10;i++) { if(arr[i] large)
large=arr[i];
}
printf(“Smallest = %d,Largest = %d\n”,small,large);
return 0;
}

Two Dimensional Array in C

Syntax of Two dimenstional Array: data_type array_name[row_size][column_size];

In above syntax row_size specifies the number of rows and column_size represents the number of column in the array. Total number of elements in the array are row_size*column_size. for example suppose we have an array arr declared as – int arr[4] [5];

Example of Array declaration:

int mat[4] [3] = {11,12,13};

These value are assigned to the elements row-size, so the value elements after this initialization are :-

mat[0][0] : 11

mat[0][1] : 12

mat[0][3] : 13

Example : Demonstrate Multiplication of Two Matrices?

#include<stdio.h>

int main(void)
{
int mat1[ROW1][COL1], mat2[ROW2[COL2],mat3[ROW3][COL3];
int i,j,k;
printf(“Enter matrix mat1(%dx%d)row-wise :\n”,ROW1,COL1);
for(i=0;i<ROW1;i++)
for(j=0;j<COL1;j++)
scanf(“%d”,&mat1[i][j]);
printf(“Enter matrix mat2(%dx%d)row-wise :\n”,ROW2,COL2);
for(i=0;i<ROW2;i++)
for(j=0;j<COL2;j++)
scanf(“%d”,&mat2[i][j]);
/Multiplication/
for(i=0;i<ROW1;i++)
for(j=0;j<COL2;j++)
{
mat3[i][j]=0;
for(k=0;k<COL1;k++)
mat3[i][j] += mat1[i][j]*mat2[k][j];
}
printf(“Result is :\n”);
for(i=0;i<ROW1;i++)
{
for(j=0;j<COL2;j++)
printf(“%5d”,mat3[i][j]);
printf(“\n”);
}
return 0;
}

Output:

Enter matrix mat1(3*4) row wise:

2 1 4 1

5 2 7 1

3 1 4 2

Enter matrix mat2(4*2) row-size:

1 2

3 4

2 5

6 2

Result is :

31 34

31 55

26 34

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

2 Comments

  1. Pingback: How to use String in C Programming? - ArzaTechs

  2. Pingback: What is Linked List in C Programming - ArzaTechs

Leave a Comment

Your email address will not be published. Required fields are marked *