Question

In: Computer Science

Programming Language Concept assignment: 1. Design abstract data type for matrices with integer elements in C++...

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!

Solutions

Expert Solution

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;


Related Solutions

Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Your class must include the following member functions: a constructor to set the hours and pay rate as arguments, a default constructor to set data members to 0, member functions to set each of the member variables to values given as an...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c ++
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
Complete the following assignment in C programming language. Get the user’s first name and store it...
Complete the following assignment in C programming language. Get the user’s first name and store it to a char array Declare a character array to hold at least 20 characters. Ask for the user’s first name and store the name into your char array. Hint: Use %s for scanf. %s will only scan one word and cannot scan a name with spaces. Get 3 exam scores from the user: Declare an array of 3 integers Assume the scores are out...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT