What are the benefits of using arrays? Compare one dimensional array with two dimensional array. Write a program to find transpose of a matrix.

This answer is restricted. Please login to view the answer of this question.

Login Now

An array is a group of related data items that share a common name. In the other words, an array is a data structure that stores a number of data items as a single entity (object).

The benefit of using arrays are given below

  1. In array, We can access the data very easily using index number
  2. We can apply searching process in array easily
  3. We can represent 2D arrays as matrices
  4. We can used to implement other data structure like linked lists, stack, queue, trees, graph etc.

The compare one dimensional array with two dimensional array is given below

Basis One Dimension Array Two Dimension Array
Definition  Store a single list of the element of a similar data type. Store a ‘list of lists’ of the element of a similar data type.
Representation Represent multiple data items as a list. Represent multiple data items as a table consisting of rows and columns.
Declaration datatype variable_name[row] datatype variable_name[row][column]
Size size of(datatype of the variable of the array) * size of the array size of(datatype of the variable of the array)* the number of rows* the number of columns.
Example int arr[7]; int arr[7][5];

Program to Transpose of Matrix

#include <stdio.h>
int main()
{
    int a[10][10], transpose[10][10], r, c;
    printf("Enter rows and columns: ");
    scanf("%d %d", &r, &c);

    // asssigning elements to the matrix
    printf("\nEnter matrix elements:\n");
    for (int i = 0; i < r; ++i)
        for (int j = 0; j < c; ++j)
        {
            printf("Enter element a%d%d: ", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }

    // printing the matrix a[][]
    printf("\nEntered matrix: \n");
    for (int i = 0; i < r; ++i)
        for (int j = 0; j < c; ++j)
        {
            printf("%d  ", a[i][j]);
            if (j == c - 1)
                printf("\n");
        }

    // computing the transpose
    for (int i = 0; i < r; ++i)
        for (int j = 0; j < c; ++j)
        {
            transpose[j][i] = a[i][j];
        }

    // printing the transpose
    printf("\nTranspose of the matrix:\n");
    for (int i = 0; i < c; ++i)
        for (int j = 0; j < r; ++j)
        {
            printf("%d  ", transpose[i][j]);
            if (j == r - 1)
                printf("\n");
        }
    return 0;
}

The output of above program is

Enter rows and columns: 3
3

Enter matrix elements:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 5
Enter element a23: 6
Enter element a31: 7
Enter element a32: 8
Enter element a33: 9

Entered matrix:
1  2  3
4  5  6
7  8  9

Transpose of the matrix:
1  4  7
2  5  8
3  6  9

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .