Thread: 2D Array Help

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    2D Array Help

    how can i get an 2 dimentional array to be called and the contents of the second array to be passed into the first array and then show the user the contents of array before and after the function call?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >the contents of the second array to be passed into the first array
    Code:
    void func(int array[][3])
    Or alternately:
    Code:
    void func(int array[3][3])
    If you have a second array, just add the second array to the function parameter list.

  3. #3
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah i have that but was wondering if anyone could give me a few more hints of what is needed.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need to explain better what you need, and maybe post some code showing what you're trying to do. Your first post isn't clear. If you use a vector instead, you can copy by doing:
    Code:
    vector<int> b = a;

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    what is needed is
    1. 2d int array of the same size
    2. function called AddArray
    3. contents of the second array to be added to the first array element by element
    4. the caller main() should output the contents of the arrays before and after the function call.

    Hope this helps a bit more.

    ill post some code when i have got a bit more done, just asked so i could get a few pointers as i go along.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    So in main you've got the following (where size is a constant):
    Code:
       int a[size][size];
       int b[size][size];
    
       //Arrays filled with values
    
       //Output the contents of the arrays (use nested for-loops)
    
       AddArray(a, b);
    
       //Output the contents of the arrays (use nested for-loops)
    Then the declaration for AddArray would look something like:
    Code:
    void AddArray(int a[size][size], int b[size][size])
    {
       //Code to add the arrays
    }
    Or you can leave out the first dimension:
    Code:
    void AddArray(int a[][size], int b[][size])

  7. #7
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    then do i just need to get a loop to go through all the characters and then output them into the first array?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >then do i just need to get a loop to go through all the characters and then output them into the first array?
    Your description says:
    >3. contents of the second array to be added to the first array element by element
    So you want to add each element of the second array to the corresponding element of the first array.

    Take something simple like printing a 2d array. How do you do that? Well normally two nested for-loops. Start by writing the code to do that. Once you have code to print a 2d array, compile and run the code to make sure it prints the array. Once you have that part working simply take that code, copy it to your AddArray function, and change it to add the arrays, instead of printing them.

  9. #9
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    just an example of the loop i will need to do is this right (StrOut from another program)

    Code:
    	  int i = 0;
          while (a[i] != '\0') 
          {
              StrOut[i] = a[i];
              i ++;
          }
      
      StrOut[i] = ' ';
      i++;
      
      int j = 0;
      while (b[j] != '\0') 
          {
              StrOut[i] = b[j];
              j ++;
              i++;
              i+=StrOut[i] = '\0';
          }
          cout <<StrOut <<endl;

  10. #10
    Registered User Xeridanus's Avatar
    Join Date
    Oct 2006
    Location
    QLD, Aussieland
    Posts
    11
    Code:
    for (int i=0; i<rows; ++i) {
    	for (int j=0; j<cols; ++j) {
    		printf("%i", A[rows][cols]); //or %f if you use doubles
    	}
    	printf("\n"); //end of row, go to next line.
    }
    NB: capital letters are standard notation for matrices in maths, doesn't really matter in code (im assuming thats your goal, a matrix). rows and cols are the dimensions of the array. i have done a matrix ADT for both C and C++. i used pointer arith and a single-d array but this will work too.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("%i", A[rows][cols]); //or %f if you use doubles
    I think you meant i and j.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i got this loop. guess it does the same thing?

    Code:
    int arr[3][3];
    for (int x=0; x<3; x++)
       for (int y=0; y<3; y++)
          arr[x][y] = 0;

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i got this loop. guess it does the same thing?
    That's exactly what you want. It will allow you to access each element of the 2d array.

  14. #14
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    once i have got that loop implemented how do i show what was in the array before and then after it had been copied?

  15. #15
    Registered User Xeridanus's Avatar
    Join Date
    Oct 2006
    Location
    QLD, Aussieland
    Posts
    11
    whoops, yes i did.
    Code:
    printf("%i", A[i][j]);
    thanks for the pick-up

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM