0% found this document useful (0 votes)
14 views23 pages

Arrays

Uploaded by

Shubham Nagpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

Arrays

Uploaded by

Shubham Nagpal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

What is Array in C?

Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value
An array is a variable that can store multiple values. For example, if you want
to store 100 integers, you can create an array for it.

int data[100];

How to declare an array?


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: dataType arrayName[arraySize];

For example, float mark[5];

Here, we declared an array, mark, of floating-point type. And its size is 5.


Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be changed once it
is declared.

Example:
###include<stdio.h>
void main()
{
int i;
int arr[] = {2, 3, 4}; // Compile time array initialization
for(i = 0 ; i < 3 ; i++)
{
printf("%d \t",arr[i]);
}
}
Types of Array in C
There are two types of arrays based on the number of dimensions it has. They
are as follows:
1. One Dimensional Arrays (1D Array)
2. Multidimensional Arrays
1. One Dimensional Array in C
The One-dimensional arrays, also known as 1-D arrays in C are those arrays
that have only one dimension.
Syntax of 1D Array in C

array_name [size];

Example:
#include <stdio.h>

main()
{

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i)
{
printf("%d\n", values[i]);
}

}
2. Multidimensional Array in C
Multi-dimensional Arrays in C are those arrays that have more than one
dimension. Some of the popular multidimensional arrays are 2D arrays and 3D
arrays.
A. Two-Dimensional Array
B. Three-Dimensional Array

A. Two-Dimensional Array

A Two-Dimensional array or 2D array in C is an array that has exactly two


dimensions. They can be understood in the form of rows and columns
organized in a two-dimensional plane.

Syntax of 2D Array in C:

array_name[size1] [size2];

Here,
• size1: Size of the first dimension.
• size2: Size of the second dimension.

Example of 2D Array in C
// C Program to illustrate 2d array
#include <stdio.h>
main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}

Output
10 20 30
40 50 60

B. Three-Dimensional Array in C

Another popular form of a multi-dimensional array is Three-Dimensional Arrayor 3D


Array. A 3D array has exactly three dimensions. It can be visualized as a collection
of 2D arrays stacked on top of each other to create the third dimension.

Syntax of 3D Array:

array_name [size1] [size2] [size3];

#include <stdio.h>
main()
{

// 3D array declaration
int arr[2][2][2] = { 10, 20, 30, 40, 50, 60, 70, 80};

// printing elements
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 2; k++)
{

printf("%d ", arr[i][j][k]);


}
printf("\n");
}
printf("\n \n");
}
return 0;
}
Passing Arrays to Function
In C, there are various general problems which requires passing more than one variable of the
same type to a function. For example, consider a function which sorts the 10 elements in
ascending order. Such a function requires 10 numbers to be passed as the actual parameters
from the main function. Here, instead of declaring 10 different numbersand then passing into
the function, we can declare and initialize an array and pass that into the function. This will
resolve all the complexity since the function will now work for any number of values.

// Program to calculate the sum of array elements by passing to a function

#include <stdio.h>
float calculateSum(float num[]);
main()
{
float result;
float num[] = {23.4, 55, 22.6, 3, 40.5, 18};
result = calculateSum(num);

printf("Result = %f", result);


}

float calculateSum(float num[])


{
float sum = 0.0;
for (int i = 0; i < 6; ++i)
{
sum += num[i];
}
return sum;
}

Output
Result = 162.50
Array of Characters
An array of characters in C is used to store a sequence of characters, such as a string. It is
essentially a collection of char data types.

Syntax

To declare an array of characters, you use the following syntax:

char arrayName[size];

Example

Here’s a simple example of declaring and using an array of characters in C:

You might also like