David Shepard
David Shepard

Reputation: 181

how to fill only one row and one column in two-dimensional array?

What is a right way to fill the array - only first row and first column? Filling the Edge array (which is one-dimensiona) works fine and I try do fill Matrix array the same way, but this doesn't work.

static void Main(string[] args)
        {
          Random rn = new Random();
          Graph gr = new Graph(rn.Next(1, 30), rn.Next(1, 30));
          Console.ReadKey();
        }
        public class Graph
        {
            private int[] Edge;

        private int n;
        private int m;

        private int x;

        private int[][] Matrix;

        public Graph(int _edge, int _matrix)
        {
            Random rn = new Random();
            x = rn.Next(0,30);

            Edge=new int[_edge];
            Matrix = new int[_matrix][];


            for (int i = 1; i < x; i++)
            {
                Edge[i] = i;
                Console.WriteLine(Edge[i]);
            }
            Console.WriteLine("Number of nodes {0}", x);

            for (int i = 0; i < x; i++)
            {
                for (int j = 0; j < x; j++)
                {
                    Matrix[0][j] = j;
                    Matrix[i][0] = i;
                    Console.WriteLine(Matrix[i][j]);
                }

            }
        }


    }

Upvotes: 0

Views: 628

Answers (1)

Henrik
Henrik

Reputation: 23324

"this doesn't work" probably means, you get a NullReferenceException. After

Matrix = new int[_matrix][];

insert

for (int i = 0; i < Matrix.Length; ++i)
    Matrix[i] = new int[_matrix];

Upvotes: 1

Related Questions