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 / C++ / MFC
  4. Copying From Single Dimension Array To Multiple Dimension Array

Copying From Single Dimension Array To Multiple Dimension Array

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structurestutorialquestion
7 Posts 5 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.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }

    C L S D 4 Replies Last reply
    0
    • M Mike Certini

      I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }

      C Offline
      C Offline
      chevu
      wrote on last edited by
      #2

      Are trying to transfer content of arr to arr3, right? But I am not able to see any memory allocation for arr3... You have to allocate some memory.. The way you are doing wont be safe in terms of memory pointer usage.. You can do something like this...

      case 1: assigning direct pointer of arr to arr3
      arr3 = new int[SIZE];
      /* for loop with i*/
      arr3[i] = &(arr[UPTO_SIZE])

      case 2: copyinh content of arr to arr3
      arr3 = new int[SIZE];
      /* for loop upto size */
      arr3[i] = new int[size2]

      /* copy logic from arr to arr3*/

      I hope you can add proper code in this logic... As we cannot complete your assignment... :D

      M 1 Reply Last reply
      0
      • C chevu

        Are trying to transfer content of arr to arr3, right? But I am not able to see any memory allocation for arr3... You have to allocate some memory.. The way you are doing wont be safe in terms of memory pointer usage.. You can do something like this...

        case 1: assigning direct pointer of arr to arr3
        arr3 = new int[SIZE];
        /* for loop with i*/
        arr3[i] = &(arr[UPTO_SIZE])

        case 2: copyinh content of arr to arr3
        arr3 = new int[SIZE];
        /* for loop upto size */
        arr3[i] = new int[size2]

        /* copy logic from arr to arr3*/

        I hope you can add proper code in this logic... As we cannot complete your assignment... :D

        M Offline
        M Offline
        Mike Certini
        wrote on last edited by
        #3

        chevu, Thank you for your reply. I am not taking a class but doing self study to learn C. I will take your solution and walk through the logic to understand. Thanks again. Mike

        C 1 Reply Last reply
        0
        • M Mike Certini

          chevu, Thank you for your reply. I am not taking a class but doing self study to learn C. I will take your solution and walk through the logic to understand. Thanks again. Mike

          C Offline
          C Offline
          chevu
          wrote on last edited by
          #4

          And ya if you don't want to new memory allocation you can directly assign address of arr to arr3, but for that in your function your have to send address of arr3 pointer i mean &arr3 (which you have declared as int** arr3), and in function you can assign address of arr to arr3....

          1 Reply Last reply
          0
          • M Mike Certini

            I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            A couple of suggestions: 1. Put your code between <pre></pre> tags so it is more readable. 2. Try compiling the above and report or fix any errors you see,

            I must get a clever new signature for 2011.

            1 Reply Last reply
            0
            • M Mike Certini

              I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              For one, transarr() does not get all the information it needs: You have a single, one-dimensional array with 5 elements, and try to transfer it into another array with ROWS rows. But you do not give an indication about the width of those rows in the parameters. The width is only given through the global symbol SIZE2. Pass SIZE2 as a parameter instead, so transarr() does not depend on a global value and can be reused for different cases. Second, in your main program you define a two-dimensional array arr2, but you do not use it. Instead you pass another variable, arr3 to transarr - that doesn't make sense. I think your intention was to copy the members of arr into arr2, no? If so, you do not need the variable arr3, you can pass arr2 into the call of transarr(), you don't even need to change the function declaration for that. Third, the purpose of transarr is somewhat ... unusual: In your code you copy the original array into each row, creating multiple copies of that array. Is that your intention? Also, if SIZE2 happens to be greater than SIZE, then your copy loop will run into undefined memory as it will read beyond arr. Of course at the moment nothing happens, but if you get your program to run and later change SIZE or SIZE2, you might end up with random values in your resulting array and not know where they originate from.

              1 Reply Last reply
              0
              • M Mike Certini

                I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                What do you want arr2 to look like when done?

                +-----+-----+-----+-----+-----+
                | 100 | 200 | 300 | 400 | 500 |
                +-----+-----+-----+-----+-----+
                | | | | | |
                +-----+-----+-----+-----+-----+

                or

                +-----+-----+-----+-----+-----+
                | | | | | |
                +-----+-----+-----+-----+-----+
                | 100 | 200 | 300 | 400 | 500 |
                +-----+-----+-----+-----+-----+

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                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