mercredi 31 janvier 2018

Random Jagged Array issues

For a class I need to make a jagged array that randomly has anywhere between 3-15 lines with each of those lines randomly having anywhere between 1-20 numbers between 1-20 in them. Then I need to type out the array, type out the sum and average of each line and finally type out the sum and average of the entire array. This is what I made:

using System;
class zigzag
{
    class jagged
    {
        public int many;
        public int total;
        public double mo;
    }
    static void Main()
    {
        Random rnd = new Random();
        int N = rnd.Next(3, 16);
        int i, j;
        int[][] P = new int[N][];
        jagged[] hold = new jagged[N];
        for (i=0; i<N; i++)
        {
            hold[i].total = 0;
            hold[i].many =rnd.Next(1, 21);
            P[i] = new int[hold[i].many];
            for (j = 0; j < hold[i].many; i++)
            {
                P[i][j] = rnd.Next(10, 21);
                hold[i].total += P[i][j];
            }
            hold[i].mo = (double) hold[i].total / hold[i].many;
        }
        for (i=0; i<N; i++)
        {
            Console.Write(P[i][hold[i].many]);
            for (j=1; j<hold[i].many; j++)
            {
                Console.Write(",");
                Console.Write(P[i][j]);
            }
            Console.WriteLine();
        }
        int ttotal=0;
        double mmo=0.0;
        for (i=0; i<N; i++)
        {
            Console.WriteLine("Line {0} has a total of {1} and an average of {2}", i, hold[i].total, hold[i].mo);
            ttotal += hold[i].total;
            mmo += hold[i].mo;
        }
        mmo = mmo / N;
        Console.WriteLine("The total of all the lines is {0} and the average of all the lines is {1}",ttotal, mmo);
        Console.ReadKey();
    }
}

Whenever I try to run the program it crashes at line 19

hold[i].total = 0;

What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire