You are on page 1of 44

PROGRAM 1:

Program to find roots of a quadratic equation:


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, d, r1, r2, realPart,i;
clrscr();
printf("Enter coefficient a");
scanf("%f",&a);
printf("Enter coefficient b");
scanf("%f",&b);
printf("Enter coefficient c");
scanf("%f",&c);
d = b*b-4*a*c;
if (d > 0)
{
r1 = (-b+sqrt(d))/(2*a);
r2 = (-b-sqrt(d))/(2*a);
printf("root1 = %f and root2 = %f",r1 , r2);
}
else if (d == 0)
{
r1 = r2 = -b/(2*a);
printf("root1 = root2 = %f ", r1);
}
else
{
realPart = -b/(2*a);
i = sqrt(-d)/(2*a);
printf("root1 = %.f + %f i and root2 = %f - %fi", realPart, i, realPart, i);
}
getch();
}

PROGRAM 2:
Program to determine the quadrant using
coordinate points:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\nEnter the point on X axis:");
scanf("%d",&x);
printf("\nEnter the point on Y axis:");
scanf("%d",&y);
if(x>0 && y>0)
printf("The coordinate points lies on 'First Quadrant'");
else if(x<0 && y>0)
printf("The coordinate points lies on 'Second Quadrant'");
else if(x<0 && y<0)
printf("The coordinate points lies on 'Third Quadrant'");
else if(x>0 && y<0)
printf("The coordinate points lies on 'Fourth Quadrant'");
else
printf("The coorinate points lies on 'origin'");
getch();
}

PROGRAM 3:
Program to determine the maximum number
from an array:
#include <stdio.h>
#include<conio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
printf("\n");
for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
for(i = 1; i < n; ++i)
{
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
getch();
}

PROGRAM 4 (i):
Linear search program
#include <stdio.h>
#include <conio.h>
void main()
{
int array[50], search,i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (i = 0; i < n; i++)
{
if (array[i] == search)
{
printf("%d is present at location %d.\n", search, i+1);
break;
}
}
if (i == n)
printf("%d isn't present in the array.\n", search);
getch();
}

PROGRAM 4 (ii):
Binary search program
#include <stdio.h>
#include<conio.h>
void main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
getch();
PROGRAM 5:
Program to insert an element at specific
position in an array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,pos,num;
clrscr();
printf("\nEnter the size of array:");
scanf("%d",&n);
printf("\nEnter the Array:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the position for insertion:");
scanf("%d",&pos);
printf("\nEnter the value to be inserted:");
scanf("%d",&num);
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;

printf("\nThe new array:");


for(i=1;i<=n+1;i++)
{
printf("\n%d",a[i]);
}
getch();
}

PROGRAM 6:
Program to delete an element from a specific
postion:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,pos,num;
clrscr();
printf("\nEnter the size of array:");
scanf("%d",&n);
printf("\nEnter the Array:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the position number for deletion:");
scanf("%d",&pos);
for(i=pos;i<=n;i++)
{
a[i]=a[i+1];
}
printf("\nThe new array:");
for(i=1;i<=n-1;i++)
{
printf("\n%d",a[i]);
}
getch();
}

PROGRAM 7:
Program to add two multidimmensional
arrays:
#include <stdio.h>
#include <conio.h>
void main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{ sum[i][j]=a[i][j]+b[i][j];
}
printf("\nSum of two matrices: \n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
getch();
}

PROGRAM 8 (i):
Program for selection sort:
#include <stdio.h>
#include <conio.h>
void main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
}
PROGRAM 8 (ii):
Program for insertion sort:

#include <stdio.h>
#include <conio.h>
void main()
{
int n, array[1000], c, d, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
t = array[c];

for (d = c - 1 ; d >= 0; d--) {


if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
getch();
}

PROGRAM 8 (iii):
Program for bubble sort:

#include <stdio.h>
#include<conio.h>
void main()
{ int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();}
PROGRAM 10 :
Program to make sparse matrix:
#include <stdio.h>
#include<conio.h>
#define MAX 20

void read_matrix(int a[10][10], int row, int column);


void print_sparse(int b[MAX][3]);
void create_sparse(int a[10][10], int row, int column, int b[MAX][3]);

int main()
{ int a[10][10], b[MAX][3], row, column;
printf("\nEnter the size of matrix (rows, columns): ");
scanf("%d", &row);
scanf("%d", &column);

read_matrix(a, row, column);


create_sparse(a, row, column, b);
print_sparse(b);
getch();
}

void read_matrix(int a[10][10], int row, int column)


{
int i, j;
printf("\nEnter elements of matrix\n");
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
}

void create_sparse(int a[10][10], int row, int column, int b[MAX][3])


{
int i, j, k;
k = 1;
b[0][0] = row;
b[0][1] = column;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
if (a[i][j] != 0)
{
b[k][0] = i;
b[k][1] = j;
b[k][2] = a[i][j];
k++;
}
}
b[0][2] = k - 1;
}
}
void print_sparse(int b[MAX][3])
{
int i, column;
column = b[0][2];
printf("\nSparse form - list of 3 triples\n\n");
for (i = 0; i <= column; i++)
{
printf("%d\t%d\t%d\n", b[i][0], b[i][1], b[i][2]);
}
}

PROGRAM 9
Program to show a polynomial representation
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, power;
float x;
clrscr();
printf("Enter the order of the polynomial \n");
scanf("%d", &num);
printf("Enter the value of x \n");
scanf("%f", &x);
/* Read the coefficients into an array */
printf("Enter %d coefficients \n", num + 1);
for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
power = num;
printf("Given polynomial is: \n");
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
/* printing proper polynomial function */
if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
getch();
}

PROGRAM 10
Program to insert a circular queue
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear =-1;
int isFull()
{
if( (front == rear + 1) || (front == 0 && rear == SIZE-1)) return 1;
return 0;
}
int isEmpty()
{
if(front == -1) return 1;
return 0;
}
void enQueue(int element)
{
if(isFull()) printf("\n Queue is full!! \n");
else
{
if(front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
printf("\n Inserted -> %d", element);
}
}
int deQueue()
{
int element;
if(isEmpty()) {
printf("\n Queue is empty !! \n");
return(-1);
} else {
element = items[front];
if (front == rear){
front = -1;
rear = -1;
} /* Q has only one element, so we reset the queue after dequeing it. ? */
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return(element);
}
}
void display()
{
int i;
if(isEmpty()) printf(" \n Empty Queue\n");
else
{
printf("\n Front -> %d ",front);
printf("\n Items -> ");
for( i = front; i!=rear; i=(i+1)%SIZE) {
printf("%d ",items[i]);
}
printf("%d ",items[i]);
printf("\n Rear -> %d \n",rear);
}
}
int main()
{
// Fails because front = -1
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
// Fails to enqueue because front == 0 && rear == SIZE - 1
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
// Fails to enqueue because front == rear + 1
enQueue(8);
return 0;
}

PROGRAM 11
Program to insert elements in dequeue
# include<stdio.h>
# define MAX 5
int deque_arr[MAX];
int left = -1;
int right = -1;
/*Begin of insert_right*/
void insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow\n");
return;}
if (left == -1) /* if queue is initially empty */
{ left = 0;
right = 0;}
else
if(right == MAX-1) /*right is at last position of queue */
right = 0;
else
right = right+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item ;
}
/*End of insert_right*/

/*Begin of insert_left*/
void insert_left()
{ int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{ printf("Queue Overflow \n");
return; }
if (left == -1)/*If queue is initially empty*/
{ left = 0;
right = 0; }
else
if(left== 0)
left=MAX-1;
else
left=left-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item ; }
/*End of insert_left*/

