Monday, 17 November 2025

DS-Practical

 

Write a Program to implement addition of two polynomials using 1 dimensional array.
Write a Program to implement Subtraction of two polynomials using 1 dimensional array.
Write a Program to implement Multiplication of two polynomials using 1 dimensional array.
Write a Program to implement addition of two polynomials using a 2 dimensional array.
Write a Program to Transpose Matrix
Write a Program to Sparse Matrix
Write a Program for Implementation of stack.
Write a Program to Checking of balanced parenthesis using stack
Write a Program to reverse a string using stack
Write a Program to implement the queue and Display no.of elements.
Write a Program of Circular Queue.
Write a Program Implementation of a BST ,Add,Search ,Min,Max
Write a Program Implementation of a BST  create insert ,countleaf ,countnodes,depth
Write a Program PreOrder, PostOrder, InOrder Traversal  
Write a program to insertion Sort
Write a Program to Bubble Sort .
Write a program to Selection Sort
Write a Program Linear Search
Write a Program Binary Search
Write a Program Linear search on sorted data
Write a Program Linear search on unsorted data

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;

}








Tuesday, 9 September 2025

Print addition symbol in polynomials


#include <stdio.h>

int main() {

    int n; // Degree of the polynomial + 1

    printf("Enter the number of terms in the polynomial: ");

    scanf("%d", &n);


    int poly1[100]; // Array for polynomial coefficients


    // Input for the polynomial

    printf("Enter the coefficients of the polynomial:\n");

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

        printf("Coefficient of x^%d: ", i);

        scanf("%d", &poly1[i]);

    }


    // Print polynomial

    printf("\nPolynomial is: ");

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

        printf("%d", poly1[i]);

        if (i != 0) {

            printf("x^%d", i); // Display power of x

        }

        if (i != n - 1) {

            printf(" + "); // Display '+' between terms in result

        }

    }   

    return 0;

}


Accept Coefficient value

 

#include<stdio.h>

int main() {

    int n; // Degree of the polynomial + 1

    printf("Enter the number of terms in the polynomials: ");

    scanf("%d", &n);

 

    int poly1[100]; // Arrays for the two polynomials and the result

 

    // Input for the first polynomial

    printf("Enter the coefficients of the first polynomial:\n");

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

        printf("Coefficient of x^%d: ", i);

        scanf("%d", &poly1[i]);

    }

  

    return 0;

}

 

Accept Array and display array

#include <stdio.h>

 

int main() {

    int n, i;

 

    printf("Enter size of array: ");

    scanf("%d", &n);   // accept size

 

    int arr[n];   // declare array of size n

 

    // Input elements

    printf("Enter %d elements:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Output elements

    printf("Array elements are:\n");

    for(i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

 

    return 0;

}