How can we initialize an array in C
Introduction
The definition of an array in the C programming language is a way to bring together several items of the same type. In addition to data types chosen by users, like structures, these entities or elements may be of the int, char, float, or double data types. The items must all have the same data type for them to have their data stored within a single array. The leftmost index, which is 0th, and the highest index (n-1), are used to store the elements in a row, from left to right.
The two types of arrays in C are single-dimensional arrays and multidimensional arrays.
Single Dimensional Arrays
The most straightforward kind of array present in C is a single-dimensional array, sometimes known as a 1-D array. This kind of array contains elements with comparable kinds, so its indices can access those elements.
Multidimensional Arrays
A two-dimensional array is the most prevalent multidimensional array utilized by the C programming language. Nevertheless, depending on the user's system's compiler, there may be more than two dimensions. These arrays are made up of elements that are other arrays.
C array declaration and initialization
The declaration and initialization of an array of values can be done in various ways. Any type of data (such as an int, float, double, or char) may be declared as an array in C. An array of variables in C can be declared and initialized as shown below.
Declaring an Array by Choosing a Size
Declaring an array involves stating its size or its number of elements. The dimension of an array indicates how many elements it can contain. In the most recent version of C, you can either define an array by simply providing its dimensions at the beginning of the declaration, or you can supply a user-specified size. An array of values can be declared using the following syntax: just state its size.
Syntax
int array1[20];
char array2[7];
//declare an array by specifying user-defined size’
int size=9
int array3[size]
Declaring an Array by Initializing Its Elements
When an array is declared, it may be initialized. The programming language will allocate an array with the same size as the number of array elements when using this form of array declaration. An array of values can be simultaneously declared and initialized using the following syntax:
Syntax
//When declaring an array, initialize it.
int my_array[] = {10, 20, 30, 40, 50}
Although the size of the array has not been given in the above syntax, the program's compiler will allocate a number of 5 numeric elements to the formed array of 5.
Array Declaration via Providing the Size and Setting up the Elements
Another way to create an array is to declare it, determine its size, and allocate the components. How this array is created is different from how it was created previously. The rest of each element is automatically initialized as 0 by the program's compiler, if the total number of prepared elements is lower than the given size of the array. To understand this, refer to the following syntax:
Syntax
//Declare an array with the size and
Initializing when a declaration is made
int my_array1[5] = {100, 200, 300, 400, 500}; // my_array1 = {100, 200, 300, 400, 500}
Initializing an array Use of a Loop
A loop can be used to initialize an array as well. For accessing all array indices beginning at 0, the loop iterates from 0 to (size - 1). The array items are initialized using the "for loop" in the following syntax. In C, this is the most typical method of initializing an array.
// declare an array.
int my_array[5];
// initialize array using a "for" loop.
int i;
for(i = 0; i < 5; i++)
{
my_array[i] = 2 * i;
}
// my_array = {0, 2, 4, 6, 8}
An array of five elements is specified first in the syntax above. The list is then initialized by iterating from index 0 to (size - 1) within a for loop.
Access Array Elements
The term "zero-based indexing" refers to the fact that the array has indices that range from "0" to "array_size -1" because it is stored in memory contiguously. In the array, this indexing indicates the location.
Accessing any of the components of the array is done using the array indices in the manner described below:
array_name[index]
Within the square brackets ("[]") is the value of the element that has to be retrieved. Integers between [0, size] comprise the index's data range.
Example 1: Example to execute array
#include <stdio.h>
int main()
{
int my_array[8];
printf("Entering the array elements:\n");
int i;
for (i = 0; i < 7; i++)
{
scanf("%d", &my_array[i]);
}
printf("\nThe Array elements are:\n");
for (i = 0; i <= 6; i++)
{
printf("%d ", my_array[i]);
}
return 0;
}
Output
![How can we initialize an array in C](https://static.tutorialandexample.com/c/how-can-we-initialize-an-array-in-c1.png)
Explanation
In the example above, an 8-dimensional integer array is declared. There can only be eight integer elements in this array. In this example, the user inputs the array components into a "because of loop." The result for these items is printed using a "for loop" similarly. To cycle through every element of the array, a loop runs from 0 to 7 twice.
Example 2: Insertion and deletion in an Array
#include <stdio.h>
int main()
{
int i;
int my_array[10] = {11, 6, 10, 50, 32, 56, 15, 98, 43, 22};
int position;
printf("Enter the position: ");
scanf("%d", &position);
int element;
printf("Enter the element to be inserted: ");
scanf("%d", &element);
if (position > 10)
{
printf("Input is invalid !");
}
else
{
for (i = 9; i >= position - 1; i--)
my_array[i + 1] = my_array[i];
my_array[position - 1] = element;
printf("Array after insertion the elment is:\n");
for (i = 0; i <= 10; i++)
printf("% d ", my_array[i]);
}
return 0;
}
Output
![How can we initialize an array in C](https://static.tutorialandexample.com/c/how-can-we-initialize-an-array-in-c2.png)
Explanation
The element that has to be introduced is used for input in the example above. Additionally taken into consideration as input is the location in which this part is to be kept. To make room for the added element, the elements of the array have been shifted to the right. The array's size is increased following insertion.
Example 3: Search an Element in the Array
#include <stdio.h>
int main()
{
int i, element;
int my_array[10] = {11, 6, 10, 50, 32, 56, 15, 98, 43, 22};
printf("Enter element to be searched:\n");
scanf("%d", &element);
for (i = 0; i <= 9; i++)
{
if (my_array[i] == element)
{
printf("Element found at index %d", i);
break;
}
}
if (i == 10)
{
printf("\nElement not found!");
}
return 0;
}
Output
![How can we initialize an array in C](https://static.tutorialandexample.com/c/how-can-we-initialize-an-array-in-c3.png)
Example
An element that the user provides is used in the illustration above. By comparing every array element with the element to be sought, the collection is traversed to do a linear search for this element. If the element is present, its location is written, and the "for loop" is ended. If the element is absent, "for loop" iterates from 0 to 9 without interruption, with "i" corresponding to 10.
Example: Sorting an element in an Array
#include <stdio.h>
int main()
{
int i, j, temp;
int my_array[10] = {9, 6, 15, 40, 32, 36, 25, 48, 33,98};
printf("Original array is: \n");
for (i = 0; i < 10; i++)
{
printf("%d ", my_array[i]);
}
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (my_array[j] > my_array[i])
{
temp = my_array[i];
my_array[i] = my_array[j];
my_array[j] = temp; }
}
}
printf("\n\nSorted array in descending order is: \n");
for (i = 0; i < 10; i++)
{
printf("%d ", my_array[i]);
}
return 0;
}
Output
![How can we initialize an array in C](https://static.tutorialandexample.com/c/how-can-we-initialize-an-array-in-c4.png)
Explanation
In the preceding instance, an array containing ten bytes is put up. The array components are organized in decreasing order utilizing the bubble sort algorithm.
Example 5: Finding the largest and smallest element of an array
#include<stdio.h>
int main()
{
int my_array[10] = {14, 16, 50, 30, 23, 86, 65, 88, 33, 32};
int i, smallest, largest;
smallest = largest = my_array[0];
for(i = 0; i < 10; i++)
{
if(my_array[i] > largest)
{
largest = my_array[i];
}
if(my_array[i] < smallest)
{
smallest = my_array[i];
}
}
printf("The smallest element in the array is: %d", smallest);
printf("\nThe largest element in the array is: %d", largest);
printf("\n\n");
return 0;
}
Output
![How can we initialize an array in C](https://static.tutorialandexample.com/c/how-can-we-initialize-an-array-in-c5.png)
Explanation
Both the biggest and lowest elements in an unsorted array are reported in the program above. The findings are stored in the two variables, greatest and smallest. The initial elements of the array are used to set up these variables. The most significant and most minor elements are contrasted with each other. At the same time, the array is linearly traversed, and the corresponding values are modified if a new greatest or smallest member is discovered.