| by Arround The Web | No comments

Arrays in C

The array represents the collection of various items that have a similar data type and are stored in the physically adjacent memory addresses. The data type of the items in an array must be similar and it can be any data type as char, integers float and double, or even the structures. In an array, the sequence of the items is stored in the form of indices. The items in the array are stored from left to right. The left-most items are the initial items with zero (0th) index. The right-most items have the index (n-1). The “n” signifies the total number of items in the array. We may access any item or member of the array with its index which is the memory location of the item.

In programming languages, we are required to make the arrays because we have to define more variables with the ordered values like 1-200. So, instead of separately defining and storing the values in 200 variables, we rather just make an array of size (200).

Procedure:

We will try to learn about the concept of arrays in the C (Visual Studio). With the help of examples, we will learn the basic methods for the initialization, declaration, and the various operations that we can perform on the arrays for various software applications. We will also try to learn more about the arrays depending on their types (single-dimensional and multi-dimensional) with their syntax.

Example 1:

The first example of this article deals with both the types of an array – single dimensional & multi-dimensional arrays. The single-dimensional arrays store the variables in them that have similar data types and the stored elements/variables can be easily accessed using the index information of each element in the array. There exists the different ways to declare an array. We can either declare it with the array size or with the user input array size. We can also declare the array by initializing the elements without an array size specification or with size specification.

Declaration and Initialization for the Single-Dimensional Array

The previously mentioned figure explains how we can declare a single-dimensional array by specifying its size. In this example, we declare an integer array having a size equal to five, which means that the array holds the capacity to store five items in it.

We can also initialize an array by specifying the array size and its elements in the following way:

#include <stdio.h>

int main(void)
{

    int array [5] = {1,2,3,4,5 };
    int i;
    for (i = 0; i<5;i++)
printf("%d", array[i]);

    return 0;

}

In the previous example, we declared the size of an array to be five and then initialized the elements of an array. The numbers between the “{}” curly braces show that the values of array items range from 1 to 5.

We can also state an array without indicating its size. This could be done only if we initialize the elements of the array. Then, the array automatically maps its size depending on the number of elements that we initialized for the array.

Here, an array of size (five) is declared depending on the number of initialized elements for the array, even though we have not specified any array size.

Multi-Dimensional Array Declaration

These types of arrays represent a map or grid like a chess board since they are more likely to have more indices as compared to the single-dimensional array that has only one index for the slots that are in only one line. Unlike the single-dimensional arrays, multi-dimensional arrays may have more than one dimension like 2, 3, or even more than these numbers. These arrays can also be declared and initialized just as the single-dimensional arrays with a small modification in syntax that we have to set the number of brackets for the size of the array depending on its dimensions. For a 2-D array, we declare the array as in the following example:

Example 2:

After the declaration and initialization of an array, we now discuss how we can access the elements of an array. The arrays’ elements can be accessed through the indices of the array. The left-most element of the array has an index of 0 and the right-most (last) element has an index of n-1. So, for an array having an array size of “5”, if we want to access the 5th element of the array, we write the following commands in the code editor:

#include <stdio.h>

int main()
{

    int array[5] = { 1,2,3,4,5 };

printf("%d", array[4]);    

}

We tried to access the 5th element of the array, so we write the index 5 with the name of the array in brackets “[]” and printed it. The fifth element in the array is “5” and it is demonstrated in the output.

Example 3:

We use all the methods discussed in the earlier examples (to declare and initialize an array) to implement an example in C using the Visual Studio to get a hand on experience with our gained concepts. In this example, we first declare an array of five integers and initialize the array by taking the input from the user. Then, we write these values in the arrays and display them.

For the implementation, we start by importing the required header $ # include <stdio.h>. Since we get the input from the user and call the print function to display the output, we need to include these functions by including this library in the code. Then, we declare the array of the name “array” with an array size equal to five. After that, to initialize the elements of the array, we call the “scanf ()” function to take the input from the user and write it on the index of the array with “&array”.

We do this function by calling in the “for loop” running on index values from 0 to 5. Until this step, the array’s elements are initialized. Then, we simply display the stored values by calling the “print ()” method. It prints the values of the array of the index “i” from 0 to 5 with the increment (+1) in the “for loop”. The example is implemented and shown in the following:

#include <stdio.h>

int main()
{
int array[5];
printf("take input from user for 5 integers: ");
for (int i = 0; i< 5; ++i)
{
scanf("%d", &array[i]);
}
printf("stored integers: ");
for (int i = 0; i< 5; ++i)
 {
printf("%d\n", array[i]);
}
return 0;
}

Now, the five elements from the user are first stored in the array and then they are displayed.

Conclusion

Arrays are the most important concepts in C when it comes to the declaration of a large number of variables with the same data type in the code. The article throws light on the concept of arrays in C by showing the various methods for array declaration, its elements initialization, and how to access the elements in the array. This article is quite helpful for you if you follow it as it is.

Share Button

Source: linuxhint.com

Leave a Reply