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
    • 99
    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).

    Num[0]

    Num[1]

    Num[2]

    Num[3]

    Num[4]

    Num[5]

    .....

    ....

    Num[N]

    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

    5

    8

    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

    mat[0][0]

    Mat[0][1]

    Mat[0][2]

    mat[0][3]

    Row 2

    Mat[1][0]

    mat[1][1]

    mat[1][2]

    mat[1][3]

    Row 3

    mat[2][0]

    mat[2][1]

    mat[2][2]

    mat[2][3]

     

    Program to print Transpose of an array

    #include<stdio.h>
    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]);
    }
    }
    }
    

    Output:

    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
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: