C# init arrays

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

    C# init arrays

    I''m working on a project that will add,multipy,sub tract and divide a matrix. Each one is suppose to be display in a method. I can figure out the addition and subtraction but not the multipication and divison. I also needs help with user input. The user suppose to enter the values in the Display Method, but I'm having trouble with the Console.Intt32. My code is below.
    [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 = new int [2,2];

    DisplayMatrix(m atrix1);

    Console.WriteLi ne();

    DisplayMatrix(m atrix2);

    AddMatrix(matri x1, matrix2, matrix3);

    SubtractMatrix( matrix1, matrix2, matrix3);

    }

    public static void DisplayMatrix(i nt[,] array)

    {

    for (int row=0; row < array.GetLength (0); row++)

    {

    for (int column = 0; column < array.GetLength (1); column++)

    array=Convert.T oInt(Console.Re adLine());

    Console.Write(" {0}", array[ row, column]);

    Console.WriteLi ne();


    }

    }

    public static void AddMatrix(int[,] array1, int [,] array2, int[,] array3)

    {

    for(int row=0; row< array1.GetLengt h(0); row++)

    {

    for(int column=0; column <array1.GetLeng th( 1 ); column++)

    array3[row, column] = array1 [row, column] + array2 [row, column];

    }

    for (int row = 0; row < array1.GetLengt h(0); row++)

    {

    for (int column = 0; column < array1.GetLengt h(1); column++)


    Console.Write(" {0}", array3[row, column]);

    }

    Console.WriteLi ne();

    }

    public static void SubtractMatrix( int[,] array1, int[,] array2, 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];

    }

    for (int row = 0; row < array1.GetLengt h(0); row++)

    {

    for (int column = 0; column < array1.GetLengt h(1); column++)

    Console.Write(" {0}", array3[row, column]);

    }

    Console.WriteLi ne();

    }


    }
    {/CODE}
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Hi wkid87
    Please put your code withing [CODE] tags for readability.

    I dont think that Console does not have a Intt32 method or property.

    There is site which i came across doing a search, especially since matrix operations have been around for a very long time
    This site provides the code to use

    cheers
    Last edited by Shashi Sadasivan; Oct 9 '07, 04:17 AM. Reason: Found a site that would be helpful

    Comment

    • wkid87
      New Member
      • Oct 2007
      • 13

      #3
      Originally posted by Shashi Sadasivan
      Hi wkid87
      Please put your code withing [CODE] tags for readability.

      I dont think that Console does not have a Intt32 method or property.

      There is site which i came across doing a search, especially since matrix operations have been around for a very long time
      This site provides the code to use

      cheers
      Thanks for the link, but it did not help any. Didn't see the code.

      Do you know how to get values from the user?

      Comment

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

        #4
        Originally posted by wkid87
        Thanks for the link, but it did not help any. Didn't see the code.

        Do you know how to get values from the user?
        well the code is to be donloaded from the site.
        I think it will be as a library, with all the functions defined in it, all you would need is to reference the library and use the methods in it. The algorithm should be implemented within that.

        How does your application run?
        Windows app, Console based, or web based?

        cheers

        Comment

        • wkid87
          New Member
          • Oct 2007
          • 13

          #5
          I use Visual Basic 2005.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by wkid87
            I use Visual Basic 2005.
            I would guess it's a console application in VB.NET

            Comment

            • wkid87
              New Member
              • Oct 2007
              • 13

              #7
              Its Microsoft Visual C# 2005

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by wkid87
                Its Microsoft Visual C# 2005
                You *JUST* said you were using Visual Basic.
                Which is it?

                Comment

                • wkid87
                  New Member
                  • Oct 2007
                  • 13

                  #9
                  Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse.

                  Comment

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

                    #10
                    Originally posted by wkid87
                    Its Microsoft Visual C# 2005, sorry. Was on my laptop earlier and was confuse.
                    Ok...VB to C#
                    So....is it a web application? a windows application or a console one?

                    Comment

                    • wkid87
                      New Member
                      • Oct 2007
                      • 13

                      #11
                      Originally posted by Shashi Sadasivan
                      Ok...VB to C#
                      So....is it a web application? a windows application or a console one?
                      It is a Console application

                      Comment

                      • wkid87
                        New Member
                        • Oct 2007
                        • 13

                        #12
                        I trying to figure out the multipication. I know I probably need three for statements but I'm still stuck.

                        Comment

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

                          #13
                          You might either want to post some code for anyone to help you on that. I tried my hands on the link that i had previously provided. Worked fine and i didnt have to do any nested forloops...(all done by that library :))
                          Have you figured out user input for you console app?

                          cheers

                          Comment

                          • wkid87
                            New Member
                            • Oct 2007
                            • 13

                            #14
                            Originally posted by Shashi Sadasivan
                            You might either want to post some code for anyone to help you on that. I tried my hands on the link that i had previously provided. Worked fine and i didnt have to do any nested forloops...(all done by that library :))
                            Have you figured out user input for you console app?



                            cheers
                            Is the a way that you can post the code on here. I retried to open the code but saying something it can't convert.

                            No I have not figure that out. I tried to do a ConvertToInt32 and keep getting errors.

                            Comment

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

                              #15
                              Is that on user input?

                              could you post the code you are actually using rather than giving 1/2 of the snippet?

                              cheers

                              I have attached the project below
                              That code was made in 1.1 due to which vs2005 will attempt to convert it.

                              cheers
                              Attached Files

                              Comment

                              Working...