C# How to display the contents of a stack when the stack is part of an array of stacks
I am working on an assignment that involves the classic towers of Hanoi game. But, my question has nothing to do with the game, its logic, or anything like that. I have been given much of the code for this problem's solution. You will see in the code below that there is a Stack class and a Tower class. The Stack class defines a bunch of methods for using a stack. The Tower class contains a tower method for creating an array of three stacks and filling the first stack. It is these three stacks that will act as the pegs in the game. My assignment is only to complete the Display and Move methods in the Tower class. These methods will handle the logic of the game and output. Again, all the code in the first block of code below has been provided (although some minor changes may be necessary).
My question is about the Display method. I am not sure how to display the contents of a stack when the stack is a member of an array. I have printed the contents of stacks before (see the second block of code below), but never from a stack that is an element of an array. The way I have displayed stack contents in the past does not seem to work with stacks that are also array elements themselves. What is the best way to display the contents of the three stacks representing the pegs? And again, I am not yet concerned with the Move method and the code for controlling the game. I will address that when I get the display method worked out.
public class Stack
{
private int Pegs; // holds the stack data
private int size; // holds the size allocated
private int top = -1; // holds the index of the top
// element, or -1 if none
public Stack()
{
size = 5;
Pegs = new int[size];
}
public Stack(int size)
{
this.size = size;
Pegs = new int[size];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return top == size - 1;
}
public void Push(int i)
{
if (IsFull())
throw new ApplicationException
("Stack full -- cannot push");
else
Pegs[++top] = i;
}
public int Pop()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- cannot pop");
else
return Pegs[top--];
}
public int Top()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- top undefined");
else
return Pegs[top];
}
}
class StackEmptyException : ApplicationException
{
public StackEmptyException(String message) : base(message)
{
}
}
class Tower
{
private Stack Pegs;
public Tower()
{
Pegs = new Stack[4];
Pegs[1] = new Stack(5);
Pegs[2] = new Stack(5);
Pegs[3] = new Stack(5);
Pegs[1].Push(5);
Pegs[1].Push(4);
Pegs[1].Push(3);
Pegs[1].Push(2);
Pegs[1].Push(1);
}
public void Display()
{
}
public void Move(int NumberToMove, int FromPeg, int ToPeg, int AuxPeg)
{
}
static void Main(string args)
{
Tower TowerOfHanoi = new Tower();
// Show me the Pegs before you do the move
TowerOfHanoi.Display();
// Move 5 disks from Peg 1 to Peg 2 Using Peg 3 as temporary space
//TowerOfHanoi.Move(5, 1, 2, 3);
// Show me the Pegs after you do the move
TowerOfHanoi.Display();
Console.ReadLine();
}
}
Previous code I have used to print a stack's contents:
int display = new int[dataStack.Length];
Console.WriteLine("The contents of the stack is: ");
for (int i = 0; i <= top; i++)
{
display[i] = dataStack[i];
Console.WriteLine(display[i]);
}
Console.WriteLine();
c# arrays collections stack towers-of-hanoi
add a comment |
I am working on an assignment that involves the classic towers of Hanoi game. But, my question has nothing to do with the game, its logic, or anything like that. I have been given much of the code for this problem's solution. You will see in the code below that there is a Stack class and a Tower class. The Stack class defines a bunch of methods for using a stack. The Tower class contains a tower method for creating an array of three stacks and filling the first stack. It is these three stacks that will act as the pegs in the game. My assignment is only to complete the Display and Move methods in the Tower class. These methods will handle the logic of the game and output. Again, all the code in the first block of code below has been provided (although some minor changes may be necessary).
My question is about the Display method. I am not sure how to display the contents of a stack when the stack is a member of an array. I have printed the contents of stacks before (see the second block of code below), but never from a stack that is an element of an array. The way I have displayed stack contents in the past does not seem to work with stacks that are also array elements themselves. What is the best way to display the contents of the three stacks representing the pegs? And again, I am not yet concerned with the Move method and the code for controlling the game. I will address that when I get the display method worked out.
public class Stack
{
private int Pegs; // holds the stack data
private int size; // holds the size allocated
private int top = -1; // holds the index of the top
// element, or -1 if none
public Stack()
{
size = 5;
Pegs = new int[size];
}
public Stack(int size)
{
this.size = size;
Pegs = new int[size];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return top == size - 1;
}
public void Push(int i)
{
if (IsFull())
throw new ApplicationException
("Stack full -- cannot push");
else
Pegs[++top] = i;
}
public int Pop()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- cannot pop");
else
return Pegs[top--];
}
public int Top()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- top undefined");
else
return Pegs[top];
}
}
class StackEmptyException : ApplicationException
{
public StackEmptyException(String message) : base(message)
{
}
}
class Tower
{
private Stack Pegs;
public Tower()
{
Pegs = new Stack[4];
Pegs[1] = new Stack(5);
Pegs[2] = new Stack(5);
Pegs[3] = new Stack(5);
Pegs[1].Push(5);
Pegs[1].Push(4);
Pegs[1].Push(3);
Pegs[1].Push(2);
Pegs[1].Push(1);
}
public void Display()
{
}
public void Move(int NumberToMove, int FromPeg, int ToPeg, int AuxPeg)
{
}
static void Main(string args)
{
Tower TowerOfHanoi = new Tower();
// Show me the Pegs before you do the move
TowerOfHanoi.Display();
// Move 5 disks from Peg 1 to Peg 2 Using Peg 3 as temporary space
//TowerOfHanoi.Move(5, 1, 2, 3);
// Show me the Pegs after you do the move
TowerOfHanoi.Display();
Console.ReadLine();
}
}
Previous code I have used to print a stack's contents:
int display = new int[dataStack.Length];
Console.WriteLine("The contents of the stack is: ");
for (int i = 0; i <= top; i++)
{
display[i] = dataStack[i];
Console.WriteLine(display[i]);
}
Console.WriteLine();
c# arrays collections stack towers-of-hanoi
add a comment |
I am working on an assignment that involves the classic towers of Hanoi game. But, my question has nothing to do with the game, its logic, or anything like that. I have been given much of the code for this problem's solution. You will see in the code below that there is a Stack class and a Tower class. The Stack class defines a bunch of methods for using a stack. The Tower class contains a tower method for creating an array of three stacks and filling the first stack. It is these three stacks that will act as the pegs in the game. My assignment is only to complete the Display and Move methods in the Tower class. These methods will handle the logic of the game and output. Again, all the code in the first block of code below has been provided (although some minor changes may be necessary).
My question is about the Display method. I am not sure how to display the contents of a stack when the stack is a member of an array. I have printed the contents of stacks before (see the second block of code below), but never from a stack that is an element of an array. The way I have displayed stack contents in the past does not seem to work with stacks that are also array elements themselves. What is the best way to display the contents of the three stacks representing the pegs? And again, I am not yet concerned with the Move method and the code for controlling the game. I will address that when I get the display method worked out.
public class Stack
{
private int Pegs; // holds the stack data
private int size; // holds the size allocated
private int top = -1; // holds the index of the top
// element, or -1 if none
public Stack()
{
size = 5;
Pegs = new int[size];
}
public Stack(int size)
{
this.size = size;
Pegs = new int[size];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return top == size - 1;
}
public void Push(int i)
{
if (IsFull())
throw new ApplicationException
("Stack full -- cannot push");
else
Pegs[++top] = i;
}
public int Pop()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- cannot pop");
else
return Pegs[top--];
}
public int Top()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- top undefined");
else
return Pegs[top];
}
}
class StackEmptyException : ApplicationException
{
public StackEmptyException(String message) : base(message)
{
}
}
class Tower
{
private Stack Pegs;
public Tower()
{
Pegs = new Stack[4];
Pegs[1] = new Stack(5);
Pegs[2] = new Stack(5);
Pegs[3] = new Stack(5);
Pegs[1].Push(5);
Pegs[1].Push(4);
Pegs[1].Push(3);
Pegs[1].Push(2);
Pegs[1].Push(1);
}
public void Display()
{
}
public void Move(int NumberToMove, int FromPeg, int ToPeg, int AuxPeg)
{
}
static void Main(string args)
{
Tower TowerOfHanoi = new Tower();
// Show me the Pegs before you do the move
TowerOfHanoi.Display();
// Move 5 disks from Peg 1 to Peg 2 Using Peg 3 as temporary space
//TowerOfHanoi.Move(5, 1, 2, 3);
// Show me the Pegs after you do the move
TowerOfHanoi.Display();
Console.ReadLine();
}
}
Previous code I have used to print a stack's contents:
int display = new int[dataStack.Length];
Console.WriteLine("The contents of the stack is: ");
for (int i = 0; i <= top; i++)
{
display[i] = dataStack[i];
Console.WriteLine(display[i]);
}
Console.WriteLine();
c# arrays collections stack towers-of-hanoi
I am working on an assignment that involves the classic towers of Hanoi game. But, my question has nothing to do with the game, its logic, or anything like that. I have been given much of the code for this problem's solution. You will see in the code below that there is a Stack class and a Tower class. The Stack class defines a bunch of methods for using a stack. The Tower class contains a tower method for creating an array of three stacks and filling the first stack. It is these three stacks that will act as the pegs in the game. My assignment is only to complete the Display and Move methods in the Tower class. These methods will handle the logic of the game and output. Again, all the code in the first block of code below has been provided (although some minor changes may be necessary).
My question is about the Display method. I am not sure how to display the contents of a stack when the stack is a member of an array. I have printed the contents of stacks before (see the second block of code below), but never from a stack that is an element of an array. The way I have displayed stack contents in the past does not seem to work with stacks that are also array elements themselves. What is the best way to display the contents of the three stacks representing the pegs? And again, I am not yet concerned with the Move method and the code for controlling the game. I will address that when I get the display method worked out.
public class Stack
{
private int Pegs; // holds the stack data
private int size; // holds the size allocated
private int top = -1; // holds the index of the top
// element, or -1 if none
public Stack()
{
size = 5;
Pegs = new int[size];
}
public Stack(int size)
{
this.size = size;
Pegs = new int[size];
}
public bool IsEmpty()
{
return top == -1;
}
public bool IsFull()
{
return top == size - 1;
}
public void Push(int i)
{
if (IsFull())
throw new ApplicationException
("Stack full -- cannot push");
else
Pegs[++top] = i;
}
public int Pop()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- cannot pop");
else
return Pegs[top--];
}
public int Top()
{
if (IsEmpty())
throw new StackEmptyException
("Stack empty -- top undefined");
else
return Pegs[top];
}
}
class StackEmptyException : ApplicationException
{
public StackEmptyException(String message) : base(message)
{
}
}
class Tower
{
private Stack Pegs;
public Tower()
{
Pegs = new Stack[4];
Pegs[1] = new Stack(5);
Pegs[2] = new Stack(5);
Pegs[3] = new Stack(5);
Pegs[1].Push(5);
Pegs[1].Push(4);
Pegs[1].Push(3);
Pegs[1].Push(2);
Pegs[1].Push(1);
}
public void Display()
{
}
public void Move(int NumberToMove, int FromPeg, int ToPeg, int AuxPeg)
{
}
static void Main(string args)
{
Tower TowerOfHanoi = new Tower();
// Show me the Pegs before you do the move
TowerOfHanoi.Display();
// Move 5 disks from Peg 1 to Peg 2 Using Peg 3 as temporary space
//TowerOfHanoi.Move(5, 1, 2, 3);
// Show me the Pegs after you do the move
TowerOfHanoi.Display();
Console.ReadLine();
}
}
Previous code I have used to print a stack's contents:
int display = new int[dataStack.Length];
Console.WriteLine("The contents of the stack is: ");
for (int i = 0; i <= top; i++)
{
display[i] = dataStack[i];
Console.WriteLine(display[i]);
}
Console.WriteLine();
c# arrays collections stack towers-of-hanoi
c# arrays collections stack towers-of-hanoi
asked Nov 13 '18 at 2:08
ZL4892ZL4892
495
495
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It took me a little to understand the logic but I think it all make sense now.
In this scenario you need to understand the importance of arrays and indexes.
In C# ( like many others languages) arrays are 0 based. Cannot stress enough how important this is. Please have a look at the documentation here.
That being said lets understand the structure:
In your Tower class you have an array of Pegs.
Pegs is a class that is responsible to handle all the operations for an array of int.
private int pegs; // holds the stack data
I have changed the caption form Pegs to pegs (Camel case it is important you will see later). Now your Stack class need to expose the pegs so they can be used in the Display method in the Tower class.
In your Stack class expose pegs:
public int Pegs()
{
return pegs;
}
Lets assume you have created the following:
Pegs = new Stack[4];
Pegs[0] = new Stack(5);
Pegs[1] = new Stack(5);
Pegs[0].Push(5);
Pegs[0].Push(4);
Pegs[0].Push(3);
Pegs[0].Push(2);
Pegs[0].Push(1);
Pegs[1].Push(15);
Pegs[1].Push(14);
Pegs[1].Push(13);
Pegs[1].Push(12);
Pegs[1].Push(11);
In your Display method you will do this:
public void Display()
{
int lenght = Pegs.Length - 1;
Console.WriteLine("The contents of the stack is: ");
//Per each stack Pegs
for (int i = 0; i < lenght; i++)
{
if (Pegs[i] != null)
{
//display all the sub-stacks
int currentPegLenght = Pegs[i].Pegs().Length;
for (int j = 0; j < currentPegLenght; j++)
{
Console.WriteLine(Pegs[i].Pegs()[j]);
}
}
}
Console.WriteLine();
}
The Display method will have a for-loop nested in another for-loop. The outer for-loop will retrieve the main Pegs. The inner for-loop will get each sub-stack per each main Peg.
Hope it helps.
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272750%2fc-sharp-how-to-display-the-contents-of-a-stack-when-the-stack-is-part-of-an-arra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It took me a little to understand the logic but I think it all make sense now.
In this scenario you need to understand the importance of arrays and indexes.
In C# ( like many others languages) arrays are 0 based. Cannot stress enough how important this is. Please have a look at the documentation here.
That being said lets understand the structure:
In your Tower class you have an array of Pegs.
Pegs is a class that is responsible to handle all the operations for an array of int.
private int pegs; // holds the stack data
I have changed the caption form Pegs to pegs (Camel case it is important you will see later). Now your Stack class need to expose the pegs so they can be used in the Display method in the Tower class.
In your Stack class expose pegs:
public int Pegs()
{
return pegs;
}
Lets assume you have created the following:
Pegs = new Stack[4];
Pegs[0] = new Stack(5);
Pegs[1] = new Stack(5);
Pegs[0].Push(5);
Pegs[0].Push(4);
Pegs[0].Push(3);
Pegs[0].Push(2);
Pegs[0].Push(1);
Pegs[1].Push(15);
Pegs[1].Push(14);
Pegs[1].Push(13);
Pegs[1].Push(12);
Pegs[1].Push(11);
In your Display method you will do this:
public void Display()
{
int lenght = Pegs.Length - 1;
Console.WriteLine("The contents of the stack is: ");
//Per each stack Pegs
for (int i = 0; i < lenght; i++)
{
if (Pegs[i] != null)
{
//display all the sub-stacks
int currentPegLenght = Pegs[i].Pegs().Length;
for (int j = 0; j < currentPegLenght; j++)
{
Console.WriteLine(Pegs[i].Pegs()[j]);
}
}
}
Console.WriteLine();
}
The Display method will have a for-loop nested in another for-loop. The outer for-loop will retrieve the main Pegs. The inner for-loop will get each sub-stack per each main Peg.
Hope it helps.
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
add a comment |
It took me a little to understand the logic but I think it all make sense now.
In this scenario you need to understand the importance of arrays and indexes.
In C# ( like many others languages) arrays are 0 based. Cannot stress enough how important this is. Please have a look at the documentation here.
That being said lets understand the structure:
In your Tower class you have an array of Pegs.
Pegs is a class that is responsible to handle all the operations for an array of int.
private int pegs; // holds the stack data
I have changed the caption form Pegs to pegs (Camel case it is important you will see later). Now your Stack class need to expose the pegs so they can be used in the Display method in the Tower class.
In your Stack class expose pegs:
public int Pegs()
{
return pegs;
}
Lets assume you have created the following:
Pegs = new Stack[4];
Pegs[0] = new Stack(5);
Pegs[1] = new Stack(5);
Pegs[0].Push(5);
Pegs[0].Push(4);
Pegs[0].Push(3);
Pegs[0].Push(2);
Pegs[0].Push(1);
Pegs[1].Push(15);
Pegs[1].Push(14);
Pegs[1].Push(13);
Pegs[1].Push(12);
Pegs[1].Push(11);
In your Display method you will do this:
public void Display()
{
int lenght = Pegs.Length - 1;
Console.WriteLine("The contents of the stack is: ");
//Per each stack Pegs
for (int i = 0; i < lenght; i++)
{
if (Pegs[i] != null)
{
//display all the sub-stacks
int currentPegLenght = Pegs[i].Pegs().Length;
for (int j = 0; j < currentPegLenght; j++)
{
Console.WriteLine(Pegs[i].Pegs()[j]);
}
}
}
Console.WriteLine();
}
The Display method will have a for-loop nested in another for-loop. The outer for-loop will retrieve the main Pegs. The inner for-loop will get each sub-stack per each main Peg.
Hope it helps.
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
add a comment |
It took me a little to understand the logic but I think it all make sense now.
In this scenario you need to understand the importance of arrays and indexes.
In C# ( like many others languages) arrays are 0 based. Cannot stress enough how important this is. Please have a look at the documentation here.
That being said lets understand the structure:
In your Tower class you have an array of Pegs.
Pegs is a class that is responsible to handle all the operations for an array of int.
private int pegs; // holds the stack data
I have changed the caption form Pegs to pegs (Camel case it is important you will see later). Now your Stack class need to expose the pegs so they can be used in the Display method in the Tower class.
In your Stack class expose pegs:
public int Pegs()
{
return pegs;
}
Lets assume you have created the following:
Pegs = new Stack[4];
Pegs[0] = new Stack(5);
Pegs[1] = new Stack(5);
Pegs[0].Push(5);
Pegs[0].Push(4);
Pegs[0].Push(3);
Pegs[0].Push(2);
Pegs[0].Push(1);
Pegs[1].Push(15);
Pegs[1].Push(14);
Pegs[1].Push(13);
Pegs[1].Push(12);
Pegs[1].Push(11);
In your Display method you will do this:
public void Display()
{
int lenght = Pegs.Length - 1;
Console.WriteLine("The contents of the stack is: ");
//Per each stack Pegs
for (int i = 0; i < lenght; i++)
{
if (Pegs[i] != null)
{
//display all the sub-stacks
int currentPegLenght = Pegs[i].Pegs().Length;
for (int j = 0; j < currentPegLenght; j++)
{
Console.WriteLine(Pegs[i].Pegs()[j]);
}
}
}
Console.WriteLine();
}
The Display method will have a for-loop nested in another for-loop. The outer for-loop will retrieve the main Pegs. The inner for-loop will get each sub-stack per each main Peg.
Hope it helps.
It took me a little to understand the logic but I think it all make sense now.
In this scenario you need to understand the importance of arrays and indexes.
In C# ( like many others languages) arrays are 0 based. Cannot stress enough how important this is. Please have a look at the documentation here.
That being said lets understand the structure:
In your Tower class you have an array of Pegs.
Pegs is a class that is responsible to handle all the operations for an array of int.
private int pegs; // holds the stack data
I have changed the caption form Pegs to pegs (Camel case it is important you will see later). Now your Stack class need to expose the pegs so they can be used in the Display method in the Tower class.
In your Stack class expose pegs:
public int Pegs()
{
return pegs;
}
Lets assume you have created the following:
Pegs = new Stack[4];
Pegs[0] = new Stack(5);
Pegs[1] = new Stack(5);
Pegs[0].Push(5);
Pegs[0].Push(4);
Pegs[0].Push(3);
Pegs[0].Push(2);
Pegs[0].Push(1);
Pegs[1].Push(15);
Pegs[1].Push(14);
Pegs[1].Push(13);
Pegs[1].Push(12);
Pegs[1].Push(11);
In your Display method you will do this:
public void Display()
{
int lenght = Pegs.Length - 1;
Console.WriteLine("The contents of the stack is: ");
//Per each stack Pegs
for (int i = 0; i < lenght; i++)
{
if (Pegs[i] != null)
{
//display all the sub-stacks
int currentPegLenght = Pegs[i].Pegs().Length;
for (int j = 0; j < currentPegLenght; j++)
{
Console.WriteLine(Pegs[i].Pegs()[j]);
}
}
}
Console.WriteLine();
}
The Display method will have a for-loop nested in another for-loop. The outer for-loop will retrieve the main Pegs. The inner for-loop will get each sub-stack per each main Peg.
Hope it helps.
answered Nov 13 '18 at 9:49
Alex LeoAlex Leo
551213
551213
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
add a comment |
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
Thank you this was helpful. The only 2 things I am unsure of is that only 2 stacks are displayed, only one will have non-zero values, but all 3 should be shown. Also, the stacks displayed have 10 items (it displays 5 4 3 2 1 0 0 0 0 0). I'm not sure how the stack has that many items, given it is declared with a size of 5.
– ZL4892
Nov 13 '18 at 21:45
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
In your main you are calling TowerOfHanoi.Display(); twice. Also when you create an object as Pegs[1] = new Stack(5); but don't push any values, all the pegs will have a default values assigned (0). If this helps please mark the question as answered. Thanks
– Alex Leo
Nov 14 '18 at 8:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272750%2fc-sharp-how-to-display-the-contents-of-a-stack-when-the-stack-is-part-of-an-arra%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown