Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Arrays in C

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 338
    Comment on it

    An array is a homogeneous collection of data. An array is a container that stores a fixed number of values of same type. The length of array is specified when we create an array. Once created, its legth is fixed. We can think of array as a structure that can store a fixed size sequential collection of data of a single kind.

    Instead of declaring individuals variables as num1, num2, num3.....num99, we can store all the values in a single array. A specific element of an array can be accessed by its index.

    The lowest address/ index corresponds to the first element of the array while the highest address corresponds to the last element. The index always start with 0. Thus the address of Nth element will be (N-1).










    Here, Num[0] represents first element in the array and Num[N] represents the last element.

    Declaration of Array:

    Array can be declared using this syntax:

    <datatype> <array name> [size];

    e.g of above syntax:

    int roll[5];

    What does a C Array Declaration tells to Compiler?

    1. Type of Array.
    2. Name of the Array.
    3. Number of Dimension.
    4. Number of elements in each dimension.

    Types of Arrays:

    1. Single Dimensional Array: Single or one dimensional array is the simplest form of array. It contains only one row to store data. Examples of single dimensional array:

    int n[3] = {2,5,8};

    char chr = Learn;




    2. Multidimensional Array: C allows to create 2 or 3 dimensions. A multi- dimensional array is an array in which an array element is identified by a sequence of indices and not a single index. A two- dimensional array requires two indices. A 2D array is an array of arrays. A 3D array is an array of arrays of arrays.

    A multi dimenional array is declared using the following syntax:

    <datatype> <arrayname>[size1][size2][size3]...[size n];

    Examples for above syntax:

    int mat[3][4];
    name is a 2D array.
    int table[5][5][20];
    table is the name of our 3D array.
      Column 0 Column 1 Column 2 Column 3
    Row 1





    Row 2





    Row 3






    Program to print Transpose of an array

    int main()
    int matrix[10] [10], transpose&#95;Mat[10][10], row, col, i,j;
    /* getting array values from user and storing in array*/
    printf(\n Enter values for rows and columns);
    scanf(%d %d, &row, &col);
    printf(\n Enter values for the Matrix);
    for(i = 0; i < row; i++) 
    for(j = 0; j < col; j++)
    scanf(%d, &matrix[i][j]);
    /*Displaying the elements of matrix*/
    printf(The Original Matrix is:)
    for(i = 0; i < row; i++)
    for(j = 0; j < col; j++)
    printf(%d , matrix[i][j]);
    /* Finding Transppose of matrix*/
    for(i = 0 ; i < row; i++)
    for(j = 0; j < col; j++)
    matrix[i][j] =  transpose&#95;Mat[j][i];
    /* Displaying Transposed matrix*/
    printf(The Transposed Matrix is:)
    for(i = 0; i < row; i++)
    for(j = 0; j < col; j++)
    printf(%d , transpose&#95;Mat[i][j]);


    Enter values for rows and columns: 3 4
    Enter values for the Matrix:
    Enter values for matrix11: 2 
    Enter values for matrix12: 3
    Enter values for matrix13: 4
    Enter values for matrix14: 5
    Enter values for matrix21: 6
    Enter values for matrix22: 7
    Enter values for matrix23: 8
    Enter values for matrix24: 9
    Enter values for matrix31: 10
    Enter values for matrix32: 11
    Enter values for matrix33: 12
    Enter values for matrix34: 13
    The Original Matrix is:
    2    3   4   5
    6    7   8   9
    10    11  12  13
    The Transposed Matrix is:
    2    6   10
    3    7   11  
    4    8   12
    5    9   13

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: