## Sparse Matrix representation for polynomials

Sparse Matrix representation for polynomials Program to represent two polynomials using Sparse Matrix representation using arrays, compute sum of two polynomials and represent the result using sparse matrix: /* Sparse Matrix Representation for Polynomials */ #include <stdio.h> #include <stdlib.h> #define MAX 15 int main(){ int poly1[MAX][MAX]={0},poly2[MAX][MAX]={0},poly3[MAX][MAX]={0}; int i,j,k,m,n,nrows,nrows1,nrows2,nrows3,ncols,ct=0,ct1=0,ct2=0; int spmat1[MAX][MAX]={0},spmat2[MAX][MAX]={0},spmat3[MAX][MAX]={0}; int deg,deg1,deg2,deg3; printf(“nEnter degree of first polynomial?”); scanf(“%d”,°1); printf(“nEnter degree of second polynomial?”); scanf(“%d”,°2); deg3=(deg1>deg2)?deg1:deg2; printf(“nFor first polynomial:”); for(i=0;i<=deg1;i++){ printf(“nEnter…

Read more...## Program to represent two polynomials using arrays and compute their sum

are closed

Sparse Polynomial representation and addition Program to represent two polynomials using arrays and compute their sum /* Representation of Polynomials using arrays */ /* Addition of two Polynomials */ #include <stdio.h> #define MAX 10 int main(){ int poly1[MAX]={0},poly2[MAX]={0},poly3[MAX]={0}; int i,deg1,deg2,deg3; printf(“nEnter degree of first polynomial?”); scanf(“%d”,°1); printf(“nEnter degree of second polynomial?”); scanf(“%d”,°2); printf(“nFor first polynomial:”); for(i=0;i<deg1;i++){ printf(“nEnter Coefficient for Exponent %d> “,i); scanf(“%d”,&poly1[i]); } printf(“nFor second polynomial:”); for(i=0;i<deg2;i++){ printf(“nEnter Coefficient…

Read more...## Sparse Polynomial representation and addition

are closed

Sparse Polynomial representation and addition: Polynomial is an expression which is composed of terms, wherein terms are composed of coefficient and exponent. An example of a polynomial is: 4×3+5×2+6x+9. This polynomial is composed of four terms with the following sets of coefficient and exponent – {(4,3), (5,2), (6,1), (9,0)}. Thus representation of a polynomial using arrays is straightforward. The subscripts of the array may be considered as exponents and the…

Read more...## Program to represent Sparse Matrix using singly linked list (One dimensional list)

are closed

Sparse Matrix representation using singly Linked List: /* Sparse Matrix representation using linked list */ #include <stdio.h> #include <stdlib.h> typedef struct list{ int rows, cols, value; struct list *next; }list; list *create(){ list *temp = (list *)malloc(sizeof(list)); if(temp==NULL){ printf(“nMemory Allocation Error !”); exit(1); } return temp; } list *makenode(int r, int c, int val){ list *temp = create(); temp->rows = r; temp->cols = c; temp->value = val; temp->next = NULL;…

Read more...## Program to represent Sparse Matrix using arrays

are closed

Program to represent Sparse Matrix using arrays: /* Sparse Matrix representation using arrays */ #include <stdio.h> #include <stdlib.h> #define MAX 15 int main(){ int arr[3][4],i,j,m,n,nrows,ncols,ct=0; int sparse_matrix[MAX][MAX]; for(i=0;i<3;i++){ printf(“nEnter values for row %d?”,i+1); for(j=0;j<4;j++){ scanf(“%d”,&arr[i][j]); if(arr[i][j] != 0) ct++; } } nrows = ct+1; ncols = 3; sparse_matrix[0][0] = 3; sparse_matrix[0][1] = 4; sparse_matrix[0][2] = ct; m=1; n=0; for(i=0;i<3;i++){ for(j=0;j<4;j++){ if(arr[i][j]!=0){ sparse_matrix[m][n++]=i; sparse_matrix[m][n++]=j; sparse_matrix[m][n]=arr[i][j]; m++; n=0; } } } printf(“nThe…

Read more...## Sparse Matrix

are closed

Sparse Matrix: A matrix that has minimum number of non-zero elements is called a sparse matrix i.e. very few elements are sparsely distributed in the matrix. Example: An array of order 3 x 4 containing sparsely located non-zero elements. 0 1 2 3 0 0 25 0 7 1 0 0 2 8 2 0 0 1 0 (Order 3 x 4) In the above example, out of 12 elements only…

Read more...## Array Representation – Column-major & Row-major

are closed

Array Representation: Column-major Row-major Arrays may be represented in Row-major form or Column-major form. In Row-major form, all the elements of the first row are printed, then the elements of the second row and so on upto the last row. In Column-major form, all the elements of the first column are printed, then the elements of the second column and so on upto the last column. The ‘C’ program to…

Read more...## Program to delete a value from a given array such that array remains sorted

are closed

Sorted Deletion in an Array: Deletion is same as in previous post with the only difference that if the given array is sorted, then the deletion automatically will result in a sorted array. /* Sorted Deletion */ #include <stdio.h> #define MAX 10 int main(){ int arr[MAX],n,i,num; printf(“nEnter total numbers?”); scanf(“%d”,&n); for(i=0;i<n;i++){ printf(“nEnter number?”); scanf(“%d”,&arr[i]); } printf(“nEnter the number to delete?”); scanf(“%d”,&num); for(i=0;i<n;i++){ if(arr[i] == num) break; } if(i == n){…

Read more...## Program to delete a value from a given array from a given position

are closed

Deletion from a position in an array: Deletion of a value is straightforward, since the elements from the given position are overwritten with the subsequent elements upto the last element, last element is initialized with zero and variable n indicating total number of elements is decremented by one. /* Deletion from a position */ #include <stdio.h> #define MAX 10 int main(){ int arr[MAX],i,n,pos; printf(“nEnter total numbers?”); scanf(“%d”,&n); for(i=0;i<n;i++){ printf(“nEnter number?”);…

Read more...## Program to insert a value in an array such that array remains sorted

are closed

Sorted insertion in an Array: Whenever a new value is inserted, appropriate place is located to insert this value. After locating that place, shifting of elements is required from that position to the last element by one position to the right. Then the located position may be assigned the new element to be inserted. The ‘C’ program for the same is given below: /* Program for sorted insertion */ #include…

Read more...
are closed