Hi guys, as you can see i have a problem with the array , i don t know how to put every remainder in the
array and then loop it to get the reverse order, it seems that the array is being overwrited every time
the while loop goes and i don t know how to handle this.

        int dec_num;
        int remainder;
        int result;
        int n = 0;
        int[] Storage = new int[10];
        Console.WriteLine("Unesite broj koji zelite pretvoriti");
        dec_num = int.Parse(Console.ReadLine());
        while (dec_num > 0)
        {
            result = dec_num / 2;
            remainder = dec_num % 2;
            Storage[n] = remainder;
            dec_num = dec_num / 2;


         Console.WriteLine("The Result is " + result + "" + " and the remainder " + remainder);
        }


        for (int i = Storage[0]; i < Storage.Length; i++)
            Console.WriteLine(Storage[n]);


            Console.ReadLine();

When do you increment n ?

yes , i forgot to do that but still with incrementation i m not able to get my array fixed

Can you post your semi-fixed code?

A few things,

First, do you have to do the conversion yourself? The .NET library has a method that will do this for you.

Second, do you have to use an array? If you want something in the reverse order that you generate it, a Stack is the data structure you'd want to use.

Third, you don't want to use a while loop, you want a do/while loop (what happens if the input is zero in your while loop).

Fourth, instead of counting from zero to array size - 1, try counting down from array size - 1 to zero.

And Fifth, Array has a method to reverse an array, called Reverse.

i m aware that the library can do the dec-bin and the reversing but i don t want to use it also i see that it would be better to use a
do while() loop but this time i want to stick with the while loop.Obviously i forgot to put the incrementation of the array but stil with
doing it a dont get good values in my foreach loop that folows the code.Can anyone put a code or something that i need to change?

while (dec_num > 0)
             {
                int n=0;
                result = dec_num / 2;
                remainder = dec_num % 2;
                Storage[n] = remainder;
                n++;
                dec_num = dec_num / 2;

the incrementation doesn t do it , why?

Dude(castajiz) I think I fixed you problem...I did a quick fix for you because you put in the effort.
You were close.
Your mistakes were:
-In the last forloop you gave 'i' the wrong value, it was supposed to be Storage.Length the you decrement until zero.
-You did not increment n.

Heres your fixed code.

        int dec_num;
        int remainder;
        int result;
        int n = 0;
        int[] Storage = new int[10];
        Console.WriteLine("Unesite broj koji zelite pretvoriti");
        dec_num = int.Parse(Console.ReadLine());
        int num = dec_num;
        int size = 0;

        //GET THE SIZE OFD THE ARRAY THE SIZE WILL BE AS BIG AS THE NUMBER OF DIVISIONS
        while(num>0)
        {
            num = num/ 2;
            size++;
        }

        //SET THE SIZE
        int [] binaryarray = new int[size];

            while (dec_num > 0)
            {
               /* result = dec_num / 2;
                remainder = dec_num % 2;
                Storage[n] = remainder;
                dec_num = dec_num / 2;
                Console.WriteLine("The Result is " + result + "" + " and the remainder " + remainder);*/

                //Get the remainder of dividing dec_num by 2
                remainder = dec_num % 2;
                //Divide dec_num and reduce it
                dec_num = dec_num / 2;

                binaryarray[n] = remainder;
                n++;
            }

            for (int i = binaryarray.Length - 1; i >= 0; i--)
                Console.WriteLine(binaryarray[i]);
        Console.ReadLine();

Sorry How do I Post code, I dont get the new editor. I tried putting it in blocks but it didn't work.
int dec_num;
int remainder;
int result;
int n = 0;
int[] Storage = new int[10];
Console.WriteLine("Unesite broj koji zelite pretvoriti");
dec_num = int.Parse(Console.ReadLine());
int num = dec_num;
int size = 0;

        //GET THE SIZE OFD THE ARRAY THE SIZE WILL BE AS BIG AS THE NUMBER OF DIVISIONS
        while(num>0)
        {
            num = num/ 2;
            size++;
        }

        //SET THE SIZE
        int [] binaryarray = new int[size];

            while (dec_num > 0)
            {
               /* result = dec_num / 2;
                remainder = dec_num % 2;
                Storage[n] = remainder;
                dec_num = dec_num / 2;
                Console.WriteLine("The Result is " + result + "" + " and the remainder " + remainder);*/

                //Get the remainder of dividing dec_num by 2
                remainder = dec_num % 2;
                //Divide dec_num and reduce it
                dec_num = dec_num / 2;

                binaryarray[n] = remainder;
                n++;
            }

            for (int i = binaryarray.Length - 1; i >= 0; i--)
                Console.WriteLine(binaryarray[i]);
        Console.ReadLine();

Well I give up...I indented five times and it didn't work.

But I hope I have been of great help.

tnx that is very well done :). Now i have a much more clearer view on arrays .

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.