It keeps telling me there is an error on line 22 when I run the program. Does anyone see the problem. My goal was to create a jagged array where the first block had 3 grades, the second block had 2 grades, and the third block had 4 grades.

using System;

public class Grades
{
   public static void Main(string [] args)
   {
      int[][] grades;
      grades = new int[3][];
      grades[0] = new int[3];
      grades[1] = new int[2];
      grades[2] = new int[4];


      for (int i = 0; i < grades.GetLength(0); i++)
      {
         Console.WriteLine("Please input grade for first course followed by enter: ");
         grades[0][i] = Convert.ToInt32(Console.ReadLine());
      }

      Console.WriteLine();

      for (int i = 0; i < grades.GetLength(1); i++)
      {
         Console.WriteLine("Please input grade for second course followed by enter: ");
         grades[1][i] = Convert.ToInt32(Console.ReadLine());
      }


      for (int i = 0; i < grades.GetLength(2); i++)
      {
         Console.WriteLine("Please input grade for third course followed by enter: ");
         grades[2][i] = Convert.ToInt32(Console.ReadLine());
      }

      Console.Write("The grades for the first course are: {0}, {1}, and {2}", grades[0][0], grades[0][1], grades[0][2]);
      Console.WriteLine();
      Console.Write("The grades for the second course are: {0} and {1}", grades[1][0], grades[1][1], grades[1][2]);
      Console.WriteLine();
      Console.Write("The grades for the third course are: {0}, {1}, {2}, and {3}", grades[2][0], grades[2][1], grades[2][2], grades[2][3]);


      Console.ReadKey();
   } // end Main
} // end class Grades
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication14
{
    public class Grades
    {
        public static void Main(string[] args)
        {
            int[][] grades;
            grades = new int[3][];
            grades[0] = new int[3];
            grades[1] = new int[2];
            grades[2] = new int[4];
            for (int i = 0; i < grades[0].Length; i++)
            {
                Console.WriteLine("Please input grade for first course followed by enter: ");
                grades[0][i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine();
            for (int i = 0; i < grades[1].Length; i++)
            {
                Console.WriteLine("Please input grade for second course followed by enter: ");
                grades[1][i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int i = 0; i < grades[2].Length; i++)
            {
                Console.WriteLine("Please input grade for third course followed by enter: ");
                grades[2][i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.Write("The grades for the first course are: {0}, {1}, and {2}", grades[0][0], grades[0][1], grades[0][2]);
            Console.WriteLine();
            Console.Write("The grades for the second course are: {0} and {1}", grades[1][0], grades[1][1]);
            Console.WriteLine();
            Console.Write("The grades for the third course are: {0}, {1}, {2}, and {3}", grades[2][0], grades[2][1], grades[2][2], grades[2][3]);
            Console.ReadKey();
        } // end Main
    } // end class Grades
}

The reason for the issue is that your jagged array really only has 1 dimension (the first [] in the type specification). The second set of [] just tells it you want to hold array types in your array. So when you asked for the 2nd dimension of the array, there isn't one.

The proper way to get the internal array size would be

jaggedArrayName[0].GetLength(0);
jaggedArrayName[1].GetLength(0);
jaggedArrayName[2].GetLength(0);

And the sample code above is poorly written. Loops are your friend.

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.