Array in C

As a programmer, It is always important to know about simple topics like an array, string, functions, looping, etc. As with other programming languages, the C programming language also has great importance in arrays. In this blog, I have tried to explain arrays in the C programming language in simple language as well as different examples
 

Introduction to array

The array is the collection of homogeneous data item can be represented by a single variable name. The individual data item in an array is called an element. A data item is stored in memory with one or more adjacent storage locations depending upon its type. The array is a kind of data structure that can store a fixed-size sequential collection of elements of the same types. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
 
All arrays consist of contiguous memory locations. The lowest address corresponding to the first element and the highest address to the last element.
 
introduction to array
 
 
The elements of the array share the same variables name but each element has a different index of number known as subscripts. Each array element is referred to by specifying the same array name followed by one or more subscripts with each enclosed in square brackets. In C subscript is non-negative integer starts at zero which is known as lower bound and it goes up to n-1 which is known as upper bound. In an five-element array, the array elements are marks[0], marks[1],…marks[4] as shown below:
 
Here lower bound = 0 and upper bound = 5-1 = 4.
 
array elements table
 
 

Why do we need an array?

When we have to store a large amount of data into different variables, the program becomes so long and complex. To reduce the size and complexity of the program we need an array. If we have a collection of similar data elements, we have the inconvenience to give each one a unique variable name. If we have to store the age of 10 students then we have to use different variable names as in the program below:
 
    // Program to read and display average age of 7 student
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int age1, age2, age3, age4, age5, age6, age7;
        float avg;
        printf("enter the age1n");
        scanf("%d", &age1);
        printf("enter the age2n");
        scanf("%d", &age2);
        printf("enter the age3n");
        scanf("%d", &age3);
        printf("enter the age4n");
        scanf("%d", &age4);
        printf("enter the age5n");
        scanf("%d", &age5);
        printf("enter the age6n");
        scanf("%d", &age6);
        printf("enter the age7n");
        scanf("%d", &age7);
        avg = (age1 + age2 + age3 + age4 + age5 + age6 + age7) / 7;
        printf("the average age of seven student is %f", avg);
        getch();
    }
 
This program reads the age of seven students only so we use seven different variables, if we have to enter the age of 100 students? Would we declare the 100 different variables then? Of course not. So clearly, we have a convenient way to refer to such collections of similar data elements with the help of an array. And this problem is solved by the use of an array by providing a way to refer to individual items in a collection by using the same variable name.
 
    // Program to read and display average age of 7 student
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int age[7];
        int i, j;
        for (i = 0; i < 7; i++)
        {
            printf("enter the agen");
            scanf("%d", age[i]);
        }
        for (j = 0; j < 7; j++)
        {
            printf("%d", age[j]);
        }
        getch();
    }
 

Types of Array (Array dimension)

Declaring the name and types of an array and defining the number of elements in the array is known as dimensioning of the array. The dimension of the array can be from one-dimension to n-dimension (multi-dimensional). In general, there is the single-dimensional or multidimensional array used in programming. The number of subscripts determines the dimensions of the array.
 

a. One Dimensional Array

A one-dimensional array has only a single subscript. The one-dimensional arrays are known as vectors. The subscript numbers identify the number of individual elements in an array. It stores data only row-wise or column-wise.
 

Declaration of one Dimensional Array

We have to declare the array before using it. While declaring a one-dimensional array we have to define the number of elements we are going to use and the data types that the array can store in it. One dimensional array is declared as:
 
data_types array_name[size];
 
Here data_type denotes any valid C data types and array_name denotes the name of the array and it can be any valid C identifier. The size of the array specifies the number of elements that can be stored in an array. It may be a positive integer constant or constants integer expression.
 
Here are some examples of a valid array declaration:
 
 
    int age[100];
    float salary[500];
    char grade[20];
 
 
Following are some of the invalid array declarations:
 
 
    int value[0]; //zero subscripted array is not supported
    int marks[0.15] //subscript value must be an integer
    int n[-40] //subscript value ,ust be non-negative
 
 

