Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Information
Technology
Department of
Artificial Intelligence and Data Science
Student Name: Aditya Sirsat
Class: F.Y. B. Tech Division: B (B2) Roll No.: 107235
Semester: 2nd Semester Academic Year: 2023-24
Subject Name & Code: IDSA
Assignment No: 3
Data Collected based on different parameters for analysis represented in
matrix given below.
00200
00005
03000
70000
Program to represent this sparse matrix using 2D Array and perform
transpose and fast transpose on it.
Date of Performance: Date of Submission:
Aim: Data collected based on different parameters for analysis represented in
matrix given below.
Objective: program to represent the sparse matrix using 2D array and perform
transpose and fast transpose on it.
Materials Required:
Computer
IDE that is supported
Visual Studio Code
Procedure:
CODE:
#include<iostream>
using namespace std;
int main(){
int k, l;
int count = 0, m = 0;
cout<< "Enter the nummber of rows :"<<endl;
cin >> k ;
cout<< "Enter the number of columns :"<<endl;
cin >> l ;
int a[k][l];
for (int i=0;i<k;i++){
for (int j=0;j<l;j++){
cout << "Enter the number at "<<i <<" , "<<j << "in the matrix :"<<endl;
cin >> a[i][j];
}
}
cout<<endl;
cout<<"The matrix entered is: "<<endl;
for (int i=0;i<k;i++) {
for(int j=0;j<l;j++){
cout<< a[i][j]<<"\t";
if (j==l-1){
cout<<endl;
}
}
}
cout<<endl;
cout<< "The transpose matrix is :"<<endl;
for (int i=0;i<l;i++) {
for(int j=0;j<k;j++){
cout<< a[j][i]<<"\t";
if (j==k-1){
cout<<endl;
}
}
}
//firdt count the non-zero elements in the user defined matrix.
for(int i = 0; i<k; i++){
for(int j = 0; j<l;j++){
if( a[i][j] != 0 ){
count++;}
}
}
cout<<endl;
cout<<"The Number of non-zero elements is: "<< count<< endl;
int b[count][3];
for (int i=0;i<k;i++) {
for(int j=0;j<l;j++){
if (a[i][j]!=0){
b[m][0]=i;
b[m][1]=j;
b[m][2]=a[i][j];
m++;}
}
}
cout<<endl;
cout<< "The sparse matrix is :" << endl;
for(int i = 0; i<count; i++){
for(int j = 0; j<3; j++){
cout<<b[i][j]<<"\t";
if(j==2){
cout<<endl; }
}}
return 0;}
OUTPUT:
Conclusion: In this assignment, we have learned to work with sparse matrices and
represent them on 2D matrix, saving memory and performance. We also learned
to transpose the given matrix increasing our skills in transposing matrix.