+ Post New Thread
Results 1 to 15 of 15
Coding Thread, C++ Help in Coding and Web Development; Hey folks, using this sunny Saturday to crack on with some college work. Need to pass a 2d array to ...
  1. #1
    kerrymoralee9280
    Guest

    C++ Help

    Hey folks, using this sunny Saturday to crack on with some college work. Need to pass a 2d array to a function in a seperate file. Here's the code I have so far (please don't go picking faults with code, this is how I'm being taught )

    From the main.cpp file

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <F:\C\menu1.cpp>
    
    void main()
    {
        int iSelect=0;
        int iAmounts[5][6]={{120,125,110,90,130,50},{137,138,150,56,136,100},{103,133,105,80,109,150},{67,66,90,70,71,180},{57,80,99,85,90,110}};
        clrscr();
        cout<<"1. The total number and value of computers sold for any given product\n";
        cout<<"2. The total number and value of computers sold for any given month\n";
        cout<<"3. The maximum number and value of computers sold for any given product\n";
        cout<<"4. The maximum number and value of computers sold for any given month\n\n";
    
        cout<<"Please select a menu option: ";
            
        cin>>iSelect;
    
        if (iSelect==1)
        {
            menu1(iAmounts); //This is the bit where it (trys) to send it to menu1
        }
    ...............................
    This is menu 1:

    Code:
    void menu1(int iAmounts[][]) //If I put a 5 & 6 in these boxes it works, but I don't want to do that in case there are more than 5 & 6 in the array because then I'd have to change that in more than 1 place. How do I stipulate the size automatically?
    {
       clrscr();
       
       cout<<iAmounts[0][0]; //This is just a test print to see if it works.
    ...................
    Cheers guys - if I haven't been clear please ask what you need to know.

    HT

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,987
    Thank Post
    851
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    Its been a while but could you not pass a pointer to the array and then use the sizeof() function to find the array size?

    These might help:
    InformIT: C++ Arrays and Algorithms > Arrays
    how to find the length of an array in c/c++ ? - GameDev.Net Discussion Forums

  3. #3
    kerrymoralee9280
    Guest
    What I have tried is:

    Code:
    int iNumMonths=6;
    int iNumProds=5;
    int iAmounts[iNumMonths][iNumProds]={.........
    This method seems to work fine but when I pass them into another function I don't know what to do. I have tried:

    Code:
    void menu1(int iNumMonths,int iNumProds,int iAmounts[iNumMonths][iNumProds])
    But this obviously isn't working.

  4. #4

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,987
    Thank Post
    851
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    Try this example:

    Handling an array parameter as a pointer : function parameters«Function«C++ Tutorial

    it deals with a single dimensional parameter but it shows how to pass it as a array typed pointer. This way is cleaner as otherwise it copies the whole array into memory again which is usually not good practice.

  5. #5
    kerrymoralee9280
    Guest

  6. #6

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,987
    Thank Post
    851
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    Haven't got C++ installed on this pc but maybe something like

    int row[] col[]

    just a guess though

  7. #7

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    It works with 1D arrays but not with 2D?!?!
    For 2-D you must pass the size of at least one of the elements e.g.

    void somefunction (int x[][SOMESIZE])




    Not that you'd do anything like this in the first place, but that's probably lesson #27.

  8. #8
    kerrymoralee9280
    Guest
    Thanks PiqueABoo.

    So to sum it up there's no way of doing this in a simple way with a 2D array?

  9. #9

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,611
    Thank Post
    1,226
    Thanked 772 Times in 670 Posts
    Rep Power
    234
    Quote Originally Posted by kerrymoralee9280 View Post
    Need to pass a 2d array to a function in a seperate file.
    Is this what you've specifically been told you have to do, or are you supposed to be working out that you need to define a class with a 2D array in it, instantiate an object of it and pass that into the function (or, indeed, make the function a method of the object)?

    Have you been told yet about the difference between call-by-value, call-by-reference, etc?

    --
    David Hicks

  10. #10
    kerrymoralee9280
    Guest
    We have a 2D array which has been given to us. And part of the assignment is to send the whole array to another function so it can be sorted using a bubble sort.

    The it needs to send it back to main.cpp (all of it) in its sorted form. Once its back in main it goes off to another function to do some more calcs.

  11. #11

    Join Date
    Jan 2006
    Location
    Surburbia
    Posts
    2,178
    Thank Post
    74
    Thanked 307 Times in 243 Posts
    Rep Power
    115
    Ughh.. out of practice talking about this - I probably should have said "at least one of the array dimensions".

    So to sum it up there's no way
    Yes.

    Think of your 2D array as a 1D array of 1D arrays.

    The compiler can handle a (unspecified size) one dimensional array of [thing]s provided it can figure out the precise size of one [thing]. From my declaration it can figure out that:

    "size of a [thing]" = SOMESIZE * sizeof(int);

    Have you been told yet
    That'll be lesson #27.. or at least I hope it is coz I'm sick of seeing classes used in cases where a bit of straight C-like code would have been far simpler and much more efficient.
    Last edited by PiqueABoo; 10th May 2008 at 05:39 PM.

  12. #12
    kerrymoralee9280
    Guest
    Quote Originally Posted by PiqueABoo View Post
    Think of your 2D array as a 1D array of 1D arrays.
    Sorry for being an idiot but what do I need to do to transfer it like this? (if at all possible)

  13. #13


    tom_newton's Avatar
    Join Date
    Sep 2006
    Location
    Leeds
    Posts
    4,458
    Thank Post
    866
    Thanked 845 Times in 667 Posts
    Rep Power
    195
    Quote Originally Posted by kerrymoralee9280 View Post
    Sorry for being an idiot but what do I need to do to transfer it like this? (if at all possible)
    OK, its monday morning, so bear with me on this one. Say you have a 2-d array, size 2*3
    array[0]=[1,2,3]
    array[1]=[4,5,6]

    what it looks like in RAM is [1,2,3,4,5,6]

    It's how you deal with it that makes it 2d. I know, there's special notation like foo[][] but trust me, it's one long list in reality. A pointer points to the first element. If you want to pass an array, generally, you pass a pointer ("the array starts here!) and a length. In your case, you might want to pass pointer, width, length (iyswim), so you can continue to use 2D notation.

    I'm not going to give you code coz it won't help you. If you want to code effectively in C (or C++) you *need* to understand pointers, references and how memory is allocated. I recommend the first few chapters of "beginning assembler" (seriously) think it is published by wiley.

  14. #14
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23
    An example from yours truely about a simple array:

    Code:
    #include "otherfile.h"
    int main (void) {
    char szArray**; 
    
    AllocateMemory(szArray); // call the function in the other file, passing in the 2D char array
    
    // do stuff with 2D array then...
    // dont forget to make a DELETE version for anything you make with NEW
    
    return 0;
    }
    otherfile.h
    Code:
    void AllocateMemory(char** szIn);
    otherfile.cpp
    Code:
    #include "otherfile.h"
    
    void AllocateMemory(char** szIn)
    {
         szIn = new char*[256]; // space for 256 'words' (think about it as 256 pointers to 1D arrays)
         for(unsigned int i=0;i<256;i++)
         {
              szIn[i] = new char[256]; // space for 256 characters on each 'word'
         }
    }

    Disclaimer: I've not tested if this compiles, its just a quick monday morning straight off the top of my head type thing.

    Of course, even if you declare your arrays static like:

    char szArray[256][256]; the pointer stuff still counts.
    Last edited by Friez; 12th May 2008 at 09:07 AM.

  15. #15
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23
    Not necessarily useful for arrays, but along the same topic, here's a bit more about references/pointers:

    Code:
    #include <stdio.h>
    
    void addfive(int* iVar) // this function is asking for the memory address of an integer as its parameter
    {
         iVar[0] += 5; // funny how we're referencing it as if it's the first element of an array hmm?
    }
    
    int main void()
    {
        int iVariable = 5; // look ma! It's not a pointer! No *'s here!
        addfive(&iVariable); // checkit! the & passes the REFERENCE to the variable
        printf("%d",iVariable); // prints 10 but its outside of the function that added it!
    
        return 0;
    }
    I'm sure they would've given you a lecture on this sort of stuff :P
    Last edited by Friez; 12th May 2008 at 09:27 AM.

SHARE:
+ Post New Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •