Question: I have to modify code for a project. This is a summary of its function. Create 8 registers. 4 inputs and 4 outputs. (hint: same
I have to modify code for a project. This is a summary of its function.
Create 8 registers. 4 inputs and 4 outputs. (hint: same size as your data type)
There will be two runs your main program. Single register and four registers.
Use looping to load/unload the queues from the data sets and to your output file. Each loop is a clock cycle. Count your clock cycle for each of the two functions. Output these counts to your file. Output your input and output data in 10 by 10 matrix to your file.
Run one uses a single register to input to queues and a single register output data.
Run two uses 4 registers to input to queues and 4 registers to output data to files
Here is the code structure we made in class.
For one input register and one output register:
Clockin = 0
Clockout = 0
Do 10 times
Do 4 times (once for each queue)
Load single input register with data value
Load one queue with data value in single register
Clockin++
End
End
Do N times
Do 4 times (once for each queue) exit when last head is null
Load single output register with data value
Delete single value from the same queue that the above data came from
Clockout++
End
Do 4 times (once for each queue) skip when N data values loaded
Load single input register with data value
Load one queue with data value in single register
Clockin++
End
End
Print Clockin and Clockout
For four input registers and four output registers:
Clockin = 0
Clockout = 0
Do 10 times
Load four input registers with data values from the 4 data arrays
Load four queues with data values in four registers
Clockin++
End
Do N times
Exit when heads are null
Load four output registers with data values from queues
Delete four values from the same queues that the above data came from
Clockout++
End
Skip when N data values loaded
Load four input registers with data value
Load four queues with data values in four registers
Clockin++
End
End
Print Clockin and Clockout
This is the project we have created thus far.
#include
#include
#include
#include
using namespace std;
class LinkedList;
class Node {
public:
int data;
Node*next;
public:
Node() :data(0), next(NULL) {}
Node(int d) :data(d), next(NULL) {}
friend class LinkedList;
};
class LinkedList {
public:
Node * head;
Node*tail;
int size;
public:
LinkedList() :head(NULL), tail(NULL), size(0) {}
LinkedList(const LinkedList&L2) {
Node*temp = L2.head;
while (temp != NULL) {
insertAtEnd(temp->data);
temp = temp->next;
}
}
void insertAtFront(int data) {
if (head == NULL) {
head = tail = new Node(data);
}
else {
Node *n = new Node(data);
n->next = head;
head = n;
}
}
void insertAtEnd(int data) {
if (head == NULL) {
head = tail = new Node(data);
}
else {
tail->next = new Node(data);
tail = tail->next;
}
}
void insert(int data, int position) {
if (position == 0) {
insertAtFront(data);
}
else {
Node*temp = head;
int count = 1;
while (count temp = temp->next; count = count + 1; } Node*nextNode = temp->next; Node*newNode = new Node(data); temp->next = newNode; newNode->next = nextNode; } } void sortedInsert(int data) { int pos = 0; if (head == NULL) { insertAtFront(data); return; } Node *temp = head; while (temp != NULL && data>temp->data) { temp = temp->next; pos++; } if (pos == 0) { insertAtFront(data); } else { insert(data, pos); } } //Define function to print the list elements void print() { Node*temp = head; int count = 0; while (temp != NULL) { if (count == 10) { cout << endl; count = 0; } cout << temp->data << "-->"; temp = temp->next; count++; } cout << endl; } ~LinkedList() { Node* temp = head; while (temp != NULL) { Node*next = temp->next; delete temp; temp = next; } head = NULL; } bool isEmpty() { if (head == NULL) return true; return false; } int removeFirst() { int retVal = 0; if (head == NULL) return 0; else { if (head == tail) { retVal = head->data; head = NULL; tail = NULL; } else { retVal = head->data; head = head->next; } } return retVal; } int removeLast() { int retVal = 0; if (tail == NULL) return 0; else { if (head == tail) { retVal = head->data; head = NULL; tail = NULL; } else { retVal = tail->data; Node* pvTail = head; while (pvTail->next != tail) pvTail = pvTail->next; tail = pvTail; tail->next = NULL; } } return retVal; } }; void arrayPrint(int *arr, int n, ofstream &fp) { int count = 0; for (int i = 0; i if (count == 10) { fp << endl; count = 0; } fp << arr[i] << " "; count++; } fp << endl << endl; } int main() { //Declare 4 arrays for inputs int arr1[100]; int arr2[100]; int arr3[100]; int arr4[100]; //Declare 4 arrays for outputs int *outArr1; int *outArr2; int *outArr3; int *outArr4; //Open output file ofstream fID("output.txt"); //Create 4 stacks LinkedList stack1, stack2, stack3, stack4; //Create 4 queues LinkedList queue1, queue2, queue3, queue4; //Seed srand(time(NULL)); //Loop to initialize all 4 arrays for (int i = 0; i<100; i++) { //Generate random value for first array arr1[i] = rand() % 100; //Generate random value for second array arr2[i] = rand() % 100; //Generate random value for third array arr3[i] = rand() % 100; //Generate random value for fourth array arr4[i] = rand() % 100; } //Loop to insert first 10 elements from input arrays for (int kk = 0; kk<10; kk++) { //Insert elements into stacks stack1.insertAtFront(arr1[kk]); stack2.insertAtFront(arr2[kk]); stack3.insertAtFront(arr3[kk]); stack4.insertAtFront(arr4[kk]); //Insert elements into queues queue1.insertAtEnd(arr1[kk]); queue2.insertAtEnd(arr2[kk]); queue3.insertAtEnd(arr3[kk]); queue4.insertAtEnd(arr4[kk]); } //Initialize output arrays outArr1 = new int[100]; outArr2 = new int[100]; outArr3 = new int[100]; outArr4 = new int[100]; //Set the index int aa = 0; for (int kk = 10; kk<100; kk++) { //Delete element from queues int tp1 = queue1.removeFirst(); int tp2 = queue2.removeFirst(); int tp3 = queue3.removeFirst(); int tp4 = queue4.removeFirst(); //Store deleted element into output arrays outArr1[aa] = tp1; outArr2[aa] = tp2; outArr3[aa] = tp3; outArr4[aa] = tp4; aa++; //Insert elements into queues queue1.insertAtEnd(arr1[kk]); queue2.insertAtEnd(arr2[kk]); queue3.insertAtEnd(arr3[kk]); queue4.insertAtEnd(arr4[kk]); } //Loop to delete remaining elements from queue while (!queue1.isEmpty()) { //Delete element from queues int tp1 = queue1.removeFirst(); int tp2 = queue2.removeFirst(); int tp3 = queue3.removeFirst(); int tp4 = queue4.removeFirst(); //Store deleted element into output arrays outArr1[aa] = tp1; outArr2[aa] = tp2; outArr3[aa] = tp3; outArr4[aa] = tp4; aa++; } //Call function to print input arrays fID << "Array 1:" << endl; arrayPrint(arr1, 100, fID); fID << " Array 2:" << endl; arrayPrint(arr2, 100, fID); fID << " Array 3:" << endl; arrayPrint(arr3, 100, fID); fID << " Array 4:" << endl; arrayPrint(arr4, 100, fID); //Call function to print output arrays. fID << " Output Array 1:" << endl; arrayPrint(outArr1, 100, fID); fID << " Output Array 2:" << endl; arrayPrint(outArr2, 100, fID); fID << " Output Array 3:" << endl; arrayPrint(outArr3, 100, fID); fID << " Output Array 4:" << endl; arrayPrint(outArr4, 100, fID); //Close file fID.close(); //Pause window system("pause"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
