Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The code example FIFO using an array, in Module . FIFO stands for First-In, Firs

ID: 3705125 • Letter: T

Question

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.

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 <iostream>

#include <iomanip>

#include <cstdlib>

#include <ctime>

#include <string>

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 << fixed << setprecision(2);

cout << "Command Menu: "

"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 << " Please enter command: ";

cin >> command;

// Parse the command

if (command == "d")

{

if (fifo_dequeue(acc) == Success)

{ cout << acc << endl; }

else

{ cout << " Dequeue failed - FIFO empty" << endl; }

}

else if (command == "e")

{

cin >> acc;

if (fifo_enqueue(acc) != Success)

{ cout << " Enqueue failed - FIFO full" << endl; }

}

else if (command == "er")

{

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

0 and 1000

if (fifo_enqueue(acc) != Success)

{ cout << " Enqueue failed - FIFO full" << endl; }

else

{ cout << acc << endl; }

}

else if (command == "e?")

{

if (is_fifo_empty())

{ cout << " FIFO empty" << endl; }

else

{ cout << " FIFO not empty" << endl; }

} // end else if - e?

else if (command == "f?")

{

if (is_fifo_full())

{ cout << " FIFO full" << endl; }

else

{ cout << " FIFO not full" << endl; }

} // end else if - f?

else if (command == "fs?")

{ cout << rtn_fifo_filled_size() << endl; }

else if (command == "as?")

{ cout << rtn_fifo_absolute_size() << endl; }

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

else

{ cout << " Unknown command" << endl; }

} // 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 <= 0) ? true : false;

} // 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 <= 0)

{

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

Explanation / Answer

#include <iostream>
#include <vector>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;
vector <int> LIFO;

enum { Success, Error_LIFO_Full, Error_LIFO_Empty };
bool is_lifo_empty(void);
char lifo_enqueue(int d_value);
char lifo_dequeue(int& d_value);


int main()
{
string command;
int acc;
srand(unsigned(time(0)));

cout << fixed << setprecision(2);
cout << "Command Menu: "
"d (Dequeue), "
"e (Enqueue a Number), "
"s? read and display the size of the LIFO, "
"er (Enqueue a random number), "
"e? (LIFO empty?), "
"r (Read last entry), "
"q (quit) ";
while (true)
{
cout<< " Please enter command: ";
cin>> command;

// Parse the command

if (command == "d")
{
if (lifo_dequeue(acc) == Success)
{
cout << acc << endl;
}
else
{
cout << " Dequeue failed - LIFO empty" << endl;
}
}

else if (command == "e")
{
cin >> acc;
lifo_enqueue(acc);
}

else if (command == "er")
{
acc = rand() * 1000.0 / RAND_MAX; // Between 0 and 1000
lifo_enqueue(acc);
cout << acc << endl;
}
else if (command == "r")
{
if(is_lifo_empty())
{
cout<<" LIFO empty";
}
else
{
cout<<" Last entry:"<<LIFO.back();
}

}
else if (command == "s?")
{
cout<<"Size of vector is:"<<LIFO.size();
}
else if (command == "e?")
{
if (is_lifo_empty())
{
cout << " LIFO empty" << endl;
}
else
{
cout << " LIFO not empty" << endl;
}
} // end else if - e?
else if (command == "q")
{
break;
}
else
{
cout << " Unknown command" << endl; }
} // end while - infinite loop
return 0;
} // end main

bool is_lifo_empty(void)
{
return LIFO.empty();
}


char lifo_enqueue(int d_value)
{
LIFO.push_back(d_value);
return Success;
}

char lifo_dequeue(int &value_out)
{
char rtn_value = Success;
if (is_lifo_empty())
{
rtn_value = Error_LIFO_Empty;
}
else
{
value_out = LIFO.back();
LIFO.pop_back();
}
return rtn_value;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote