Tuesday, 22 September 2026

to Check Whether a Matrix is Sparse or Not

 #include <iostream>

using namespace std;


int main()

{

    int matrix[3][3];

    int zero = 0;

    int total = 9;


    cout << "Enter 3 x 3 matrix:" << endl;


    // Input matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix[i][j];


            if (matrix[i][j] == 0)

            {

                zero++;

            }

        }

    }


    // Check sparse matrix

    if (zero > total / 2)

    {

        cout << "Matrix is a Sparse Matrix.";

    }

    else

    {

        cout << "Matrix is not a Sparse Matrix.";

    }


    return 0;

}




Write a Program to Addition of Two Sparse Matrices

 #include <iostream>

using namespace std;


int main()

{

    int matrix1[3][3];      int matrix2[3][3];


    int sparse1[10][3];      int sparse2[10][3];      int result[10][3];


    int k1 = 0;     int k2 = 0;      int k3 = 0;

    // Input Matrix 1

    cout << "Enter first 3 x 3 matrix:" << endl;

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix1[i][j];

        }

    }

    // Input Matrix 2

    cout << "Enter second 3 x 3 matrix:" << endl;

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix2[i][j];

        }

    }








    // Convert Matrix 1 into Sparse Matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            if (matrix1[i][j] != 0)

            {

                sparse1[k1][0] = i;          // Row

                sparse1[k1][1] = j;          // Column

                sparse1[k1][2] = matrix1[i][j]; // Value

                k1++;

            }

        }

    }

    // Convert Matrix 2 into Sparse Matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            if (matrix2[i][j] != 0)

            {

                sparse2[k2][0] = i;          // Row

                sparse2[k2][1] = j;          // Column

                sparse2[k2][2] = matrix2[i][j]; // Value

                k2++;

            }

        }

    }







    // Add two sparse matrices

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            int value = matrix1[i][j] + matrix2[i][j];

            if (value != 0)

            {

                result[k3][0] = i;       // Row

                result[k3][1] = j;       // Column

                result[k3][2] = value;   // Value

                k3++;

            }

        }

    }

    // Display Sparse Matrix 1

    cout << "\nSparse Matrix 1:" << endl;

    cout << "Row\tColumn\tValue" << endl;

    for (int i = 0; i < k1; i++)

    {

        cout << sparse1[i][0] << "\t"

             << sparse1[i][1] << "\t"

             << sparse1[i][2] << endl;

    }


    // Display Sparse Matrix 2

    cout << "\nSparse Matrix 2:" << endl;

    cout << "Row\tColumn\tValue" << endl;


    for (int i = 0; i < k2; i++)

    {

        cout << sparse2[i][0] << "\t"

             << sparse2[i][1] << "\t"

             << sparse2[i][2] << endl;

    }


    // Display Result

    cout << "\nAddition of Two Sparse Matrices:" << endl;

    cout << "Row\tColumn\tValue" << endl;

    for (int i = 0; i < k3; i++)

    {

        cout << result[i][0] << "\t"

             << result[i][1] << "\t"

             << result[i][2] << endl;

    }


    return 0;

}



SPARSE MATRIX -Cpp

 #include <iostream>

int main()

{    int matrix[3][3];     int sparse[10][3];      int k = 0;

    cout << "Enter 3 x 3 matrix:" << endl;

 # 3 rows  ,3 columns , Total = 9 elements

    // Input matrix

    for (int i = 0; i < 3; i++)

    {

        for (int j = 0; j < 3; j++)

        {

            cin >> matrix[i][j];

        }

    }










    // Find non-zero elements

    for (int i = 0; i < 3; i++)

    {

#i represents the row number. 

        for (int j = 0; j < 3; j++)

     # j represents the column number. 

        {

            if (matrix[i][j] != 0) # If the matrix element is not zero, store it. 

            {

              #k keeps track of the next position to store a non-zero element.

                sparse[k][0] = i; #sparse[k][0] → Row

                sparse[k][1] = j; #sparse[k][1] → Column

                sparse[k][2] = matrix[i][j]; #sparse[k][2] → Value

                k++;

            }

        }

    }               

    // Display sparse matrix

    cout << "\nSparse Matrix:" << endl;

    cout << "Row\t Column\t Value" << endl;

    for (int i = 0; i < k; i++)

    {        cout << sparse[i][0] << "\t"

             << sparse[i][1] << "\t"

             << sparse[i][2] << endl;

    }

    return 0;

}


