C Program to implement sparse matrix using array;

 #include<stdio.h> 

  

int main() 

    

    int sparseMatrix[100][100],rows,columns; 

    printf("Enter number of rows and columns of sparse Matrix:\n");

    scanf("%d%d",&rows,&columns);

    printf("Enter the elemnts of a spare Matrix:\n ");

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

    {

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

        {

           printf("Element[%d][%d]:",i,j);

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

        }

    }    

    

    

  

    int size = 0; 

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

    {

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

        {

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

            {

                size++; 

            }

        }

    }    

 

    int resultant_matrix[size][3]; 

  

    

    int k = 0; 

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

    {

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

        {

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

            { 

                resultant_matrix[k][0] = i; 

                resultant_matrix[k][1] = j; 

                resultant_matrix[k][2] = sparseMatrix[i][j]; 

                k++; 

            } 

        }

    }

  

  printf("---------------Original sparse Matrix:----------------\n");

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

    {

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

        {

           printf(" %d |",sparseMatrix[i][j]);

        }

        printf("\n");

    }    

    

    printf("-------------------Result:--------------\n ");

 printf(" Row  |  Column  | Element   |\n\n");  

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

    { 

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

        {

            printf("   %d     ", resultant_matrix[i][j]); 

        }

  

        printf("\n"); 

    } 

    

    printf("\n\n --------------Code Written by-'VIKAS KUMAR'---------------------\n\n");

    return 0; 

OUTPUT:



Comments

Popular posts from this blog

[ C++ program] Assume that a bank maintains two kinds of accounts for customers, | | one called as savings account and the other as current account. | | The savings account provides compound interest and withdrawal facilities but no cheque book facility. | | The current account provides cheque book facility but no interest. | | Current account holders should also maintain a minimum balance | | and if the balance falls below this level, a service charge is imposed. | | Create a class account that stores customer name, account number and type of account. | | From this derive the classes curacct and savacct to make them more specific to their requirements.

Perform all operation in a singly linked list using C language:

Write a program in C++ to create a base class shape and derive two classes named Circle and Square from it. Create and initialize objects from these classes using appropriate constructors.