C# init arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wkid87
    New Member
    • Oct 2007
    • 13

    #16
    I got it figure out. Instead of displaying my results in each Method I need it to be display in the Answer Method. I need to pass the arguments to the Answer Method The next problem I having is the answer that it is displaying is not in a matrix but in a straight line. Here are the directions. I want to make sure I have this right:

    Write a method that sets data size and the elements for each of the matrices.
    Write a method that multipies the two matrices.
    Write a method that dds the two matrices.
    Write a method that subtract one matrix from the other.
    Write a method nicely dsplay the matrices.

    The main Method should test the class. The method names shall be selected to reflect what the method does.
    Code:
    .
    
    using System;
    
    
    
    public class Matrix
    {
    
        public static void Main(string[] args)
        {
    
            int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
    
            int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
    
            int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
    
            InputMatrix(matrix1);
    
            Console.WriteLine();
    
            InputMatrix(matrix2);
    
            Console.WriteLine();
    
            AddMatrix(matrix1, matrix2, matrix3);
    
            SubtractMatrix(matrix1, matrix2, matrix3);
    
            Multipy(matrix1, matrix2, matrix3);
    
        }
    
        public static void InputMatrix(int[,] array)
        {
    
            for (int row = 0; row < array.GetLength(0); row++)
            {
    
                for (int column = 0; column < array.GetLength(1); column++)
                {
                    Console.WriteLine("Enter element {0}, {1}", row, column);
                    array[row, column] = Convert.ToInt32(Console.ReadLine());
    
                }
                Console.WriteLine();
    
    
            }
    
        }
    
        public static void AddMatrix(int[,] array1, int[,] array2, int[,] array3)
        {
    
            for (int row = 0; row < array1.GetLength(0); row++)
            {
    
                for (int column = 0; column < array1.GetLength(1); column++)
    
                    array3[row, column] = array1[row, column] + array2[row, column];
                Console.WriteLine();
    
            }
    
            for (int row = 0; row < array1.GetLength(0); row++)
            {
    
                for (int column = 0; column < array1.GetLength(1); column++)
    
                    Console.WriteLine();
                   
    
            }
    
       }
    
        public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)
        {
    
            for (int row = 0; row < array1.GetLength(0); row++)
            {
    
                for (int column = 0; column < array1.GetLength(1); column++)
    
                    array3[row, column] = array1[row, column] - array2[row, column];
    
            }
    
            for (int row = 0; row < array1.GetLength(0); row++)
            {
    
                for (int column = 0; column < array1.GetLength(1); column++)
    
                    Console.Write("{0}", array3[row, column]);
    
            }
    
            Console.WriteLine();
    
        }
        public static void Multipy(int[,] array1, int[,] array2, int[,] array3)
        {
            for (int row = 0; row < array1.GetLength(0); row++)
            {
                for (int column = 0; column < array2.GetLength(1); column++)
                {
                    for (int a = 0; a < array1.GetLength(1); a++)
                        array3[row, column] += array1[row, a] * array2[a, column];
                    Console.Write("{0}", array3[row, column]);
                }
                Console.WriteLine();
            }
    
        }
        public static void Answers(int[,] array3)
        {
       
        
        
        
        }
    
            
     }

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #17
      I can see quite a few runtime errors (practically in each method that you have created, minor ones, but will affect runtime critically)
      Things like assigning the matrix (dosent return anything, addidion subtraction, multiplication dosent really hlp unless you are printing the result from that method itself)
      Anyhows, what are the issues you are facing? I wanst able to get your problem

      Comment

      • wkid87
        New Member
        • Oct 2007
        • 13

        #18
        Instead of displaying the results in the each method. I want the result only display in the Answer method. I want to return the Add, Subtract, and Multipy Methods so I can get the reults to display in the Answer method. How will I do that.
        Code:
         using System;
        
        
        
        public class Matrix
        {
        
            public static void Main(string[] args)
            {
        
                int[,] matrix1 = { { 1, 2 }, { 3, 4 } };
        
                int[,] matrix2 = { { 2, 3 }, { 4, 5 } };
        
                int[,] matrix3 = { { 0, 0 }, { 0, 0 } };
        
                InputMatrix(matrix1);
        
                Console.WriteLine();
        
                InputMatrix(matrix2);
        
                Console.WriteLine();
        
                AddMatrix(matrix1, matrix2, matrix3);
        
                SubtractMatrix(matrix1, matrix2, matrix3);
        
                Multipy(matrix1, matrix2, matrix3);
        
            }
        
            public static void InputMatrix(int[,] array)
            {
        
                for (int row = 0; row < array.GetLength(0); row++)
                {
        
                    for (int column = 0; column < array.GetLength(1); column++)
                    {
                        Console.WriteLine("Enter element {0}, {1}", row, column);
                        array[row, column] = Convert.ToInt32(Console.ReadLine());
        
                    }
                    Console.WriteLine();
        
        
                }
        
            }
        
            public static void AddMatrix(int[,] array1, int[,] array2, int[,] array3)
            {
        
                for (int row = 0; row < array1.GetLength(0); row++)
                {
        
                    for (int column = 0; column < array1.GetLength(1); column++)
        
                        array3[row, column] = array1[row, column] + array2[row, column];
        
                }
        
                for (int row = 0; row < array1.GetLength(0); row++)
                {
        
                    for (int column = 0; column < array1.GetLength(1); column++)
        
                        Console.Write("{0}", array3[row, column]);
        
                        Console.WriteLine();
                       
        
                }
        
           }
        
            public static void SubtractMatrix(int[,] array1, int[,] array2, int[,] array3)
            {
        
                for (int row = 0; row < array1.GetLength(0); row++)
                {
        
                    for (int column = 0; column < array1.GetLength(1); column++)
        
                        array3[row, column] = array1[row, column] - array2[row, column];
        
                }
        
                for (int row = 0; row < array1.GetLength(0); row++)
                {
        
                    for (int column = 0; column < array1.GetLength(1); column++)
        
                        Console.Write("{0}", array3[row, column]);
        
                }
        
                Console.WriteLine();
        
            }
            public static void Multipy(int[,] array1, int[,] array2, int[,] array3)
            {
                for (int row = 0; row < array1.GetLength(0); row++)
                {
                    for (int column = 0; column < array2.GetLength(1); column++)
                    {
                        for (int a = 0; a < array1.GetLength(1); a++)
                            array3[row, column] += array1[row, a] * array2[a, column];
                        Console.Write("{0}", array3[row, column]);
                        Console.WriteLine();
                    }
                    Console.WriteLine();
                }
        
            }
            public static void Answers(int[,] array3)
            {
           
            
            
            
            }
        
                
         }

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #19
          [CODE=cpp]using System;
          using System.Collecti ons.Generic;
          using System.Text;

          namespace ConsoleApplicat ion4
          {
          public static class Matrix
          {

          public static void theMatrix()
          {

          int[,] matrix1 = { { 1, 2 }, { 3, 4 } };

          int[,] matrix2 = { { 2, 3 }, { 4, 5 } };

          int[,] matrix3 = { { 0, 0 }, { 0, 0 } };

          InputMatrix(ref matrix1);

          Console.WriteLi ne();

          InputMatrix(ref matrix2);

          Console.WriteLi ne();

          AddMatrix(matri x1, matrix2, ref matrix3);

          SubtractMatrix( matrix1, matrix2, ref matrix3);

          Multipy(matrix1 , matrix2, ref matrix3);

          }

          public static void InputMatrix(ref int[,] array)
          {
          for (int row = 0; row < array.GetLength (0); row++)
          {
          for (int column = 0; column < array.GetLength (1); column++)
          {
          Console.WriteLi ne("Enter element {0}, {1}", row, column);
          array[row, column] = Convert.ToInt32 (Console.ReadLi ne());
          }
          Console.WriteLi ne();
          }
          }

          public static void AddMatrix(int[,] array1, int[,] array2,ref int[,] array3)
          {
          for (int row = 0; row < array1.GetLengt h(0); row++)
          {
          for (int column = 0; column < array1.GetLengt h(1); column++)
          array3[row, column] = array1[row, column] + array2[row, column];
          }
          Answers(array3, "Addition") ;
          }

          public static void SubtractMatrix( int[,] array1, int[,] array2, ref int[,] array3)
          {
          for (int row = 0; row < array1.GetLengt h(0); row++)
          {
          for (int column = 0; column < array1.GetLengt h(1); column++)
          array3[row, column] = array1[row, column] - array2[row, column];
          }
          Answers(array3, "Subtractio n");
          }

          public static void Multipy(int[,] array1, int[,] array2, ref int[,] array3)
          {
          for (int row = 0; row < array1.GetLengt h(0); row++)
          {
          for (int column = 0; column < array2.GetLengt h(1); column++)
          {
          for (int a = 0; a < array1.GetLengt h(1); a++)
          array3[row, column] += array1[row, a] * array2[a, column];
          }
          }
          Answers(array3, "Multiplication ");
          }
          public static void Answers(int[,] array3, string answerType)
          {
          Console.WriteLi ne("Answer({0}) :",answerTyp e);
          for (int row = 0; row < array3.GetLengt h(0); row++)
          {
          for (int column = 0; column < array3.GetLengt h(1); column++)
          {
          Console.Write(" {0}", array3[row, column]);
          }
          Console.WriteLi ne();
          }
          }


          }

          }[/CODE]
          Hi,
          I have modified your code a bit so as to make it a complete static class. Please make the necessary changees to revert it back to your (static class, Main has been replaced to theMatrix)
          I have put the ref tags, generally we could have done without it. especially for array3.
          Input can be modified to a return type.

          cheers

          Comment

          • wkid87
            New Member
            • Oct 2007
            • 13

            #20
            Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam.

            Comment

            • Shashi Sadasivan
              Recognized Expert Top Contributor
              • Aug 2007
              • 1435

              #21
              Originally posted by wkid87
              Thanks. One more thing. Is there a way I can space the numbers out instead of being so jam.
              hello again,
              Put a space :).... you can do a lot of things.
              if you want you can even make a table.
              But you have to experiment it yourself
              happy coding

              Comment

              Working...