Monday, 17 November 2025
DS-Practical
Write a Program to implement addition of two polynomials using 1 dimensional array.
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)
|
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;
}




