logo of SA Coder
        Menu
sign in

      References



Array



Arrays are an essential data structure in C programming language that enables you to store and access multiple values of the same data type. It's a variable that can store similar types of data elements at continuous memory locations.


One-Dimensional Arrays



The most basic type of array in C programming is a one-dimensional array, which is an array of elements of the same data type, like integers or floats. You can access each element in a one-dimensional array using an index starting from 0 and ending at n-1, where n is the size of the array. Let's consider an example of a one-dimensional array of integers.


copy

#include <stdio.h>

int main() {
    int data[5];

    data[0] = 54;
    data[1] = 65;
    data[2] = 61;
    data[3] = 94;
    data[4] = 89;

    for (int i = 0; i < 5; i++) {
        printf("The %dth element is %d\n", i, data[i]);
    }

    return 0;
}



In this example, we have created an array called 'data' with a size of 5 integers. We have assigned values to each element of the array using the index operator and accessed each element using a for loop that iterates over the array.


Multi-Dimensional Arrays



Multi-dimensional arrays are arrays of arrays, which are useful when you need to store data in a table or matrix format. You can create multi-dimensional arrays in C programming by defining the size of each dimension in the array declaration. Here's an example of a two-dimensional array of integers.


copy

int data[10][5];



In this example, we have created a two-dimensional array called data with 10 rows and 5 columns, which can store a total of 50 integers.


Manipulating Array Elements



You can manipulate array elements in C programming using different operations like inputting, outputting, and finding the maximum and minimum value. Let's consider some examples.



Example 1: Inputting and Displaying Float Numbers



copy

#include <stdio.h>

int main() {
    float data[10];

    printf("Enter 10 floating-point numbers:\n");
    for (int i = 0; i < 10; i++) {
        scanf("%f", &data[i]);
    }

    for (int i = 0; i < 10; i++) {
        printf("The %dth element is %.2f\n", i, data[i]);
    }

    return 0;
}



In this example, we have created an array called 'data' of size 10 floats. We have taken user input for 10 floating-point numbers and assigned them to each element of the array. Finally, we have displayed all the elements of the array using a for loop that iterates over the array.



Example 2: Finding the Maximum and Minimum Number



copy

#include <stdio.h>

int main() {
    int n, i;
    printf("How many numbers will you input?\n");
    scanf("%d", &n);
    
    int a[n], max, min;
    printf("Enter %d numbers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    
    max = min = a[0]; // set initial values
    
    for (i = 1; i < n; i++) {
        if (a[i] > max) {
            max = a[i];
        }
        if (a[i] < min) {
            min = a[i];
        }
    }
    
    printf("Maximum number is: %d\n", max);
    printf("Minimum number is: %d\n", min);
    
    return 0;
}



Output


how many number will be input by you 5 enter number(1) 54 enter number(2) 98 enter number(3) 65 enter number(4) 85 enter number(5) 45 maximum number is = 98 minimum number is = 45



The program prompts the user to input the number of integers to be entered, and then takes those numbers as input. It then finds the maximum and minimum values of those numbers using a nested loop and conditional statements. Finally, it prints the maximum and minimum values. In the above example, the maximum number entered was 98 and the minimum was 45.




Please login first to comment.