The code example FIFO using an array, in Module . FIFO stands for First-In, Firs
ID: 3702459 • 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 <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include<vector>
using namespace std;
enum { Success, Error_FIFO_Empty };
//void lifo_initialize(void);
//int rtn_lifo_absolute_size(void);
int rtn_lifo_filled_size(void);
bool is_lifo_empty(void);
//bool is_lifo_full(void);
char lifo_enqueue(int d_value);
char lifo_dequeue(int& d_value);
//read without dequeue,added by CheggEA
int rtn_lifo_read();
int main()
{
int 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), "
"l? (read without dequeue), "
"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 - FIFO empty" << endl;
}
}
else if (command == "e")
{
cin >> acc;
if (lifo_enqueue(acc) != Success)
{
cout << " Enqueue failed - FIFO full" << endl;
}
}
else if (command == "er")
{
acc = rand() * 1000.0 / RAND_MAX; // Between 0 and 1000
if (lifo_enqueue(acc) != Success)
{
cout << " Enqueue failed - FIFO full" << endl;
}
else
{
cout << acc << endl;
}
}
else if (command == "e?")
{
if (is_lifo_empty())
{
cout << " FIFO empty" << endl;
}
else
{
cout << " FIFO not empty" << endl;
}
} // end else if - e?
/*else if (command == "f?")
{
if (is_lifo_full())
{
cout << " FIFO full" << endl;
}
else
{
cout << " FIFO not full" << endl;
}
} */// end else if - f?
else if (command == "fs?")
{
cout << rtn_lifo_filled_size() << endl;
}
//read without dequeue
else if (command == "l?")
{
cout << rtn_lifo_read() << 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
//declared stack of type int for LIFO, CheggEA
vector<int> stack;
/*void lifo_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_lifo_filled_size(void)
{
return stack.size();
} // end rtn_fifo_filled_size
bool is_lifo_empty(void)
{
return (stack.size() <= 0) ? true : false;
} // end if - is_fifo_empty
/*bool is_lifo_full(void)
{
return (filled_size >= FIFO_SIZE) ? true : false;
}*/ // end if - is_fifo_full
char lifo_enqueue(int value_in)
{
char rtn_value = Success;
stack.push_back(value_in);
return rtn_value;
} // end fifo_enqueue
char lifo_dequeue(int &value_out)
{
char rtn_value = Success;
if (is_lifo_empty())
rtn_value = Error_FIFO_Empty;
else
{
value_out = stack.back();
stack.pop_back();
}
return rtn_value;
} // end lifo_dequeue
//read without dequeue, added by cheggEA
int rtn_lifo_read()
{
return stack.back();
}
/*output
Command Menu:
d (Dequeue),
e (Enqueue a Number),
er (Enqueue a random number),
e? (FIFO empty?),
fs? (fill size),
l? (read without dequeue),
q (quit)
Please enter command: d
Dequeue failed - FIFO empty
Please enter command: e
1
Please enter command: e
2
Please enter command: l?
2
Please enter command: er
566
Please enter command: l?
566
Please enter command: e?
FIFO not empty
Please enter command: d
566
Please enter command: d
2
Please enter command: d
1
Please enter command: e?
FIFO empty
Please enter command: er
845
Please enter command: e
1
Please enter command: l?
1
Please enter command: d
1
Please enter command: d
845
Please enter command: d
Dequeue failed - FIFO empty
Please enter command: q
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.