/*Begin of delete_left*/
void delete_left()
{ if (left == -1)
{ printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{ left = -1;
right=-1; }
else
if(left == MAX-1)
left = 0;
else
left = left+1;
}
/*End of delete_left*/
/*Begin of delete_right*/
void delete_right()
{if (left == -1)
{printf("Queue Underflow\n");
return ; }
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{ left = -1;
right=-1; }
else
if(right == 0)
right=MAX-1;
else
right=right-1; }
/*End of delete_right*/
/*Begin of input_que*/
void display_queue()
{ int front_pos = left,rear_pos = right;
if(left == -1)
{ printf("Queue is empty\n");
return; }
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{ while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]);
front_pos++; } }
else
{ while(front_pos <= MAX-1)
{ printf("%d ",deque_arr[front_pos]);
front_pos++; }
front_pos = 0;
while(front_pos <= rear_pos)
{ printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
/*End of display_queue*/
/*Begin of input_que*/
void input_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{ case 1:
insert_right();
break;
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of input_que*/

/*Begin of output_que*/
void output_que()
{ int choice;
do
{ printf("1.Insert at right\n");
printf("2.Insert at left\n");
printf("3.Delete from left\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display_queue();
break;
case 5:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=5);
}
/*End of output_que*/

/*Begin of main*/
main()
{ int choice;
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}
}
PROGRAM 12
Program to insert element in linked lists
#include <stdio.h>
#include <stdlib.h>
struct node{
int val;
struct node *next;
};
/*Print the linked list*/
void print_list(struct node *head)
{
printf("H->");
while(head)
{
printf("%d->", head->val);
head = head->next;
} printf("|||\n");
}
/*Insert an element at the front of the list*/
void insert_front(struct node **head, int value)
{
struct node * new_node = NULL;
/*Allocating memory for the new node*/
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL)
{
printf("Failed to insert element. Out of memory");
}
new_node->val = value;
/*Pointing the new node to where head is currently pointing to*/
new_node->next = *head;
/*Pointing head to new node.*/
*head = new_node;
}
void main()
{
int count = 0, i, val;
struct node * head = NULL;
printf("Enter number of elements: ");
scanf("%d", &count);
for (i = 0; i < count; i++)
{
printf("Enter %dth element: ", i);
scanf("%d", &val);
insert_front(&head, val);
}
printf("Linked List: ");
print_list(head);
}
PROGRAM 13
Program to delete element in linked lists
#include <stdio.h>
#include <stdlib.h>

// A linked list node


struct Node
{
int data;
struct Node *next;
};

/* Given a reference (pointer to pointer) to the head of a list


and an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}

/* Given a reference (pointer to pointer) to the head of a list


and a position, deletes the node at the given position */
void deleteNode(struct Node **head_ref, int position)
{
// If linked list is empty
if (*head_ref == NULL)
return;

// Store head node


struct Node* temp = *head_ref;

// If head needs to be removed


if (position == 0)
{
*head_ref = temp->next; // Change head
free(temp); // free old head
return;
}

// Find previous node of the node to be deleted


for (int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;

// If position is more than number of ndoes


if (temp == NULL || temp->next == NULL)
return;

// Node temp->next is the node to be deleted


// Store pointer to the next of node to be deleted
struct Node *next = temp->next->next;

// Unlink the node from linked list


free(temp->next); // Free memory

temp->next = next; // Unlink the deleted node from list


}

// This function prints contents of linked list starting from


// the given node
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}

/* Drier program to test above functions*/


int main()
{
/* Start with the empty list */
struct Node* head = NULL;

push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);

puts("Created Linked List: ");


printList(head);
deleteNode(&head, 4);
puts("\nLinked List after Deletion at position 4: ");
printList(head);
return 0;
}

PROGRAM 14
Program for Push, Pop and Display in Stack
#include<stdio.h>
#include<process.h>
#include<stdlib.h>

#define MAX 5 //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;

while(1) //infinite loop, will end when choice will be 4


{
printf("\n*** Stack Menu ***");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);

default: printf("\nWrong Choice!!");


}
}
}

