Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 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 #include #include #include 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: 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<................... Cheers guys - if I haven't been clear please ask what you need to know. HT
SYNACK Posted May 10, 2008 Posted May 10, 2008 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
Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 What I have tried is: 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: void menu1(int iNumMonths,int iNumProds,int iAmounts[iNumMonths][iNumProds]) But this obviously isn't working.
SYNACK Posted May 10, 2008 Posted May 10, 2008 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.
Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 Thanks, Used this - Declare int array parameter for a function without indicating the array length : function parameters « Function « C++ Tutorial It works with 1D arrays but not with 2D?!?!
SYNACK Posted May 10, 2008 Posted May 10, 2008 Haven't got C++ installed on this pc but maybe something like int row[] col[] just a guess though
PiqueABoo Posted May 10, 2008 Posted May 10, 2008 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.
Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 Thanks PiqueABoo. So to sum it up there's no way of doing this in a simple way with a 2D array?
dhicks Posted May 10, 2008 Posted May 10, 2008 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
Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 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.
PiqueABoo Posted May 10, 2008 Posted May 10, 2008 (edited) 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. Edited May 10, 2008 by PiqueABoo
Guest kerrymoralee9280 Posted May 10, 2008 Posted May 10, 2008 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)
tom_newton Posted May 12, 2008 Posted May 12, 2008 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.
Friez Posted May 12, 2008 Posted May 12, 2008 (edited) An example from yours truely about a simple array: #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 void AllocateMemory(char** szIn); otherfile.cpp #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. Edited May 12, 2008 by Friez
Friez Posted May 12, 2008 Posted May 12, 2008 (edited) Not necessarily useful for arrays, but along the same topic, here's a bit more about references/pointers: #include 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 Edited May 12, 2008 by Friez
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now