Multidimensional arrays in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bigshot
    New Member
    • May 2007
    • 5

    Multidimensional arrays in C

    Im trying to scan a file using fscanf and want to put the results in a multidimensiona l array (since C has no strings I need it to store names).

    Basically I want to be able to have a multidimensiona l array of names that were scanned in order that the file had and be able to access each one by using array[1] for example, to output one of the names.

    Im very bad at multidimensiona l array syntax so am asking for help, I could post my code and file contents if needed, but thought someone may know how to do this without them, thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    Check this thread , it will help u.
    Also search this forum .

    Thanks
    Raghuram

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      HI,
      sorry i missed the link
      Raghu

      Comment

      • m1003037
        New Member
        • May 2007
        • 1

        #4
        Have a look at the following code.
        This program reads "size" no of strings(of max length 15) that are separated by either space or new line from the file C:\a.txt & prints.

        <Removed code as per Posting Guidelines -- MODERATOR>
        Last edited by AdrianH; May 11 '07, 12:54 PM. Reason: Removed code as per Posting Guidelines

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          If you are still having difficulties, please post the fragment that you are having difficulties with. Or state what it is that you are having problems with.

          We at TSDN are not here to code the solution for you, but to help you in understanding the problem you are experiencing.


          Adrian

          Comment

          • Bigshot
            New Member
            • May 2007
            • 5

            #6
            okay well thanks for the help, but I'm still not getting where I wanted, I'll start a new thread and post my code in there to maybe get some more specific help.

            Comment

            • rosun82
              New Member
              • May 2007
              • 5

              #7
              In my opinion, using multidimensiona l arrays is always a BAD choice. It is not flexible, it is not easy to read, it suffers from distinction between compilers.

              Probably the most convenient solution is use one dimensional array and adding your own dimension management. In this case, I suggest a one dimensional array for storing pointer to string, like this:

              vector<string*> my_array;

              then you add

              vector<int> size;

              to manage the dimensional information, say 2*3*5 array, and so on.

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                Originally posted by rosun82
                In my opinion, using multidimensiona l arrays is always a BAD choice. It is not flexible, it is not easy to read, it suffers from distinction between compilers.
                I agree with everything you said except the last point regarding distinction between compilers. To what are you referring?
                Originally posted by rosun82
                Probably the most convenient solution is use one dimensional array and adding your own dimension management. In this case, I suggest a one dimensional array for storing pointer to string, like this:

                vector<string*> my_array;

                then you add

                vector<int> size;

                to manage the dimensional information, say 2*3*5 array, and so on.
                What is the vector<int> for?


                Adrian

                Comment

                • rosun82
                  New Member
                  • May 2007
                  • 5

                  #9
                  Originally posted by AdrianH
                  I agree with everything you said except the last point regarding distinction between compilers. To what are you referring?

                  What is the vector<int> for?


                  Adrian
                  When I started using C++, I remember some code regarding multi-dimensional arrays can not compile in some compiler, but was OK in other compilers. This might not be a problem anymore: )

                  the vector<int> is used for storing sizes of each dimension. For example, for a 2*2 table, the vector<int> is [2,2], for a 3 * 5 * 9 table, the vector<int> is [3, 5, 9]

                  In this way, we can create different abstract table from one set of data, e.g. at one time we may think a table is 2*2*2, at the other time we may think it's 2*4, no data copying is required at all.

                  Further more, by introducing a new vector, which I called an ACU, we can change the abstract dimension order of a table. Say a 2*3 table, the ACU is [3,1], it means we increase 1 to shift the right most dimension, and increase 3 to shift the left most dimension. Now we may want to swap the two dimensions, so we can view the table as a 3*2. In this case we use the new size [3,2] and ACU[1,3], and KEEP THE DATA UNCHANGED.

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by rosun82
                    When I started using C++, I remember some code regarding multi-dimensional arrays can not compile in some compiler, but was OK in other compilers. This might not be a problem anymore: )
                    I hope not, this is part of the standard. ;)
                    Originally posted by rosun82
                    the vector<int> is used for storing sizes of each dimension. For example, for a 2*2 table, the vector<int> is [2,2], for a 3 * 5 * 9 table, the vector<int> is [3, 5, 9]

                    In this way, we can create different abstract table from one set of data, e.g. at one time we may think a table is 2*2*2, at the other time we may think it's 2*4, no data copying is required at all.

                    Further more, by introducing a new vector, which I called an ACU, we can change the abstract dimension order of a table. Say a 2*3 table, the ACU is [3,1], it means we increase 1 to shift the right most dimension, and increase 3 to shift the left most dimension. Now we may want to swap the two dimensions, so we can view the table as a 3*2. In this case we use the new size [3,2] and ACU[1,3], and KEEP THE DATA UNCHANGED.
                    Interesting, but I must ask. Why would one want to change the array's dimentions while keeping the array's current representation?


                    Adrian

                    Comment

                    • rosun82
                      New Member
                      • May 2007
                      • 5

                      #11
                      Originally posted by AdrianH
                      I hope not, this is part of the standard. ;)
                      Interesting, but I must ask. Why would one want to change the array's dimentions while keeping the array's current representation?


                      Adrian
                      ha, this actually happens in my one of my graduate program, where we must handle the product of a bunch of probability tables, such as

                      P(A, B, C) P(D, C) P(D, A) P(B, E), each table is large, and when you want to multiply them together, you must specify an order, such as A, B, C, D, E to ensure the result is right. Sometimes the variable order in different tables contradicts, such as P(A, B)P(C, B, A), so the variable ordering must be changed.

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by rosun82
                        ha, this actually happens in my one of my graduate program, where we must handle the product of a bunch of probability tables, such as

                        P(A, B, C) P(D, C) P(D, A) P(B, E), each table is large, and when you want to multiply them together, you must specify an order, such as A, B, C, D, E to ensure the result is right. Sometimes the variable order in different tables contradicts, such as P(A, B)P(C, B, A), so the variable ordering must be changed.
                        I understand, but how are you able to process the data appropriately without the use of an adapter but by simply saying that the array should be interpreted using some arbitrary dimensions? You would have to be very careful not to use inappropriate dimensions lest the elements accessed though the dimensions not make much sense.


                        Adrian

                        Comment

                        Working...