You are on page 1of 8

1

Practice 05 - Two-dimensional Arrays / Easy

C Program to read and print an RxC Matrix, R and C must be input by the User.

Input format:
The first line contains two integers r and c, representing the no.of rows and columns in the
matrix respectively.
The next r lines contain c space-separated integers in each line.

Output format:
Print the matrix.

#include<stdio.h>
void main(){
int a[10][10],i,j,r,c;
scanf("%d %d",&r,&c);
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
}
2

C Program to print non-diagonal elements of the given square matrix.

Input format:
The first line contains an integer n, which represents the order of the matrix.
The next n lines contain n space-separated integers in each line.

Output format:
Print the non-diagonal elements of the matrix.
#include<stdio.h>
void main() {
int a[10][10],i,j,r,c;
scanf("%d",&c);
r=c;
for(i=0;i<r;i++){
for(j=0;j<c;j++) {
scanf("%d",&a[j][i]);
}
}
for(i=0;i<c;i++){
for(j=0;j<r;j++) {
if(i!=j)
{
printf("%d ",a[i][j]);
}
}

}
3

Write a program to sort the elements in ascending order of each row of a matrix.

Input format:
The first line contains two integers m and n, representing the no.of rows and columns in the
matrix respectively.
The next m lines contain n space-separated integers in each line.

Output format:
Print the sorted matrix.
#include<stdio.h>
void main() {
int a[10][10], i,j,r,c,k,temp;
scanf("%d %d",&r,&c);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<r;j++){
for(k=0;k<c-1;k++) {
if(a[j][k]>a[j][k+1])
{
temp=a[j][k];
a[j][k]=a[j][k+1];
a[j][k+1]=temp;
}

}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
}
4

Write a program to find the transpose of a given matrix.

Input format:
The first line contains two integers m and n, representing the no.of rows and columns in the matrix respectively.
The next m lines contain n space-separated integers in each line.

Output format;
Print transpose matrix of a given matrix.
#include<stdio.h>
void main() {
int i,j,r,c;
scanf("%d %d",&r,&c);
int a[r][c], b[r][c];
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
b[j][i]=a[i][j];
}
}
for(j=0;j<c;j++){
for(i=0;i<r;i++){
printf("%d ",b[j][i]);
}
printf("\n");
}
}
5

Print the upper triangular matrix of a square matrix.

Input format:
The first line contains an integer n, which represents the order of the matrix.
The following n lines contain n space-separated integers in each line.

Output format:
Print the upper triangular matrix of the given square matrix.
#include<stdio.h>
void main() {
int a[20][20], r,i,j;
scanf("%d",&r);
for(i=0;i<r;i++) {
for(j=0;j<r;j++) {
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<r;j++){
if(i<=j)
{
printf("%d ",a[i][j]);
}
else
{
printf("%d ",0);
}

}
printf("\n");
}
}
6

C Program to print diagonal elements of the given square matrix.

Input format:
The first line contains an integer n, which represents the order of the matrix.
The next n lines contain n space-separated integers in each line.

Output format:
Print the diagonal elements of the matrix.
#include<stdio.h>
void main() {
int r,c,i,j;
scanf("%d",&r);
c=r;
int a[r][c];
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(i==j)
printf("%d ",a[i][j]);
}
}

}
7

Given a boolean 2D array, where each row is sorted.  Write a program to find the row with the
maximum number of 1's.

Input format:
The first line contains two integers m and n, representing the no.of rows and columns in the
matrix respectively.
The next m lines contain n space-separated integers in each line.

Output format;
Print the row number which has the maximum number of 1's.

Note: Row number starts with '0'.


#include <stdio.h>
void main() {
int a[20][20],r,c,i,j, maxcount=0,index=0;
scanf("%d %d",&r,&c);
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++) {
int count=0;
for(j=0;j<c;j++) {
if(a[i][j]==1)
count++;
}
if(count>maxcount)
{
maxcount=count;
index=i;

}
printf("%d",index);

}
8

Write a program in C to sort the matrix row-wise and column-wise.

Input format:
The first line contains two integers m and n, representing the no.of rows and columns in the matrix respectively.
The next m lines contain n space-separated integers in each line.

Output format;
Print the sorted matrix.

#include<stdio.h>
void main() {
int a[10][10],r,c,i,j,k,temp;
scanf("%d%d",&r,&c);
for(i=0;i<r;i++){
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
for(j=0;j<r;j++) {
for(k=0;k<c-1;k++){
if(a[j][k]>a[j][k+1])
{
temp=a[j][k];
a[j][k]=a[j][k+1];
a[j][k+1]=temp;
}
}

for(i=0;i<c;i++)
for(k=0;k<c;k++) {
for(j=0;j<r-1;j++){
if(a[j][k]>a[j+1][k])
{
temp=a[j][k];
a[j][k]=a[j+1][k];
a[j+1][k]=temp;
}
}
}

for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d ",a[i][j]);
} printf("\n");
}

You might also like