0% found this document useful (0 votes)
13 views8 pages

Array in C

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
13 views8 pages

Array in C

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

What is Array in C?

Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To create an array, define the data type (like int) and specify the name of the array followed
by square brackets [].
To insert values to it, use a comma-separated list, inside curly braces:
int myNumbers[] = {25, 50, 75, 100};

An array in C is a fixed-size collection of similar data items stored in contiguous


memory locations. It can be used to store the collection of primitive data types such
as int, char, float, etc.,

C Array Declaration

In C, we have to declare the array like any other variable before using it. We can
declare an array by specifying its name, the type of its elements, and the size of its
dimensions. When we declare an array in C, the compiler allocates the memory
block of the specified size to the array name.

Syntax of Array Declaration

data_type array_name [size];


int arr[50];
or
data_type array_name [size1] [size2]...[sizeN];

where N is the number of dimensions.


C Array Initialization
Initialization in C is the process to assign some initial value to the variable. When
the array is declared or allocated memory, the elements of the array contain some
garbage value. So, we need to initialize the array to some meaningful value. There
are multiple ways in which we can initialize an array in C.
1. Array Initialization with Declaration
data_type array_name [size] = {value1, value2, ... valueN};
int arr[5]={2,4,8,12,16};

2. Array Initialization with Declaration without Size

data_type array_name[] = {1,2,3,4,5};

The size of the above arrays is 5 which is automatically deduced by the compiler.
C Program To Read & Print Elements Of Array
#include <stdio.h>
int main()
{
int a[1000],i,n;
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
return 0;
}

One dimensional array in C


One-dimensional arrays, also known as single arrays, are arrays with only one dimension or a single
row.

Syntax of One-Dimensional Array in C


The syntax of a one-dimensional array in C programming language is as follows:
dataType arrayName[arraySize];
Example of One-Dimensional Array in C
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
for(int i=0; i<5; i++) {
printf("numbers[%d] = %d\n", i, numbers[i]);
}
return 0;
}
Output:
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50

Two Dimensional Array in C


The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];

Consider the following example.

int twodimen[4][3];

Here, 4 is the number of rows, and 3 is the number of columns.

Two-dimensional array example in C


#include<stdio.h>
int main(){
int i,j;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}

Output

arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
C 2D array example: Storing elements in a matrix and printing it.
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}

printf("\n printing the elements ....\n");


for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
}
}
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34

Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....

56 10 30
34 21 34
45 56 78

C program to find the sum of two matrices


include <stdio.h>
int main()
{
int a[3][3], b[3][3], result[3][3];
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
scanf("%d", &b[i][j]);
}
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}

// Displaying the sum


printf("Sum Of Matrix:\n");

for (int i = 0; i < 3; ++i)


{
for (int j = 0; j < 3; ++j)
{
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter elements of 1st matrix
2
2
2
2
2
2
2
2
2
Enter elements of 2nd matrix
3
3
3
3
3
3
3
3
3
Sum Of Matrix:
5 5 5
5 5 5
5 5 5
C program to multiply two Matrices
#include<stdio.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 1 1
2 2 2
3 3 3
enter the second matrix element=
1 1 1
2 2 2
3 3 3
multiply of the matrix=
6 6 6
12 12 12
18 18 18
#include <stdio.h>

int main()
{
int r, c, i, j, matrix[10][10], transpose[10][10];

printf("Enter the number of rows and columns of a matrix\n");


scanf("%d%d", &r, &c);
printf("Enter elements of the matrix\n");
for (i= 0; i< r; i++)
for (j= 0;j<c;j++)
scanf("%d", &matrix[i][j]);

for (i= 0; i< r; i++)


for (j= 0;j<c;j++)
transpose[j][i] = matrix[i][j];

printf("Transpose of the matrix:\n");

for (i= 0; i< r; i++){


for (j= 0;j<c;j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
return 0;
}

You might also like