C
C#2mo ago
Ho3in

stack

This is the form of the question I wrote : Write a program that reads n integers from the input, places them on a stack, then clears the numbers from the stack and outputs them. ! I want to keep this short and simple, without classes or objects. :vibin: ! But one more thing: use a question mark for the pop function, so that it doesn't throw an error when it returns null . code : class Program {
private int n; private int[] stack; private int top = -1; public Program(int size) { n = size; stack = new int[n]; } public void Push(int k) { if (top == n - 1) { Console.WriteLine("The stack is full."); return; } stack[++top] = k; } public int Pop() { if (top == -1) { Console.WriteLine("The stack is empty.");
return -1; } return stack[top--]; } static void Main() { Console.WriteLine("Enter the number of numbers:"); int n = int.Parse(Console.ReadLine()); Program p = new Program(n); for (int i = 0; i < n; i++) { Console.Write($"عدد {i + 1}: "); int num = int.Parse(Console.ReadLine()); p.Push(num); } Console.WriteLine("Numbers popped from the stack:"); while (p.top >= 0) { Console.WriteLine(p.Pop()); } } }
33 Replies
MODiX
MODiX2mo ago
Ho3in
You're right, but I wrote this myself. I just wanted to see how an expert would answer this question.
Quoted by
<@72080813948153856> from #database (click here)
React with ❌ to remove this embed.
mg
mg2mo ago
how would i do it? use a Stack<T> instead of writing the stack logic myself but that's probably not what your instructor would want you to do
Ho3in
Ho3inOP2mo ago
Not by presenting
mg
mg2mo ago
I want to keep this short and simple, without classes or objects
not possible. C# is an object oriented language. Program is a class typically you don't create instances of Program the right way, without using the existing Stack<T> class, would be to write a Stack class yourself to encapsulate the logic of creating the stack and pushing/popping then create an instance of that stack class, read each number and push it on the stack, and then pop them all off like you're doing i don't understand what you mean by this
Salman
Salman2mo ago
maybe if you really wanna shorten it you could've used a List<T> as a stack would be a lil weird
Ho3in
Ho3inOP2mo ago
Non, le but est le suivant : je veux simuler la pile en la présentant comme : int[] stack = new int[n];
mg
mg2mo ago
i don't speak french they need a stack
Ho3in
Ho3inOP2mo ago
No, the goal is this: I want to simulate the stack by presenting it as: int[] stack = new int[n];
Salman
Salman2mo ago
List<int> stack = new List<int>();

// Push items onto the stack
stack.Add(1);
stack.Add(2);
stack.Add(3);

// Pop items from the stack
while (stack.Count > 0)
{
int item = stack[^1];
stack.RemoveAt(stack.Count - 1);
Console.WriteLine(item); // Prints 3, 2, 1
}
List<int> stack = new List<int>();

// Push items onto the stack
stack.Add(1);
stack.Add(2);
stack.Add(3);

// Pop items from the stack
while (stack.Count > 0)
{
int item = stack[^1];
stack.RemoveAt(stack.Count - 1);
Console.WriteLine(item); // Prints 3, 2, 1
}
mg
mg2mo ago
you're not simulating a stack, you're implementing an actual stack
Salman
Salman2mo ago
are you allowed to use Lists or only arrays if you wanna simulate it ?
Ho3in
Ho3inOP2mo ago
My teacher wrote with a presentation and said to start with a presentation.
mg
mg2mo ago
if they're using anything from System.Collections.Generic they might as well be using Stack<T>, but i guess i'm making assumptions about what their instructor has made available
Salman
Salman2mo ago
yeah instructions are unclear
mg
mg2mo ago
i think the language barrier is too big for me to be able to help, as i'm not understanding what you mean. sorry
Ho3in
Ho3inOP2mo ago
np thanks
Salman
Salman2mo ago
are you an Arab? assumed that based on the print statement in your code
Pobiega
Pobiega2mo ago
I'd say you should implement your own Stack<T> class based on an a T[], with or without resizing.
Ho3in
Ho3inOP2mo ago
im Persian iran
Salman
Salman2mo ago
ah okay
Ho3in
Ho3inOP2mo ago
I haven't learned enough in class yet, so I needed a presentation.
Pobiega
Pobiega2mo ago
that doesnt mean anything in this context in english, I'm afraid
Salman
Salman2mo ago
what do you mean by presentation here ? like if you can elaborate further, then we could help you better
mg
mg2mo ago
maybe they mean it as in "appearance" i.e. the assignment is for them to write theyir own stack implementation
No description
mg
mg2mo ago
would explain the use of "simulate" too
Salman
Salman2mo ago
yeah ig
Ho3in
Ho3inOP2mo ago
The question given by the teacher is as follows: Question 1 Write a program that reads n integers from the input, then puts them on a stack, then clears the numbers in the stack and puts them on the output. By presenting Something like this but this seems to be used with a class or advanced, for example, statics before defining variables I don't want to be using System; class Program { static int[] stack; static int top = -1; static int n; // Push function to add an element to the stack static void Push(int k) { if (top < n - 1) { stack[++top] = k; } else { Console.WriteLine("The stack is full."); } } // Pop function to remove an element from the stack static int? Pop() { if (top == -1) { Console.WriteLine("The stack is empty."); return null; // Return null instead of throwing an error } return stack[top--]; } static void Main() { Console.WriteLine("Enter the number of numbers:"); n = int.Parse(Console.ReadLine()); stack = new int[n]; // Pushing numbers onto the stack for (int i = 0; i < n; i++) { Console.Write($"Enter number {i + 1}: "); int num = int.Parse(Console.ReadLine()); Push(num); } // Popping numbers from the stack and displaying them Console.WriteLine("Numbers popped from the stack:"); int? poppedNumber; while ((poppedNumber = Pop()) != null) { Console.WriteLine(poppedNumber); } } }
Salman
Salman2mo ago
Well if you dont want this then as mg said just use the Stack<T> and if something wrong ask your teacher for more clarification
Ho3in
Ho3inOP2mo ago
What is this? Is it a class?
Salman
Salman2mo ago
yes it's a predefined class
Stack<int> stack = new Stack<int>();

// Push items onto the stack
stack.Push(1);
stack.Push(2);
stack.Push(3);

// Pop items from the stack
while (stack.Count > 0)
{
int item = stack.Pop();
Console.WriteLine(item); // Prints 3, 2, 1
}
Stack<int> stack = new Stack<int>();

// Push items onto the stack
stack.Push(1);
stack.Push(2);
stack.Push(3);

// Pop items from the stack
while (stack.Count > 0)
{
int item = stack.Pop();
Console.WriteLine(item); // Prints 3, 2, 1
}
https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack?view=net-9.0 Better explanation https://www.programiz.com/csharp-programming/stack
Ho3in
Ho3inOP2mo ago
The code I saw from my professor was like the one I sent, but it didn't use the class you mentioned, nor did it use these statics in the first variable, and it was a little simpler. You can
Salman
Salman2mo ago
well without looking at your professor's code we can't compare that with anything

Did you find this page helpful?