Introduce with array in C programming

An array in C can be said as a collection or group of same data types. We can also do this with another variable but it will make our program more complicated and boring to write. Suppose you want to store CGPA of 100 students. Then how can we do this? We can do this by two methods.

First is the taking 100 variables and store every CGPA inside each variable.

Second is taking only one array and store their CGPA.

Obviously the second method is better for coder and we will also follow the second method. An array can be the best solution for this purpose.

Syntax of array in C, how to declare an array

We can declare an array in C by the type of array that what type of data it will be stored, followed by the name of array and size of array inside the square bracket. See some array declaration to get the idea about syntax of an array and how to declare an array in C.

// how to declare an array

int num[10];       // integer type array of capacity 10
float cgpa[20];     // float type array of capacity 20
char ch[15];         // character type array of capacity 15
double weight[30];    // double type array of capacity 30
......................

Analyze an array in C

Suppose, we have declared an array named cg of size 10 to store the CGPA of 10 students. When we declare the array, we will get 10 space inside the memory to store 10 float type data. Let’s try to analyze the array.

Now, lets see the syntax of variables in C and some general mistakes which people make when declaring a variable.

array in c, 1d array in c, c array

We can describe each data inside an array by index of array. The index of an array starts from 0 and ends in (n – 1).

For example, here we have declared the array of size 10. The index of first memory cell will be cg[0], second will be cg[1] ………. and the nth index will be cg[n – 1] which is cg[9] here.

Accessing the element of an array

Suppose, we have declared an array in C named cg of size 10. Now we will assign the value inside each cell and print them. So, lets see the C program bellow.

// accessing the element of an array in c
#include <stdio.h>
int main(){
    float cg[10];     // array declaration

    cg[0] = 3.12;     // assigning value
    cg[1] = 3.55;
    cg[2] = 3.22;
    cg[3] = 3.98;
    cg[4] = 3.12;
    cg[5] = 3.77;
    cg[6] = 3.32;
    cg[7] = 3.14;
    cg[8] = 3.67;
    cg[9] = 3.80;

    printf("%.2f\n", cg[2]);
    printf("%.2f\n", cg[6]);
    printf("%.2f\n", cg[9]);

    cg[8] = 3.92;      // re assigning value
    printf("\n%.2f\n", cg[8]);

return 0;
}

Output will be as follows:

array in c, accessing the element of an array

Array traversing after taking input from user

Array traversing is printing all the data from inside the array. In this program we will take the input from the user, store all the input in an array and then print them using a loop. You can use any loop although we will use for loop here.

// taking input from user store them and print from an array
#include <stdio.h>
int main(){
    float cgpa[5];
    int i;

    for(i = 0; i < 5; i++){
        printf("Enter CGPA of student %d here : ", i+1);
        scanf("%f", &cgpa[i]);      // taking input and store in the array
    }

    printf("\n");

    for(i = 0; i < 5; i++){      // printing the CGPA
        printf("CGPA of student %d is = %.2f\n", i + 1, cgpa[i]);
    }
return 0;
}

Output of this program

Sorting an array in C

Sorting means re allocating all the data inside of an array in ascending or descending order. We can do this operation using various sorting method although we will use bubble sort here to sort an array. Let’s sort the data of an array in descending order.

// sorting an array using bubble sort
#include<stdio.h>
int main(){
    int a, b, temp;
    int arr[10] = {5, 12, 70, 30, 82, 21, 11, 17, 31, 12};
    for(a = 0; a < 10; a++){
        for(b = a + 1; b < 10; b++){
            if(arr[b] > arr[a]){
                temp = arr[a];
                arr[a] = arr[b];
                arr[b] = temp;
            }
        }
    }
    printf("Sorted array is\n\n");
    for(a = 0; a < 10; a++){
        printf("%d ",arr[a]);
    }
    printf("\n");
return 0;
}

See output of this array program

Sorted array is

82 70 31 30 21 17 12 12 11 5

Process returned 0 (0x0)   execution time : 0.026 s
Press any key to continue.

You can take any type of array and perform some operation using for loop, while loop or do while loop as well as other control statement. So, try this in your own.

Previous pagegoto statement in C

Next page:  Two dimensional array in C