Dsa Important
Dsa Important
Transpose of matrix
a. #include <iostream>
using namespace std;
int main(){
int row,column;
cout<<"Enter row count : ";
cin>>row;
cout<<"Enter column count : ";
cin>>column;
int arr[row][column];
int transpose[row][column];
cout<<"Enter element for array\n\n";
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout<<"Element no "<<i<<j<<": ";
cin>>arr[i][j];
}
}
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
transpose[j][i] = arr[i][j];
}
}
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout<<transpose[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
2. Sparse matrix
What is a sparse matrix? A sparse matrix is a special case of
a matrix in which the number of zero elements is much
higher than the number of non-zero elements. As a rule of
thumb, if 2/3 of the total elements in a matrix are zeros, it can
be called a sparse matrix.
#include <iostream>
int main()
{
int row, column;
cout << "Enter row count : ";
cin >> row;
cout << "Enter column count : ";
cin >> column;
int arr[row][column];
cout << "Enter element for array\n\n";
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
cout << "Element no " << i << j << ": ";
cin >> arr[i][j];
}
}
int zero_count = 0;
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < column; j++)
{
if (arr[i][j] == 0)
{
zero_count++;
}
}
}
if (zero_count > row * column)
{
cout<<"This is sparse matrix";
}
else {
cout<<"This is not sparse matrix";
}
}
3. Data structure as linear and nor-linear
Its examples are: array, stack, While its examples are: trees and
6. queue, linked list, etc. graphs.