So using Microsoft visual studio I\'m having trouble with this C++ problem. Plea
ID: 3595782 • Letter: S
Question
So using Microsoft visual studio I'm having trouble with this C++ problem. Please help me with my code.
Here is the question:
Here is my code:
#include <iostream>
using namespace std;
class Stack {
int arr[27][2];
bool empty = true;
int topIndex = -1;
int count = 0;
public :
//delete top element
void pop() {
//return if empty
if(empty)
return;
count--;
//if only one element , then remove it and set isEmpty true
if(arr[topIndex][1] == -1) {
empty = true;
topIndex = 0;
arr[topIndex][0]=0;
arr[topIndex][1]=0;
return;
}
int nextTop = arr[topIndex][1];
arr[topIndex][0] = 0;
arr[topIndex][1] = 0;
topIndex = nextTop;
}
//return top element
int top() {
return arr[topIndex][0];
}
//push element at top
void push(int value) {
//return if stack full
if(count==26)
return;
empty = false;
//increase count
count++;
int randomIndex;
for(int i=1; i<27; i++) {
if(isValid(i)) {
randomIndex = i;
break;
}
}
arr[randomIndex][0] = value;
arr[randomIndex][1] = topIndex;
topIndex = randomIndex;
}
//return isEmpty
bool isEmpty() {
return empty;
}
//display all elements from top to bottom
void display() {
if(empty)
return;
int index = topIndex;
while(index!=-1) {
cout<<arr[index][0]<<" ";
index = arr[index][1];
}
cout<<endl;
}
//check if index is valid to add number or not
bool isValid(int index) {
if(arr[index][1]==0)
return true;
return false;
}
};
//main program
int main() {
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
stack.push(10);
stack.display();
stack.push(20);
stack.display();
stack.push(30);
stack.display();
stack.push(40);
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
return 0;
}
Explanation / Answer
From what I understand, you need help with the random valid position generation part of the question. Your code was quite well written for the most part and made my task easy. However, I've made some minor changes in the code apart from adding the random functionality. The comments in the code which you should have a look at start with "CHANGE:". Please upvote my answer if you find it useful. Have a good day.
------CODE------
#include <iostream>
// CHANGE: Added headers for random number generation
#include <stdlib.h>
#include <time.h>
using namespace std;
class Stack {
// CHANGE: Capacity of 26 would mean 26 as highest index
int arr[26][2];
bool empty;
int topIndex;
int count;
public :
Stack()
{
// CHANGE: It is good practice in general to initialize inside the constructor
empty = true;
topIndex = -1;
count = 0;
// Setting all addresses to -2 to imply that they are empty
for(int i=0;i<26;i++)
arr[i][1] = -2;
}
//delete top element
void pop() {
//return if empty
if(empty)
return;
count--;
//if only one element , then remove it and set isEmpty true
if(arr[topIndex][1] == -1) {
empty = true;
arr[topIndex][0]=0; // not necessary
arr[topIndex][1]=-2;
// CHANGE: Top index should be set to -1 and not 0 because pushing uses arr[randomIndex][1] = topIndex;
// Empty stack should therefore always have topIndex = -1
// This assignment should also be done after setting arr[topIndex][0] and arr[topIndex][1] to 0
topIndex = -1;
return;
}
int nextTop = arr[topIndex][1];
arr[topIndex][0] = 0; // not necessary
arr[topIndex][1] = -2;
topIndex = nextTop;
}
//return top element
int top() {
return arr[topIndex][0];
}
//push element at top
void push(int value) {
//return if stack full
if(count==26)
return;
empty = false;
//increase count
count++;
int randomIndex;
// CHANGE: Added random index generation
do{
randomIndex = rand()%26;
}while(!isValid(randomIndex));
arr[randomIndex][0] = value;
arr[randomIndex][1] = topIndex;
topIndex = randomIndex;
}
//return isEmpty
bool isEmpty() {
return empty;
}
//display all elements from top to bottom
void display() {
if(empty)
return;
int index = topIndex;
while(index!=-1) {
cout<<arr[index][0]<<" ";
index = arr[index][1];
}
cout<<endl;
}
//check if index is valid to add number or not
bool isValid(int index) {
// CHANGE: Using 0 is not advisable here because a data element could have next address 0
if(arr[index][1]==-2)
return true;
return false;
}
};
//main program
int main() {
// CHANGE: Seeding random number generator
srand( (unsigned)time( NULL ) );
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
stack.push(8);
stack.push(9);
stack.push(10);
stack.display();
stack.push(20);
stack.display();
stack.push(30);
stack.display();
stack.push(40);
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
cout<<stack.top()<<endl;
stack.pop();
stack.display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.