void push()
{
int val;

if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

void display()
{
int i;

if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
printf("%d\n",stack[i]);
}
}
PROGRAM 15
Program for switch case to perform
enqueue(),dequeue() and display operations
in queue
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
PROGRAM 16:
Program to add two sparse matrix
#include<stdio.h>
#include<conio.h>
void main(){
int a[20][20],b[20][20],sa[15][3],sb[15][3],sc[30][3],m,n,i,j,k;
clrscr();
printf("\nEnter order of matrix(m*n) :");
scanf("%d %d",&m,&n);
//INPUT MATRIX 1
printf("\nEnter elements of Matrix 1\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
//INPUT MATRIX 2
Printf ("Enter elements of Matrix 2\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&b[i][j]);
}
}
// GENERATE SPARSE 1
k=1;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(a[i][j]!=0){ // ENTER DATA OF ELEMENT IF ELEMENT!=0
sa[k][0]=i;
sa[k][1]=j;
sa[k][2]=a[i][j];
k++;
}
}
}
//specifying details of sparse matrix in first row
sa[0][0]=m;
sa[0][1]=n;
sa[0][2]=k-1;
//GENERATE SPARSE 2
k=1;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(b[i][j]!=0){
sb[k][0]=i;
sb[k][1]=j;
sb[k][2]=b[i][j];
k++;
}
}
}
sb[0][0]=m;
sb[0][1]=n;
sb[0][2]=k-1;
//Display Sparse Matrices
printf("\nSparse Matrix 1\nRow\tColumn\tvalue\n");
for(i=0;i<=sa[0][2];i++){
for(j=0;j<=2;j++){
printf("%d\t",sa[i][j]);
}
printf("\n");
}printf("\nSparse Matrix 2\nRow\tColumn\tvalue\n");
for(i=0;i<=sb[0][2];i++){
for(j=0;j<=2;j++){
printf("%d\t",sb[i][j]);
}
printf("\n");
}
//add sparse matrix 1 & 2
i=1;//for sparse matrix 1
j=1;//for sm2
k=1;//for sm sum
// infinite loop that breaks when no elements are present in sm1 & sm2 to be
transferred
// alternative for break (for(i=1,j=1;(i<=sa[0][2])||(j<=sb[0][2]);))
for(;;){
if(!((i==sa[0][2]+1)||(j==sb[0][2]+1))){
if(sa[i][0]<sb[j][0]){ //row1<row2 enter first element
sc[k][0]=sa[i][0];
sc[k][1]=sa[i][1];
sc[k][2]=sa[i][2];
k++;
i++;
} else if(sa[i][0]>sb[j][0]){ //row1>row2 enter second element
sc[k][0]=sb[j][0];
sc[k][1]=sb[j][1];
sc[k][2]=sb[j][2];
k++;
j++;
}else { //row1=row2 , compare columns
if(sa[i][1]<sb[j][1]){ //col1<col2 enter first element
sc[k][0]=sa[i][0];
sc[k][1]=sa[i][1];
sc[k][2]=sa[i][2];
k++;
i++;
} else if(sa[i][1]>sb[j][1]){ //col1>col2 enter second
element
sc[k][0]=sb[j][0];
sc[k][1]=sb[j][1];
sc[k][2]=sb[j][2];
k++;
j++;
} else { //add both elements and enter
sc[k][0]=sa[i][0];
sc[k][1]=sa[i][1];
sc[k][2]=sa[i][2]+sb[j][2];
k++;
i++;
j++;
}
}
}else if((i==sa[0][2]+1)){ // only second sparse matrix remains
sc[k][0]=sb[j][0];
sc[k][1]=sb[j][1];
sc[k][2]=sb[j][2];
k++;
j++;
} else { // only first sparse matrix remains
sc[k][0]=sa[i][0];
sc[k][1]=sa[i][1];
sc[k][2]=sa[i][2];
k++;
i++;
}
//printf("%d %d %d\t",sc[k][0],sc[k][1],sc[k][2]);
if((i==sa[0][2]+1)&&(j==sb[0][2]+1)){
break;
}
}
//enter details of sum sparse matrix to first row
sc[0][0]=m;
sc[0][1]=n;
sc[0][2]=k-1;
//DISPLAY SUM SPARSE MATRIX
printf("\nSparse Matrix Sum\nRow\tColumn\tvalue\n");
for(i=0;i<=sc[0][2];i++){
for(j=0;j<=2;j++){
printf("%d\t",sc[i][j]);
}
printf("\n");
}
getch();
}

You might also like