jagged arrays problem! gives an error

Updated on June 11, 2018 in [C] Beginner
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on June 10, 2018

using System;

namespace jaggedarrays
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Creategrid();
        }

        public static void Creategrid()
        {
            int height = 5;
            int width = 5;
            int[][] grid = new int[height][width]; // error here!
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Console.Write(grid[i][j]);
                }
                Console.WriteLine();
            }
        }
    }
}

  • Liked by
Reply
1 on June 11, 2018

You should consider googling the issue prior to posting, I found This Resource within seconds. Although, being new to coding I imagine it’s difficult to know what to search for. So I understand if it proves difficult. 

public static void Main(string[] args)
 {
    Creategrid();    
    Console.ReadKey(); // Wait for key press to exit program.
 }
public static void Creategrid()
 {
    int height = 5;
     int width = 5;
     int[,] grid = new int[height,width];
    for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width; j++)
         {
             grid[i, j] = 1; // Fill array.
             Console.Write(grid[i,j]);
         }
     Console.WriteLine();
     }
 }
on June 11, 2018

The code you mentioned above is of multi-dimensional arrays , int[,] grid = new int[height,width]; //This is multidimensional arrays declaration. My question is for jagged arrays!!

Show more replies
  • Liked by
Reply
Cancel