Aim:-Wap To Convert The Sparse Matrix Into Non-Zero Form and Vice-Versa. CODE
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++;
}
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;
};
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');
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');
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');
return 0;
}
OUTPUT:-
INSERTION
DELETION