Home TechnologyProgramming Subscribe to RSS

I Have A Problem With The Output Using Arrays With Loops. Can You Help?

Here is an example of how to print all the values in an array of 10 elements using a for loop. Int[] arr = new int[10]; for (int x = 0; x < 10; x++) Console.WriteLine(arr[x]); but when I run this code in c# it displays 9 zeroes...How come? Could you explain to me where the mistakes are and what would be the output for this code?

Answer Question

1 Answer - Sort by: Date | Rating

    In this example you are just making an array not assigning a value to it. So for this you can run the following code


    int[] arr = new int[10];

    for (int x = 0; x < 10; x++)

    {
       arr[x]=x; // this will assign the value in the array
       Console.WriteLine(arr[x]);
      }

    or check out the following site.

    learn-programming

    1 0

    Xarsh 

    answered 1 year ago

      More

      More

         
         

        Ask a Question via Twitter

        Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

        Blurtit Store

        Get T-shirts, hoodies, caps and more at the Blurtit store

        Blurtit International