0% found this document useful (0 votes)
5 views10 pages

Aim:-Wap To Convert The Sparse Matrix Into Non-Zero Form and Vice-Versa. CODE

The document contains C++ code for two main functionalities: converting a sparse matrix into a non-zero form and implementing queue operations using a circular array. The first part of the code initializes a sparse matrix, counts non-zero elements, and stores their positions in a compact format. The second part defines a circular queue class with methods for insertion, removal, and display, allowing users to interactively manage queue elements.

Uploaded by

CS 73 Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Aim:-Wap To Convert The Sparse Matrix Into Non-Zero Form and Vice-Versa. CODE

The document contains C++ code for two main functionalities: converting a sparse matrix into a non-zero form and implementing queue operations using a circular array. The first part of the code initializes a sparse matrix, counts non-zero elements, and stores their positions in a compact format. The second part defines a circular queue class with methods for insertion, removal, and display, allowing users to interactively manage queue elements.

Uploaded by

CS 73 Mohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

AIM:-WAP TO CONVERT THE SPARSE

MATRIX INTO NON-ZERO FORM AND VICE-


VERSA.
CODE:-

#include<iostream>
using namespace std;
int main()
{
int sparseMatrix[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};

int size = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
size++;
int compactMatrix[3][size];
int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
{
compactMatrix[0][k] = i;
compactMatrix[1][k] = j;
compactMatrix[2][k] =
sparseMatrix[i][j];
k++;
}

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


{
for (int j=0; j<size; j++)
cout<<compactMatrix[i][j]<<” “;

cout<<"\n";
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
cout<<sparseMatrix[i][j]<<”
“;
cout<<”\n”;
}
return 0;
}
OUTPUT:-
AIM:-Perform Queues operations using
circular Array implementation.
CODE:-
#include<iostream>
using namespace std;
const int size=5;
int flag=0;
template<class t>
class CQ
{
t queue[size];
int front,rear;
public:
CQ()
{
front=rear=-1;
}

void insert(t n)
{
if((rear+1)%size!=front)
{
rear=(rear+1)%size;
queue[rear]=n;
if(front<0)
front=0;
}
else
{
cout<<"Queue overflow element not
inserted:\n";
flag=-1;
//fflush(stdin);
}
}

t remove()
{
t a;
if(empty()!=-1)
{
if((front==rear)&&(front>=0))
{
a=queue[front];
front=rear=-1;
}
else if(front!=size-1)
{
a=queue[front++];
}
else
{
front=0;
a=queue[front];
}
return a;
}
else
{
cout<<"Queue underflow:\n";
return -1;
}
}

int empty()
{
if(front==-1)
return -1;
else
return 0;
}

void display()
{
if(empty()==-1)
{
cout<<"Empty
queue:\n";
}
else
{
int r,f;
r=rear;
f=front;
while((f+1)%size!=(r+1)%size)
{
cout<<queue[f]<<endl;
f=(f+1)%size;
}
cout<<queue[f]<<endl;

/*cout<<"the values in the original


array:\n";
for(int i=0;i<size;i++)
{
cout<<queue[i]<<" ";
}
cout<<endl;*/
}
}

};
int main()
{
CQ<char> q1;
char ch;
char n;
flag=0;
do
{
cout<<"Enter 'y' if want to enter any
element:\n";
cin>>ch;
if(ch=='y')
{
cout<<"Enter the element:\n";
cin>>n;
q1.insert(n);
if(flag==-1)
break;
}

}while(ch=='y');

cout<<"The resulting queue:\n";


q1.display();

flag=0;
if(q1.empty()!=-1)
{
do
{
cout<<"Enter 'y' if want to remove any
element:\n";
cin>>ch;
if(ch=='y')
{
n=q1.remove();
if(n==-1)
break;
}
}while(ch=='y');

cout<<"The resulting queue:\n";


q1.display();

do
{
cout<<"Enter 'y' if want to enter any
element:\n";
cin>>ch;
if(ch=='y')
{
cout<<"Enter the element:\n";
cin>>n;
q1.insert(n);
if(flag==-1)
break;
}
}while(ch=='y');

cout<<"The resulting queue:\n";


q1.display();
}

return 0;
}
OUTPUT:-

INSERTION

DELETION

You might also like