-2

I have declared an integer array in which i am storing the factors of a number.....I want that array to print the number in it's elements, but I have failed too; Is there a way to access the array and it's elements outside the loop in which they are declared?

class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter number : ");
            int num = Convert.ToInt32(Console.ReadLine());


            for (int i = 1; i <= num; i++) 
            {
                int[] factor = new int[i];

                if (num % i == 0) 
                {
                    Console.WriteLine("Number going in i: " + i);
                    factor[i - 1] = i;
                };
            }


            //Not working
            for (int i = 0; i < factor.length; i++) 
            {
                Console.WriteLine(factor[i]);
            }

        }
    }
aliadiere
  • 65
  • 1
  • 1
  • 10

3 Answers3

3

The factor in your first for loop only lives in the scope of the for loop. Your first loop also overwrites the array every iteration.

You need to declare and initialize it in the parent scope.

int[] factor = new int[num];

for (int i = 1; i <= num; i++) 
{
    factor[i] ...
}

for (int i = 1; i <= factor.length; i++) 
{
    factor[i] ...
}

See also for example 3.7 Scopes (C#), confused with the scope in c#.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • @JensB that should have been a comment, not an edit. – CodeCaster Aug 26 '14 at 11:08
  • Hmm, I am new to programming pardon me If I am being stupid but if i'd write: int[] factor = new int[num]; and num = 20 lets say for example, wouldn't that create 20 elements in an array.... – aliadiere Aug 26 '14 at 11:10
  • @aliadiere It would create an array with 20 elements, initialized to their defaults. Isn't that what you want? – CodeCaster Aug 26 '14 at 11:10
  • Tried out and worked, thanks alot...but there are a lot of 0's too stored in the array.....Is there a way through which only factors could be stored in array, when it's in the loop; only factors were being stored... – aliadiere Aug 26 '14 at 11:16
  • 1
    @aliadiere sounds like you need a list, not an array. Or keep a counter which you increase each time you found a factor and use `factor[counter] = ...` when you find a match. Then don't use `i <= factor.length` in your second loop but use `i <= counter`. – CodeCaster Aug 26 '14 at 11:17
0
class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Enter number : ");
        int num = Convert.ToInt32(Console.ReadLine());

        int[] factor ; // <-- out of for loop

        for (int i = 1; i <= num; i++) 
        {
           factor  = new int[i];

            if (num % i == 0) 
            {
                Console.WriteLine("Number going in i: " + i);
                factor[i - 1] = i;
            };
        }


        //Not working
        for (int i = 0; i < factor.length; i++) 
        {
            Console.WriteLine(factor[i]);
        }

    }
}
bob-cac
  • 1,272
  • 2
  • 17
  • 35
Edi G.
  • 2,432
  • 7
  • 24
  • 33
0
class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter number : ");
            int num = Convert.ToInt32(Console.ReadLine());
             int[] factor ;
            for (int i = 1; i <= num; i++) 
            {
                factor = new int[i];

                if (num % i == 0) 
                {
                    Console.WriteLine("Number going in i: " + i);
                    factor[i - 1] = i;
                };
            }


            //Not working
            for (int i = 0; i < factor.length; i++) 
            {
                Console.WriteLine(factor[i]);
            }

        }
    }

See this link about scopes

bob-cac
  • 1,272
  • 2
  • 17
  • 35