Monday, 17 November 2025

DS- CPP-Practical

1.Write a program to Calculate the Area of a Circle using Function.

2.Write a program to find the largest of 3 numbers using a function.

3.Write a program to compute the factorial of a number using recursion.

4.Write an Program to find and print sum and average of array elements 

5.Write a Program to implement addition of two polynomials using 1 dimensional array.

6.Write a Program to implement Subtraction of two polynomials using 1 dimensional array.

7.Write a Program to implement Multiplication of two polynomials using 1 dimensional array.

8.Write a Program to implement addition of two polynomials using a 2 dimensional array.

9.Write a Program to Transpose Matrix

10.Write a Program to Sparse Matrix

11.Write a Program to  Addition of Two Sparse Matrices 

12.Write a Program to Check Whether a Matrix is Sparse or Not

 

 

 

Wednesday, 17 September 2025

SPARSE MATRIX

  • A matrix is just a table of numbers (rows × columns).

  • A sparse matrix is a matrix where most of the elements are 0.

  • Instead of storing all elements (including 0s), we store only the non-zero elements with their row, column, and value.


Matrix (4 × 4):  

         0 1   2  3

   0    0  0  3  0

   1    0  0  0  0

   2   0  4  0  5

   3   7  0  0  0

 most values are 0.

Only these are non-zero:

   Value at(Row,Col)

  • 3 at (0,2)

  • 4 at (2,1)

  • 5 at (2,3)

  • 7 at (3,0)

   

        0           1      2

        Row   Col   Value

   0      0          2     3

    1     2          1     4

    2     2          3     5

    3     3           0     7



[

 At(Row,column)                                                                                   Value

At (0,2) → compact[0][0]=0, compact[1][0]=2, compact[2][0]=3]




#include <stdio.h>


int main() {

    int rows = 4, cols = 4;

    

    // Original sparse matrix

    int sparse[4][4] = {

        {0, 0, 3, 0},

        {0, 0, 0, 0},

        {0, 4, 0, 5},

        {7, 0, 0, 0}

    };


// Count non-zero elements

    int size = 0; //size is used to count how many non-zero elements

    for (int i = 0; i < rows; i++)

        for (int j = 0; j < cols; j++)

            if (sparse[i][j] != 0)

                size++;


   [    //Value at(Row,Col)

  • 3 at (0,2)                value = 3 → not zero → size = 1

  • 4 at (2,1)                value = 4 → not zero → size = 2

  • 5 at (2,3)                value = 5 → not zero → size = 3

  • 7 at (3,0)                value = 7 → not zero → size = 4 ]


 


 // Compact matrix (3 × size)

    int compact[3][size];// 3-> row,column,value

    int k = 0; // k keeps track of which column we are filling in the compact matrix


for (int i = 0; i < rows; i++) {

    for (int j = 0; j < cols; j++) {

        if (sparse[i][j] != 0) {

            compact[0][k] = i;             //  store Row index

            compact[1][k] = j;             // store Column index

            compact[2][k] = sparse[i][j];  // store  Value

            k++; //k++ → move to next column

        }

    }

}

[

 At(Row,column)                                                                                   Value

At (0,2) → compact[0][0]=0, compact[1][0]=2, compact[2][0]=3]


 // Print compact matrix

 printf("Row  Col  Value");

 for (int i = 0; i < size; i++) 

{

printf("%d    %d    %d\n", compact[0][i], compact[1][i], compact[2][i]);

    }

    return 0;

}