Wednesday, August 11, 2010

Program to demonstrate multidimensional array.


Write a program to demonstrate multidimensional array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixCalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Matrix obj = new Matrix();
            obj.MatrixAddition();
            Console.ReadLine();
        }
    }
    class Matrix
    {
        int a,b;
        public void MatrixAddition()
        {
            Console.WriteLine("Enter the length of matrix");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            int[,] m = new int[a, b];
            int[,] n = new int[a, b];
            Console.WriteLine("Enter the values of matrix a");
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    m[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            Console.WriteLine("Enter the values of matrix b");
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    n[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            echo:
            Console.WriteLine("======================");
            Console.WriteLine("1.Addition");
            Console.WriteLine("2.Subtraction");
            Console.WriteLine("3.Multiplication");
            Console.WriteLine("4.Transpose");
            Console.WriteLine("The values of Matrix A are :");
            {
                for (int i = 0; i < a; i++)
                {
                    for (int j = 0; j < b; j++)
                    {
                        Console.Write("{0}\t", m[i, j]);
                    }
                    Console.Write("\n");
                }
            }
            Console.WriteLine("The values of Matrix B are :");
            {
                for (int i = 0; i < a; i++)
                {
                    for (int j = 0; j < b; j++)
                    {
                        Console.Write("{0}\t", n[i, j]);
                    }
                    Console.Write("\n");
                }
            }
            int option;
            char c;
            option = Convert.ToInt32(Console.ReadLine());
            switch (option)
            {
                case 1:
                    Console.WriteLine("The Matrix addition is:");
                    {
                        for (int i = 0; i < a; i++)
                        {
                            for (int j = 0; j < b; j++)
                            {
                                Console.Write("{0}\t", m[i, j] + n[i, j]);
                            }
                            Console.Write("\n");
                        }
                    }
                    break;
                case 2:
                    Console.WriteLine("The Matrix Subtraction is:");
                    {
                        for (int i = 0; i < a; i++)
                        {
                            for (int j = 0; j < b; j++)
                            {
                                Console.Write("{0}\t", m[i, j] - n[i, j]);
                            }
                            Console.Write("\n");
                        }
                    }
                    break;
                case 3:
                    Console.WriteLine("The Matrix Multiplication is:");
                    {
                        for (int i = 0; i < a; i++)
                        {
                            for (int j = 0; j < b; j++)
                            {
                                Console.Write("{0}\t", m[i, j] * n[i, j]);
                            }
                            Console.Write("\n");
                        }
                    }
                    break;
                case 4:
                    Console.WriteLine("Transpose of Matrix A is:");
                    {
                        for (int i = 0; i < a; i++)
                        {
                            for (int j = 0; j < b; j++)
                            {
                                Console.Write("{0}\t", m[j,i]);
                            }
                            Console.Write("\n");
                        }
                    }
                    Console.WriteLine("Transpose of Matrix B is:");
                    {
                        for (int i = 0; i < a; i++)
                        {
                            for (int j = 0; j < b; j++)
                            {
                                Console.Write("{0}\t", m[j, i]);
                            }
                            Console.Write("\n");
                        }
                    }
                    break;
                    default:
                    Console.WriteLine("Invalid Option");
                    break;
            }
            Console.WriteLine("Enter y to goto menu or any other key exist");
            c = Convert.ToChar(Console.ReadLine());
            if (c == 'y')
            {
                goto echo;
            }
            

        }
       
    }
}

No comments:

Post a Comment