Problem Statement:- Program to read and display a Matrix Or 2D Array.
Sample Input/Output:-
Data requirement:-
Input Data:- row_size, col_size, matrix[][]
Output Data:-matrix[][]
Additional Data:- i, j
Program in C
Here is the source code of the C Program to read and display a 2*2 Matrix Or 2D Array.
Code:
#include<stdio.h>
int main()
{
int row_size,col_size;
//Get size of matrix
printf("Enter the row Size Of the Matrix:");
scanf("%d",&row_size);
printf("Enter the columns Size Of the Matrix:");
scanf("%d",&col_size);
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
printf("Enter the Matrix Element:\n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
scanf("%d",&matrix[i][j]);
}
}
//display the Matrix
printf("Given Matrix is:\n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
printf("%d ",matrix[i][j]);
}
printf("\n");
}
}
Input/Output:
Enter the row Size Of the Matrix:2
Enter the columns Size Of the Matrix:2
Enter the Matrix Element:
1 2
3 4
Given Matrix is:
1 2
3 4
Program in C++
Here is the source code of the C++ Program to read and display a 3*3 Matrix Or 2D Array.
Code:
#include<iostream>
using namespace std;
int main()
{
int row_size,col_size;
//Get size of matrix
cout<<"Enter the row Size Of the Matrix:";
cin>>row_size;
cout<<"Enter the columns Size Of the Matrix:";
cin>>col_size;
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
cout<<"Enter the Matrix Element:\n";
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
cin>>matrix[i][j];
}
}
//display the Matrix
cout<<"Given Matrix is:\n";
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<"\n";
}
}
Input/Output:
Enter the Matrix Element:
5 6 7
8 9 1
2 3 4
Given Matrix is:
5 6 7
8 9 1
2 3 4
Program in Java
Here is the source code of the Java Program to read and display a 5*5 Matrix Or 2D Array.
Code:
import java.util.Scanner;
public class DisplayMatrix {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int row_size,col_size;
//Get size of matrix
System.out.print("Enter the row Size Of the Matrix:");
row_size=cs.nextInt();
System.out.print("Enter the columns Size Of the Matrix:");
col_size=cs.nextInt();
int matrix[][] =new int[row_size][col_size];
//Taking input of the matrix
int i,j;
System.out.println("Enter the Matrix Element:");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
matrix[i][j]=cs.nextInt();
}
}
//display the Matrix
System.out.println("Given Matrix is:");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
cs.close();
}
}
Input/Output:
Enter the row Size Of the Matrix:5
Enter the columns Size Of the Matrix:5
Enter the Matrix Element:
11 12 13 14 15
31 22 33 32 42
55 62 20 75 77
10 11 13 14 15
16 17 18 19 21
Given Matrix is:
11 12 13 14 15
31 22 33 32 42
55 62 20 75 77
10 11 13 14 15
16 17 18 19 21
Program in Python
Here is the source code of the Python Program read and display a Matrix Or 2D Array.
Code:
# Get size of matrix
row_size=int(input("Enter the row Size Of the Matrix:"))
col_size=int(input("Enter the columns Size Of the Matrix:"))
matrix=[]
# Taking input of the matrix
print("Enter the Matrix Element:")
for i in range(row_size):
matrix.append([int(j) for j in input().split()])
# display the Matrix
print("Given Matrix is:")
for m in matrix:
print(m)
Input/Output:
Enter the row Size Of the Matrix:2
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
4 5 6
7 8 9
Given Matrix is:
[4, 5, 6]
[7, 8, 9]
Most Recommend Questions:-
More Questions:-
Array Programming Questions and Solutions(50)
0 Comments
Please do not Enter any spam link in the comment box