Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The code example FIFO using an array, in Module . FIFO stands for First-In, First Out, meaning the first value read into the FIFO will

The code example FIFO using an array, in Module. FIFO stands for First-In, First Out, meaning the first value read into the FIFO will be the first value read out at some later time. Another queue structure is a LIFO, which is Last-In, First Out. This is also called a stack. When you pop an entry from a LIFO, it will be the most recent value pushed onto the stack.

Based on FIFO.cpp image text in transcribed but using a vector (rather than an array), create a LIFO call your file LIFO.cpp. Make the stored type an int. Because you are using a vector, you do not need to designate the LIFO size as part of the program. In addition, the call to return the absolute size is now meaningless. There is no needfor a Head or a Tail index and therefore no need for an initialization function.

What your design needs to do is to enqueue an entry, dequeue an entry, and return the filled size. A function to query whether the LIFO is empty or not is also nice, as is a function to read the last entry without dequeueing it. Retain the return (renamed) values Success and Error, LIFO Empty; you will not need Error LIFO Full. In main(), create the following commands that will verify operation of your LIFO.

d - dequeue next entry from LIFO. Display. e N - enqueue N onto the LIFO er - enqueue a randomly selected number onto the LIFO. Display first. s? - read and display the size of the LIFO e? - is FIFO empty? l? - read (without dequeueing) last entered value q - quit and exit

FIFO.cpp program as below:

#include

#include

#include

#include

#include

using namespace std;

enum { Success, Error_FIFO_Full, Error_FIFO_Empty };

void fifo_initialize(void);

int rtn_fifo_absolute_size(void);

int rtn_fifo_filled_size(void);

bool is_fifo_empty(void);

bool is_fifo_full(void);

char fifo_enqueue(double d_value);

char fifo_dequeue(double& d_value);

int main()

{

double acc;

string command;

// Initialize the FIFO and key the random number generator.

fifo_initialize();

srand(unsigned(time(0)));

cout

cout

"d (Dequeue), "

"e (Enqueue a Number), "

"er (Enqueue a random number), "

"e? (FIFO empty?), "

"f? (FIFO full?), "

"fs? (fill size), "

"as? (absolut size), "

"q (quit) ";

while (true)

{

cout

cin >> command;

// Parse the command

if (command == "d")

{

if (fifo_dequeue(acc) == Success)

{ cout

else

{ cout

}

else if (command == "e")

{

cin >> acc;

if (fifo_enqueue(acc) != Success)

{ cout

}

else if (command == "er")

{

acc = rand() * 1000.0 / RAND_MAX; // Between

0 and 1000

if (fifo_enqueue(acc) != Success)

{ cout

else

{ cout

}

else if (command == "e?")

{

if (is_fifo_empty())

{ cout

else

{ cout

} // end else if - e?

else if (command == "f?")

{

if (is_fifo_full())

{ cout

else

{ cout

} // end else if - f?

else if (command == "fs?")

{ cout

else if (command == "as?")

{ cout

else if (command == "q") break;

else

{ cout

} // end while - infiinite loop

return 0;

} // end main

const int FIFO_SIZE = 10; // Size

double FIFO[FIFO_SIZE]; // FIFO

int head_idx; // Head index

int tail_idx; // Tail index

int filled_size; // Filled size

void fifo_initialize(void)

{

head_idx = tail_idx = filled_size = 0;

return;

} // end fifo_initialize

int rtn_fifo_absolute_size(void)

{

return FIFO_SIZE;

} // end rtn_fifo_absolute_size

int rtn_fifo_filled_size(void)

{

return filled_size;

} // end rtn_fifo_filled_size

bool is_fifo_empty(void)

{

return (filled_size

} // end if - is_fifo_empty

bool is_fifo_full(void)

{

return (filled_size >= FIFO_SIZE) ? true : false;

} // end if - is_fifo_full

char fifo_enqueue(double value_in)

{

char rtn_value = Success;

if (filled_size >= FIFO_SIZE)

{

rtn_value = Error_FIFO_Full;

}

else

{

FIFO[head_idx] = value_in;

if (++head_idx >= FIFO_SIZE)

{

head_idx = 0;

}

filled_size++;

}

return rtn_value;

} // end fifo_enqueue

char fifo_dequeue(double &value_out)

{

char rtn_value = Success;

if (filled_size

{

rtn_value = Error_FIFO_Empty;

}

else

{

value_out = FIFO[tail_idx];

if (++tail_idx >= FIFO_SIZE)

{

tail_idx = 0;

}

filled_size--;

}

return rtn_value;

} // end fifo_dequeue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

ISBN: 3642341780, 978-3642341786

More Books

Students also viewed these Databases questions