In: Computer Science
Programming Language Concept assignment:
1. Design abstract data type for matrices with integer elements
in C++ language, including operations for matrix addition,
subtraction, and multiplication!
2. Design abstract queue data types for float elements in C++
language, including operations for enqueue, dequeue, and empty. The
dequeue operation removes the element and returns its value!
3. Set semaphores in Ada and use them to provide co-operation and
synchronization of competitions in shared buffer instances!
1. In c++
class Matrix
{
int a[10][10],b[10][10],c[10][10],r,q,r1,q1;
public : void mat1() ;
int matrixp1();
void matrix2();
int matrixp2();
int add();
int sub();
int mul();
};
void Matrix :: matrix1() //intake matrices A:
{
int i,j;
cout<<“Enter size of row & column of matrix\n”;
cin>>r>>q;
cout<<“Enter actual element of matrix\n”;
for (i=0;i<r;i++)
{
for(j=0;j<q;j++)
{
cin>>a[i][j];
}
}
}
int Matrix :: matrixp1() // printing matrices A:
{
int i,j;
cout<<“Matrix a is\n”;
for (i=0;i<r;i++)
{
for(j=0;j<q;j++)
{
cout<<a[i][j]<<” \t”;
}
cout<<“\n”;
}
}
void Matrix :: matrix2() // intake matrices B:
{
int i,j;
cout<<“Enter size of row & column of matrix\n”;
cin>>r1>>q1;
cout<<“Enter actual element of matrix\n”;
for (i=0;i<r1;i++)
{
for(j=0;j<q1;j++)
{
cin>>b[i][j];
}
}
}
int Matrix :: matrixp2() // printing matrices B:
{
int i,j;
cout<<“\nMatrix b is:\n”;
for (i=0;i<r1;i++)
{
for(j=0;j<q1;j++)
{
cout<<b[i][j]<<” \t”;
}
cout<<“\n”;
}
}
//addition
int Matrix :: add()
{
int i,j;
cout<<“\nAddition of matrix is\n”;
if(r==r1 && q==q1)
for (i=0;i<r;i++)
{
for(j=0;j<q;j++)
{
cout<<a[i][j]+b[i][j]<<“\t”;
}
cout<<“\n”;
}
else
{
cout<<“matrix error\n\n”;
}
}
//subtraction
int Matrix :: sub()
{
int i,j;
cout<<“Subtraction of matrix is\n”;
if(r==r1 && q==q1)
for (i=0;i<r;i++)
{
for(j=0;j<q;j++)
{
cout<<a[i][j]-b[i][j]<<“\t”;
}
cout<<“\n”;
}
else
{
cout<<“matrix error \n \n”;
}
}
//multiplication
int Matrix :: mul()
{
int i,j,k,sum;
cout<<“Multiplication of matrix is\n”;
if(r==q)
for(i=0;i<r;i++)
{
for(j=0;j<q1;j++)
{
sum=0;
for(k=0;k<r1;k++)
{
sum=sum+a[i][k]*b[k][j];
}
cout<<sum<<“\t”;
}
cout<<“\n”;
}
}
//main
int main()
{
Matrix m;
int choice,ch;
m.mat1();
m.mat2();
do
{
cout<<“MENU\n”;
cout<<” 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4. Exit \nEnter your choice…!\n”;
cin>>choice;
switch (choice)
{
case 1:
m.matrixp1();
m.matrixp2();
m.add();
break;
case 2:
m.matrixp1();
m.matrixp2();
m.sub();
break;
case 3:
m.matrixp1();
m.matrixp2();
m.mul();
break;
default : break;
}
cout<<“Do you want to continue press 1 :\n”;
cin>>ch;
}
while (ch==1);
return 0;
}
2. In c++
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
using namespace std;
int q[SIZE],front=0,rear=0;
int main()
{
int ch;
void enq();
void deq();
void display();
while(1)
{
cout<<"\n 1.add element";
cout<<"\n 2.remove element";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
enq();
break;
case 2:
deq();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
}
//enqueue
void enq()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}
//dequeue
void deq()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{
no=q[front];
front++;
cout<<"\n"<<no<<" removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}
}
3.
The Ada language does not directly provide library functions for
a semaphore. However, semaphores can be implemented by means of a
protected object. Create a package specification Semaphore in the
file Semaphores.ads and the corresponding package body in the file
Semaphores.adb
that implements a counting
semaphore
Use the semaphore package for a reliable implementation of the
problem. Modify the file xyz.adb
and save the final
code as xyz_sem.adb
. In order to use the semaphore
package it shall be installed in the same directory as
xyz_sem.adb
. It can then be accessed by
with Semaphores;
use Semaphores;