/* JaggedArray.java by Craig Persiko
   Example program for CS 111A
   Shows how a 2-D array can be jagged: have different numbers of columns for each row.
   Inputs test scores for courses - each course can have a different number of tests.
*/

import java.util.Scanner;

public class JaggedArray
{
  public static void main(String[] args)
  {
    Scanner s = new Scanner(System.in);
    int numCourses, numTests;
    double[][] testScores;
	 
    System.out.print("How many courses are you taking? ");
    numCourses = s.nextInt();
	 
    testScores = new double[numCourses][]; // allocate an array of references to 1-D arrays (rows)
	 
    for(int i = 0; i < numCourses; i++)
    {
      System.out.print("How many tests are in course number " 
                       + (i+1) + "? ");
      numTests = s.nextInt();
      testScores[i] = new double[numTests];  // allocate this row: a 1-D array of doubles
    }
	 
    for(int courseId=0; courseId < testScores.length; courseId++)
      for(int testId=0; testId < testScores[courseId].length; testId++)
        {
          System.out.print("What was your grade on test " 
                           + (testId+1) + " for course "
                           + (courseId+1) + "? ");
          testScores[courseId][testId] = s.nextDouble();
        }							 
  }
}

/* Sample Output:

-bash-3.2$ java JaggedArray
How many courses are you taking? 5
How many tests are in course number 1? 4
How many tests are in course number 2? 2
How many tests are in course number 3? 1
How many tests are in course number 4? 3
How many tests are in course number 5? 2
What was your grade on test 1 for course 1? 80
What was your grade on test 2 for course 1? 88
What was your grade on test 3 for course 1? 92
What was your grade on test 4 for course 1? 79
What was your grade on test 1 for course 2? 78
What was your grade on test 2 for course 2? 77
What was your grade on test 1 for course 3? 81
What was your grade on test 1 for course 4? 69
What was your grade on test 2 for course 4? 75
What was your grade on test 3 for course 4? 79
What was your grade on test 1 for course 5? 90
What was your grade on test 2 for course 5? 95

*/



syntax highlighted by Code2HTML, v. 0.9