Arrays in C programming language

A finite ordered set of similar data stored in contiguous memory locations is known as an array. It’s easier to store similar data types in a single variable and then access them later with an array index.

Declaring and Initialization of One Dimension Array

Example: float mark[5]
Here, we declared a floating-point list. It can also hold 5 floating-point values as shown in the figure below. Where, mark[0] is the first element, followed by mark[1], and so on.

initialization of array in c language

It’s important to remember that once an array is declared, it can’t be modified in size or shape.

Declaring and Initialization of Two Dimension Array

You can make an array of arrays in C programming. These arrays are called multidimensional arrays. In C programming, a two-dimensional (2D) array is also known as a matrix. A table of rows and columns can be used to represent a matrix.

For example: float x[3][3];

Here, x is a two-dimensional array that can hold 9 elements. You can think of the array as a table with 3 rows and each row has 3 columns.

Multidimensional Arrays in C / C++ - GeeksforGeeks

Examples on Arrays in C Programming

Write a program to input any 10 numbers and count the even & odd ones

/*WAP to input any 10 numbers and count the even & odd ones */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,even,odd;
printf("Enter any 10 numbers: \n");
for(i=1;i<=10;i++)
{
	scanf("%d",&a[i]);
	if(a[i]%2==0)
	{
	even++;
	}
	else
	{
		odd++;
	}
}
printf("\n There are %d even numbers & %d odd in your entered list",even,odd);
getch();
}

Write a C program to accept ‘n’ numbers and arrange them in an ascending order

/* WAP to accept 'n' numbers and arrange them in an ascending order */
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N: \n");
scanf("%d", &n);
printf("Enter the numbers: \n");
for(i=0;i<n;i++)
{
    scanf("%d", &number[i]);	
}
for(i=0;i<n;i++) 
   {
    for(j=i+1;j<n;j++)
    {
    if (number[i]>number[j]) 
    {
     a =  number[i];
     number[i] = number[j];
     number[j] = a;
    }
    }
   }
printf("The numbers arranged in ascending order are given below: \n");
for (i = 0; i < n; i++)
{
printf("%d\n", number[i]);
}
getch();
}

Write a C program to add any two 3×3 matrix

/*WAP to add any two 3x3 matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the first 3x3 matrix elements: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	scanf("%d",&a[i][j]);
	}
}
printf("Enter the second 3x3 matrix elements: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
		scanf("%d",&b[i][j]);
	}
}
printf("The addition of above matrices is: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	c[i][j]=a[i][j]+b[i][j];
	printf("%d\t",c[i][j]);
	}
	printf("\n");
}
getch();	
}

Write a C program to multiply any two 3×3 matrix

/*WAP to multiply any two 3x3 matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],m[3][3],i,j,k;
printf("Enter the first 3x3 matrix elements: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	scanf("%d",&a[i][j]);
	}
}
printf("Enter the second 3x3 matrix elements: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	scanf("%d",&b[i][j]);
	}
}
printf("The multiplication of above matrices is: \n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	m[i][j]=0;
	for(k=0;k<3;k++)
	{
	    m[i][j]=m[i][j]+a[i][k]*b[k][i];
	}
	printf("%d\t",m[i][j]);	
	}
	printf("\n");
}
getch();	
}
Print Friendly, PDF & Email

Leave a Comment

Hey, wait!Get Regular Update's

Subscribe Stay Connected, Keep Learning!