Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CAn we define this kind of constructor

CAn we define this kind of constructor

Scheduled Pinned Locked Moved C#
helptutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitmohanty
    wrote on last edited by
    #1

    I want to define a class (which will store a 2D matrix and will do some operations on that). Say I want to define a 2D matirx [1 2 3 ;1 2 3; 3 4 5] and I have a GUI to enter the data, where I enter matrix(1,2,3;1,2,3;3,4,5) and it constructs the matrix [1 2 3 ;1 2 3; 3 4 5]. Can someone help me how to acheive this?

    S G 2 Replies Last reply
    0
    • A amitmohanty

      I want to define a class (which will store a 2D matrix and will do some operations on that). Say I want to define a 2D matirx [1 2 3 ;1 2 3; 3 4 5] and I have a GUI to enter the data, where I enter matrix(1,2,3;1,2,3;3,4,5) and it constructs the matrix [1 2 3 ;1 2 3; 3 4 5]. Can someone help me how to acheive this?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Maybe you can do something like

      new Matrix(
      new MatrixRow(1, 2, 3),
      new MatrixRow(1, 2, 3),
      new MatrixRow(3, 4, 5)
      );

      Does that help? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      1 Reply Last reply
      0
      • A amitmohanty

        I want to define a class (which will store a 2D matrix and will do some operations on that). Say I want to define a 2D matirx [1 2 3 ;1 2 3; 3 4 5] and I have a GUI to enter the data, where I enter matrix(1,2,3;1,2,3;3,4,5) and it constructs the matrix [1 2 3 ;1 2 3; 3 4 5]. Can someone help me how to acheive this?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        To store a two dimensional array, you use an array of arrays. int[][] matrix; matrix = new int[3][]; for (int i=0; i<3; i++) matrix[i] = new int[3]; Now you have a 3x3 array that you can store the values in. --- b { font-weight: normal; }

        A 1 Reply Last reply
        0
        • G Guffa

          To store a two dimensional array, you use an array of arrays. int[][] matrix; matrix = new int[3][]; for (int i=0; i<3; i++) matrix[i] = new int[3]; Now you have a 3x3 array that you can store the values in. --- b { font-weight: normal; }

          A Offline
          A Offline
          amitmohanty
          wrote on last edited by
          #4

          But only problem is that i don't know what will be the size of the mastirx before hand. I just gave u an example ,..it could be any n*m matrix.

          G 1 Reply Last reply
          0
          • A amitmohanty

            But only problem is that i don't know what will be the size of the mastirx before hand. I just gave u an example ,..it could be any n*m matrix.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            You can replace the numbers with variables containing any number you want. --- b { font-weight: normal; }

            A 1 Reply Last reply
            0
            • G Guffa

              You can replace the numbers with variables containing any number you want. --- b { font-weight: normal; }

              A Offline
              A Offline
              amitmohanty
              wrote on last edited by
              #6

              Have you worked in MATLAB? its like nothing is mentioned? you just enter >>[1 2 3,4 5 6;8 9 6] and it creates a matrix. i want some application lik ethat. by looking at the expression user entered you construct the matrix. i know we can do that by defiing a variable. but how to determine th eorder of the matrix (n*m) by looking at the expression. bec as far as i know constructor takes only parameters. trick here is that by looking at the string 1 2 3,4 5 6;8 9 6 your constructor should be able to say that it is a 3*3 matrix and defines the matrix accordingly

              G 1 Reply Last reply
              0
              • A amitmohanty

                Have you worked in MATLAB? its like nothing is mentioned? you just enter >>[1 2 3,4 5 6;8 9 6] and it creates a matrix. i want some application lik ethat. by looking at the expression user entered you construct the matrix. i know we can do that by defiing a variable. but how to determine th eorder of the matrix (n*m) by looking at the expression. bec as far as i know constructor takes only parameters. trick here is that by looking at the string 1 2 3,4 5 6;8 9 6 your constructor should be able to say that it is a 3*3 matrix and defines the matrix accordingly

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Split the string by semicolon: string input = "1 2 3;4 5 6;7 8 9"; string[] rows = input.Split(';'); Now you have an array with three records. Create an array of string arrays to store the final result: string[][] matrix = new string[rows.Length][]; Now split each row by space to get an array, and put it in the matrix: for (int i=0; i<rows.Length; i++) matrix[i] = rows[i].Split(' '); --- b { font-weight: normal; }

                A 1 Reply Last reply
                0
                • G Guffa

                  Split the string by semicolon: string input = "1 2 3;4 5 6;7 8 9"; string[] rows = input.Split(';'); Now you have an array with three records. Create an array of string arrays to store the final result: string[][] matrix = new string[rows.Length][]; Now split each row by space to get an array, and put it in the matrix: for (int i=0; i<rows.Length; i++) matrix[i] = rows[i].Split(' '); --- b { font-weight: normal; }

                  A Offline
                  A Offline
                  amitmohanty
                  wrote on last edited by
                  #8

                  Thanks..solves my problem. u exactly got into the root of the problem.

                  G 1 Reply Last reply
                  0
                  • A amitmohanty

                    Thanks..solves my problem. u exactly got into the root of the problem.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Note though that the code does not check if the matrix is square. It will happily turn a string like "1;2 3 4 5;;6;7;8 9" into arrays without complaining. --- b { font-weight: normal; }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups