what does an array contain when it is declared?
For example, when i declare an int array numbers like:
int[] numbers=new int[5];
now without intializing any values, i tried to print that array.So it showed me that all elements inside that array were 0.
Now if 0 means empty, then if i assign some random values to that arrays, and then i assign
say for eg, number[3]=0.
does this mean that that element is empty or it means that it is simply assigned 0 value.

Uninitialized arrays are bitwise zeroed..The values for a uninitialized int array are zero.

Uninitialized arrays are bitwise zeroed..The values for a an uninitialized int array are zero.

then how can we make a particular element at index i of array,contain nothing.The reason i am asking this is because i am implementing an stack program using arrays.
There is no problem i n push operation , but when i perform the pop operation then how to delete the top element in an array, should i assign it 0 value.
but if i assign it a 0 value then i don't think that it will delete that element, so what should be my approach for this?

int array cannot be empty. Empty is for string. Int can have only numbers. So when you initialize a new integer array all indexes set to zero This is 0 and not empty.

int array cannot be empty. Empty is for string. Int can have only numbers. So when you initialize a new integer array all indexes set to zero This is 0 and not empty.

so, how can we perform the pop operation in an int array.
So should i assign 0 value to the remaining elements when i perform an pop operation.

pop operation? What is that?

pop operation? What is that?

push and pop opeartions of an stack.
Push opearation= insert elements in an stack array.
pop operation= remove the topmost element in an array.here is the code that runs if i assign 0 value to all the remaining elements.

using System;
class stack12
{
    int tos;
    int[] stack;
    public void assignsize(int size)
    {
        stack = new int[size];
        tos = 0;
    }
    public void push(int no)
    {
        if (tos == stack.Length)
        {
            Console.WriteLine("the stack is full");
            return;
        }
        stack[tos] = no;
        tos++;
    }
    public void pop()
    {
        if (tos == 0)
        {
            Console.WriteLine("the stack is empty");
            return;
        }
        for (int i = tos; i < stack.Length; i++)
            stack[i] = 0;
        tos--;
    }
    public void display()
    {
        Console.WriteLine("\nthe stack now contains");
        for (int i = 0; i < stack.Length; i++)
            Console.Write(" "+stack[i]);
    }
}
class stackdemo
{
    static void Main()
    {
        stack12 obj1 = new stack12();
        int size = 5;
        obj1.assignsize(size);
        obj1.push(3);
        obj1.display();
        obj1.push(5);
        obj1.display();
        obj1.push(8);
        obj1.display();
        obj1.push(6);
        obj1.display();
        obj1.push(9);
        obj1.display();
        obj1.push(1);
        obj1.display();
        obj1.pop();
        obj1.display();
        obj1.pop();
        obj1.display();
    }  
}

So should i assign 0 value to the remaining elements when i perform an pop operation.

Unnecessary. The tos field is what determines where the top of the stack is, and when you push to a spot that's previously been populated, just overwrite the value.

However, if you really did need an "empty" integer (which you don't in this case), you could store it as a nullable type.

Unnecessary. The tos field is what determines where the top of the stack is, and when you push to a spot that's previously been populated, just overwrite the value.

However, if you really did need an "empty" integer (which you don't in this case), you could store it as a nullable type.

yes i know tos determines the top of stack and i want an empty integer but i am not able to make that element empty, that's why i assigned 0 values to remaining array. Could you tell me how to make any element empty using nullable type?

yes i know tos determines the top of stack and i want an empty integer but i am not able to make that element empty, that's why i assigned 0 values to remaining array. Could you tell me how to make any element empty using nullable type?

I don't think you understand. You don't need to make that element empty. I suspect you think you need to make it empty because of the display() method, which is wrong. You should be using tos instead of stack.Length:

public void display()
{
    Console.WriteLine("\nthe stack now contains");
    for (int i = 0; i < tos; i++)
        Console.Write(" "+stack[i]);
}

This way only the valid contents of the stack are printed. The lesson in this is to fix the real problem, not try to cover it up.

I don't think you understand. You don't need to make that element empty. I suspect you think you need to make it empty because of the display() method, which is wrong. You should be using tos instead of stack.Length:

public void display()
{
    Console.WriteLine("\nthe stack now contains");
    for (int i = 0; i < tos; i++)
        Console.Write(" "+stack[i]);
}

This way only the valid contents of the stack are printed. The lesson in this is to fix the real problem, not try to cover it up.

ok i understood, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.