Accessing array elements

The elements of the array can be accessed by specifying the array name followed by subscript in brackets. In C, the array subscripts start from 0. Hence if there is an array of size 5 then the valid subscripts will be from 0 to 4. The last valid subscript is one less than the size of the array. The last subscript is sometimes known as the upper bound of the array and 0 is known as the lower bound of the array.
 
Let us take an array:
 
int arr[5]; //size of array 5, can hold five integer elements.
 
The elements of this array are:
 
arr[0],arr[1],arr[3],arr[4]
 
The subscript can be any expression that yields an integer value. It can be any constant, integer variable, integer expression, or return value(int) from a function call. For example, if i and j are integer then these are some valid subscripted array elements-
 
 
    arr[3];
    arr[i];
    arr[i+j];
 
 

Processing array elements

For processing arrays, we generally use a for loop and the loop variable is used at the place of subscript. The initial value of the loop variable is taken as 0 since the array subscript starts from zero. The loop variable is increased by 1 each time so that we can access and process the next elements in the array. The total number of passes in the loop will be equal to the number of elements in the array and in each pass will process one element.
 

Initialization of array

Each array element can be initialized when an array is declared. The initial values must appear in the order in which they will be assigned to the individual array elements, enclosed in curly braces {} and separated by commas. The syntax for initialization of array is:
 
data_type array_name[size] = {value1, value2, value3......valueN};
 
Example is:
 
 
    int digit[5] = {1,2,3,4,5};
    float salary[4] = {200.50,69.90,8.6,7.87};
    char color[3] = {‘R’, ‘G’, ‘B’};
 
 
Here the first one has five elements values five all are of integer types. The second one has four-element values all are of float type and the last one has three elements all are of char types.
 

Solved Programs

 
 
// program to find the largest and smallest value in array using C language
#include <stdio.h>
#include <conio.h>
void main()
{
    int i, j, arr[10] = {18, 45, 90, 67, 23, 45, 23, 34, 67, 78};
    int min, max;
    min = max = arr[0];
    for (i = 0; i < 10; i++)
    {
        if (arr[i] < min)
            min = arr[i];
        if (arr[i] > max)
            max = arr[i];
    }
    printf("maximun value is = %dn minimum value is = %d", max, min);
    getch();
}
 
    // program to search the given data from the lsit of element from given array
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int i, num, flag = 0, arr[10] = {8, 7, 6, 45, 34};
        printf("enter the number tou want to search");
        scanf("%d", num);
        for (i = 0; i < 5; i++)
        {
            if (num == arr[i])
            {
                flag = 1;
                break;
            }
        }
        if (flag == 1)
            printf("data found");
        else
            printf("data not found");
        getch();
    }
 

b. Multidimensional array (two-dimensional arrays)

When we declare an array more than one dimensional it is known as a multidimensional array. It consists of two subscripts in which first gives a number of rows and the second gives a number of column sizes.
 

Declaration of two or multidimensional array

 
data_type array_name[row_size][column_size];
 
Example:
 
int mat[3][2];
 
So the above example declaration is a 2D array of integer type. This integer array has been named mat and it can hold up to 6 elements (3rows and 2 columns).
 

Solved Programs

 
    // program to input 3X3 matrix and display it
    #include <stdio.h>
    #include <conio.h>
    void main()
    {
        int i, j, mat[3][3];
        printf("enter the elments of 3*3 matrix");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                scanf("%d", &mat[i][j]);
            }
        }
        printf("the elements of 3X3 matrix are:n");
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
            {
                printf("%dt", mat[i][j]);
            }
            printf("n");
        }
        getch();
    }
 

Character of Array

We can express the character easily by char data type. But the string is a group of characters that can only be represented by a character array. So that the array of characters is known as a string in C. String are defined without double quotation symbols. The last character of a string is marked by the NULL(“”) character. For example “BIKASH” is a string. A string stored in memory be using the ASCII codes of the character that form the string.
 
 

Similar Posts

Leave a Reply

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