How to iterate through all the elements of the n-size int array in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacekr
    New Member
    • Jun 2010
    • 3

    How to iterate through all the elements of the n-size int array in C#?

    hi,

    What I want to do, is to print/generate n-size int array like this:

    Code:
    int [3] array;
    
    0 0 0
    0 0 1
    0 0 2
    ...
    9 9 9
    Code:
    int [10] array;
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 2
    ...
    9 9 9 9 9 9 9 9 9 9
    The size of the array should not be hardcoded, it should be set during the program execution.
    How could it be solved?

    Best Regards
    Jack
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by jacekr
    hi,

    What I want to do, is to print/generate n-size int array like this:

    Code:
    int [3] array;
    
    0 0 0
    0 0 1
    0 0 2
    ...
    9 9 9
    Code:
    int [10] array;
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 2
    ...
    9 9 9 9 9 9 9 9 9 9
    The size of the array should not be hardcoded, it should be set during the program execution.
    How could it be solved?

    Best Regards
    Jack
    Your definition of the array doesn't meet the type of value you require..

    Can you explain your exact problem

    Comment

    • jacekr
      New Member
      • Jun 2010
      • 3

      #3
      I have such program:

      Code:
      namespace prog
      {
          class Program
          {
              static void Main(string[] args)
              {
      
                  int[] array = new int[Convert.ToInt32(args[0])];
                  
                  foreach (int i in array)
                  {
                      Console.Write(i);
                  }
              
              }
          }
      }
      so, during the execution I can set the int array size as argument:

      C:\prog>prog.ex e 3
      000

      C:\prog>prog.ex e 10
      0000000000


      Now I would like to set my array's elements in some loop and print it on the screen in this way:

      C:\prog>prog.ex e 3
      000
      001
      002
      003
      ...
      999

      the question is, how should I iterate the array?

      Comment

      • damoda
        New Member
        • Jun 2010
        • 1

        #4
        try this:

        int n = 3;
        int[] array = new int[n];
        for(int i = 0;i< Math.Pow(10,n); i++)
        {
        int value = i;
        for(int j = n - 1;j>= 0;j--)
        {
        int help = value / (int)Math.Pow(1 0,j);
        array[j] = help;
        value -= help * (int)Math.Pow(1 0,j)
        Console.Write(a rray[j] + " ");
        }
        Console.WriteLi ne();
        }

        Comment

        • jacekr
          New Member
          • Jun 2010
          • 3

          #5
          what if n=2000 ?

          Code:
          Console.Write("-->"+Math.Pow(10, 2000));
          -->Infinity


          The solution should be more generic.

          Comment

          • ThatThatGuy
            Recognized Expert Contributor
            • Jul 2009
            • 453

            #6
            Originally posted by jacekr
            what if n=2000 ?

            Code:
            Console.Write("-->"+Math.Pow(10, 2000));
            -->Infinity


            The solution should be more generic.
            damoda's suggested code works well and suits your requirements...
            Anyways an input value of 2000 will certainly run into infinity...

            There's no option left

            Comment

            • Joseph Martell
              Recognized Expert New Member
              • Jan 2010
              • 198

              #7
              I created a recursive solution. It could probably be cleand up some and I am willing to bet that Linq could be used to clean up the array copying that I am doing, but I am not very good with Linq yet.

              This solution *should* handle an array of any size though. I have changed the array from an int[] to byte[] because no single element ever has to go above 10.

              Here is the class that actually does the work:

              Code:
                  class ArrayCounter
                  {
                      static public void createArray(int digits)
                      {
                          if (digits > 0)
                          {
                              byte[] myArray = new byte[digits];
                              for (int i = 0; i < myArray.Length; ++i)
                              {
                                  myArray[i] = 0;
                              }
              
                              bool overflow = false;
                              string printString = null;
              
                              do
                              {
                                  printString = "";
                                  for (int i = 0; i < myArray.Length; ++i)
                                  {
                                      printString += myArray[i].ToString() + " ";
                                  }
                                  Console.WriteLine(printString);
                                  incrementArray(ref myArray, ref overflow);
                              } while (!overflow);
                          }
                      }
              
                      static private void incrementArray(ref byte[] digitArray, ref bool overflow)
                      {
                          overflow = false;
                          int x = digitArray.Length - 1;
              
                          if (digitArray[x] < 9)
                          {
                              digitArray[x]++;
                              overflow = false;
                          }
                          else
                          {
                              digitArray[x] = 0;
                              overflow = true;
                          }
              
                          if ((overflow) && (x > 0))
                          {
                              byte[] lessDigitArray = new byte[digitArray.Length - 1];
                              for (int i = 0; i < lessDigitArray.Length; ++i)
                              {
                                  lessDigitArray[i] = digitArray[i];
                              }
                              incrementArray(ref lessDigitArray, ref overflow);
                              for (int i = 0; i < lessDigitArray.Length; ++i)
                              {
                                  digitArray[i] = lessDigitArray[i];
                              }
                          }
                      }
                  }
              To use it, you simply do something like the following:

              Code:
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          Console.WriteLine("CreateArray with digits = 3");
                          ArrayCounter.createArray(3);
                          Console.ReadKey();
                      }
              
                  }
              Last edited by Joseph Martell; Jun 23 '10, 03:53 PM. Reason: This version is more efficient than my original version: less recursive calls.

              Comment

              Working...