0

I am trying to do a matrix multiplication program in c# using jagged array. The code followws:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JaggedMM
{
class Program
{
    static void Main(string[] args)
    {
        int r;
        Console.WriteLine("Enter the number of rows");
        r = Convert.ToInt32(Console.ReadLine());
        int[][] a = new int[r][];
        int[][] b = new int[r][];
        int[][] result = new int[r][];
        Console.WriteLine("First matrix");
        for (int i = 0; i < r; i++) {
            Console.WriteLine("Enter number of elements in" + i + "row:");
            int bc = Convert.ToInt32(Console.ReadLine());
            a[i] = new Int32[bc];
            for(int j = 0; j < bc; j++)
            {
                Console.WriteLine("Enter the elements in "+i+" row");
                a[i][j] = Convert.ToInt32(Console.ReadLine());
            }
        }
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < a[i].Length; j++) {
                Console.Write(a[i][j] + "\t");
                Console.Write("\n");
            }
        }
        Console.WriteLine("Second matrix");
        for (int i = 0; i < r; i++)
        {
            Console.WriteLine("Enter number of elements in" + i + "row:");
            int bc1 = Convert.ToInt32(Console.ReadLine());
            b[i] = new Int32[bc1];
            for (int j = 0; j < bc1; j++)
            {
                Console.WriteLine("Enter the elements in " + i +  " row");
                b[i][j] = Convert.ToInt32(Console.ReadLine());
            }
        }
        for (int i = 0; i < r; i++)
        {
            for (int j = 0; j < b[i].Length; j++)
            {
                Console.Write(b[i][j] + "\t\n");
            }
        }
        for (int i = 0; i < r; i++) {
            for(int j = 0; j < a[i].Length && j<b[i].Length; j++)
            {
                result[i][j] = a[i][j]*b[j][i];
            }
        }
        for(int i = 0; i < r; i++)
        {
            for(int j = 0; j < a[i].Length && j<b[i].Length; j++)
            {
                Console.Write("The result is: \n"+result[i][j]+"\n\n");
            }
        }
    }
}
}

i got two input matrices. The problem is when i try to calculate the multiplication of those matrix im getting a systemNullException error.

Bharath
  • 15
  • 7
  • Where are you getting the null exception? – TheDude Jul 28 '16 at 14:44
  • If you do matrix multiplication then the number of columns of the first matrix must equal the number of rows of the second. Also a matrix should not be jagged, the number of columns for each row should be the same. – juharr Jul 28 '16 at 14:49
  • The error shows in calculating the result. – Bharath Jul 28 '16 at 14:51
  • The problem is that you never create the sub arrays inside of `result`. So `result[i]` is null. You need to do something like `result[i] = new int[size];`. But really you'd be better off if you just used 2D arrays instead since they better represents a matrix. – juharr Jul 28 '16 at 14:54
  • Can you help me what to specify in sub arrays of result. result[i]=new int [??] – Bharath Jul 28 '16 at 15:02

0 Answers0