![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Design a function to write the data in a matrix into filesHi,
My coding objective is quite simple: design a function to write the data in a matrix into files. And here is my code: CPP / C++ / C Code:
Errors occured as following during compilation: Code:
Any one can tell me what's the matter and tell me how to fix it? Thank u in advance. Last edited by admin : 12-Jul-2009 at 04:25.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Design a function to write the data in a matrix into filesQuote:
CPP / C++ / C Code:
See Footnote. Note that you don't have to tell it the first dimension. (It doesn't hurt if you do it, but it doesn't help either.) With that function definition, the routine will work no matter what the size of the first dimension is. In general for multi-dimensional arrays, if you want to pass a pointer to a function, you must give the array name and the sizes of all dimensions except the first. The compiler simply must have this information in order to generate code to access the array elements. Period. Full stop. In order to have a function that will work on different sizes of matrices (something other than "x" by three), you will have to work with pointers and memory allocation to get your "arrays," not with declared arrays. Now, since this is C++ (not C), you might consider using vectors instead of 1-dimensional arrays and vectors of vectors instead of 2-dimensional arrays. The user notation can be made the same (using mat[i][j] to access the elements of the "matrix") bit for many people it's actually easier to implement. I think using pointer arrays is a valuable thing to learn, but I think that using vectors and vectors of vectors is also important. Chacun à son goût! Regards, Dave Footnote: In C and C++ there really are no such things as multi-dimensonal arrays. A 2-D array is actually an array of 1-D arrays. Note that for all arrays in C and C++ the name of the array is a pointer to the first element in the array. In your example, with myMat[3][3], the following is true: myMat[0] is a pointer to a 3-element array. The elements are myMat[0][0], myMat[0][1], myMat[0][2] myMat[1] is a pointer to a 3-element array. The elements are myMat[1][0], myMat[1][1], myMat[1][2] myMat[2] is a pointer to a 3-element array. The elements are myMat[2][0], myMat[2][1], myMat[2][2] The following is completely, absolutely, exactly equivalent for the function definition: CPP / C++ / C Code:
This says that mp is a pointer to an array of three floats, and the results are exactly, absolutely, completely equivalent to the other definition. The actual preferred notation for initializing your array would be: CPP / C++ / C Code:
The elements are stored in sequential memory locations whether you put the inner braces or not, so they aren't absolutely required (some compilers give warnings, but the code works without them), but writing it like this emphasizes the "array of arrays" principle. |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Flex and bison coding | lucky88star | C++ Forum | 5 | 24-Dec-2007 12:57 |
| Need Help with input files. | Efferus | C++ Forum | 2 | 24-Nov-2007 17:19 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 07:17 |
| Problem with writing to a file | hellhammer | C Programming Language | 7 | 09-Jun-2005 12:30 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The