C

Examples Of Arrays In C Programming

An array is a collection of similar data types. An int array, for example, contains elements of int types, whereas a float array contains elements of float types.

Why is an array required in C programming?

Consider the following scenario: you need to find the average of 100 integer numbers entered by the user. There are two ways to do this in C: 1) Create 100 variables with int data types, then perform 100 scanf() operations to store the entered values in the variables before calculating the average of them. 2) Use a single integer array to store all of the values, loop through the array to store all of the entered values, and then compute the average.
Which solution do you think is superior? Obviously, the second solution is more convenient: store similar data types in a single variable and access them later using an array index (we will discuss that later in this tutorial).

In C, how do you declare an array?

int num[35];  /* An integer array of 35 elements */
char ch[10];  /* An array of characters for 10 elements */

Similarly, an array can be of any data type, including double, float, short, and so on.

In C, how do you get to an array element?

To access any element in an array, use array subscript (or index). Because subscript begins with 0, arr[0] represents the first element in the array arr.
In general, arr[n-1] can be used to access the array’s nth element. where n can be any integer number
As an example:

int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*/

Array Example To find the average of four integers in C programming

#include <stdio.h>
int main()
{
    int avg = 0;
    int sum =0;
    int x=0;

    /* Array- declaration – length 4*/
    int num[4];

    /* We are using a for loop to traverse through the array
     * while storing the entered values in the array
     */
    for (x=0; x<4;x++)
    {
        printf("Enter number %d \n", (x+1));
        scanf("%d", &num[x]);
    }
    for (x=0; x<4;x++)
    {
        sum = sum+num[x];
    }

    avg = sum/4;
    printf("Average of entered number is: %d", avg);
    return 0;
}

Output:

Enter number 1 
10
Enter number 2 
10
Enter number 3 
20
Enter number 4 
40
Average of entered number is: 20

Let us go over the most important aspects of the preceding program:

Fill out the array with data.

Because the array has a size of 4, we are iterating through it from 0 to 3. We are displaying a message to the user to enter the values within the loop. Using the scanf function, all input values are stored in the corresponding array elements.

for (x=0; x<4;x++)
{
    printf("Enter number %d \n", (x+1));
    scanf("%d", &num[x]);
}

Data extraction from an array

If we want to display the array elements, we can use the for loop in C like this.

for (x=0; x<4;x++)
{
    printf("num[%d]\n", num[x]);
}

Various methods for initializing an array

In the preceding example, we simply declared the array and then initialized it with the values entered by the user. However, you can also initialize the array during the declaration, as shown below:

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

OR (both are same) (both are same)

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

Uninitialized arrays are always filled with garbage values.

Array – Memory representation in C

More Arrays in C Topics:
2D array – C supports multidimensional arrays such as 2D and 3D arrays. However, the most common and widely used array is the 2D – two dimensional array. In this post, you will learn how to declare, read, and write data in a 2D array, as well as some of its other features.
Passing an array to a function- While we typically pass values and variables when calling a function, we can also pass arrays. A function can be passed an array’s element as well as the entire array (by simply specifying the array name, which acts as a pointer).
Pointer to array – In C, pointers can be used to access and manipulate array elements. You can easily handle arrays by using pointers. All the elements of an array can be accessed simply by assigning the array’s base address to a pointer variable.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button