This task is to be done in c++ Task 2: Use given code and write \"printListR\" f
ID: 3740942 • Letter: T
Question
This task is to be done in c++
Task 2: Use given code and write "printListR" function using Stack ADT. There is an array of integer
having multiple lists each list is terminated by 0 as delimiter. You have to print each list in reverse
order. Consider example:
9 1 4 3 6 1 4 1 1 8 0 4 7 2 7 5 7 9 0 5 4 9
8 1 1 4 1 6 3 4 1 9 First list is ended on digit 8
9 7 5 7 2 7 4 Second list is ended on digit 9
9 4 5 Last list has 5 4 9
void rInitArray(int *x, const int SIZE) {
int i, count=0, d;
for (i=0;i d=rand()%10;
if (d==0 || count==10){
x[i]=0;
count=0;
}
else {
x[i]=d;
count++;
}
}
}
void printArray(int *x, const int SIZE) {
int i;
for (i=0;i cout<
return 0;
}
Explanation / Answer
Hello, I have implemented the required method as necessary. Made the other two methods required changes to work properly, it was throwing a lot of compile errors. Go through the code, and comments included, understand how the method works, and drop a comment if you have any queries. Thanks.
//code.cpp
#include<iostream>
#include<stack>
#include<stdlib.h>
#include<time.h>
using namespace std;
//method to initialize the array, made proper changes to work without errors
void rInitArray(int *x, const int SIZE) {
int i, count=0, d=rand()%10;
for (i=0;i<SIZE;d=rand()%10,i++){
if (d==0 || count==10){
x[i]=0;
count=0;
}
else {
x[i]=d;
count++;
}
}
}
//method to print the array
void printArray(int *x, const int SIZE) {
int i;
for (i=0;i<SIZE;i++){
cout<<x[i]<<" ";
}
cout<<endl;
}
/* method to print all the lists terminated by 0, in the array x, in reverse order
* using stack of integers */
void printListR(int *x, const int SIZE){
int i=0;
//defining a stack of integers
stack<int> st;
//looping through all elements
int listcount=0; //to keep the track of lists
while(i<SIZE){
//getting current element
int num=x[i];
if(num==0){
//zero is found
++listcount;
if(st.empty()){
//no elements were found before 0
cout<<"The list "<<listcount<<" was empty (0 at first position)"<<endl;
}else{
//saving the last element (top of stack)
int last=st.top();
/*printing all elements in the stack, which will be the reverse order
* of entry */
while(!st.empty()){
int num=st.top();
st.pop();
cout<<num<<" ";
}
//displaying the last digit
cout<<"The list "<<listcount<<" is ended on digit "<<last<<endl;
}
}else{
//number is not 0, pushing to the stack
st.push(num);
if(i==SIZE-1){
//last element of the array
stack<int> st2; //using another stack to store the numbers
while(!st.empty()){
/*displaying the numbers in reverse order, as well as adding to the
* other stack */
int num=st.top();
st.pop();
cout<<num<<" ";
st2.push(num);
}
/* displaying the last list in normal order, from the second stack */
cout<<"Last list has ";
while(!st2.empty()){
int num=st2.top();
cout<<num<<" ";
st2.pop();
}
cout<<endl;
}
}
i++;
}
}
int main(){
srand(time(NULL));//seeding random number generator
int size=20;
int array[size];
rInitArray(array,size); //generating array
printArray(array,size); //displaying the initial array
printListR(array,size); //calling the required method
return 0;
}
/*OUTPUT*/
0 6 5 0 4 3 4 3 3 9 0 4 0 5 0 7 5 4 1 1
The list 1 was empty (0 at first position)
5 6 The list 2 is ended on digit 5
9 3 3 4 3 4 The list 3 is ended on digit 9
4 The list 4 is ended on digit 4
5 The list 5 is ended on digit 5
1 1 4 5 7 Last list has 7 5 4 1 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.