GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jul-2009, 03:23
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

Design a function to write the data in a matrix into files


Hi,
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:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int matwrite(float* mp,int row_sz,int col_sz,string mat_file);

int main()
{
	// create a simple binary file first for later matrix operation.
	float myMat[3][3] = {1,2,3,4,5,6,7,8,9};
	string file_name = "mat.bin";
	matwrite(myMat,3,3,file_name);	
}
int matwrite(float* mp,int row_sz,int col_sz,string file_name)
{
    ofstream mat_file(file_name.c_str(),ios::binary);
	for (int row=0; row<row_sz; row++)
	{
		for (int col=0; col<col_sz; col++)
		{
			mat_file.write(reinterpret_cast<char*>(&mp[row][col]),sizeof(float));
		}
	}
	cout<<"The matrix has been successfully written to file."<<endl;
	return 0;
}

Errors occured as following during compilation:
Code:
e:\code zone\practice code\matrix_class\matrix\matrix_class\mat_demo.cpp(14) : error C2664: 'matwrite' : cannot convert parameter 1 from 'float [3][3]' to 'float *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast e:\code zone\practice code\matrix_class\matrix\matrix_class\mat_demo.cpp(23) : error C2109: subscript requires array or pointer type

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  
Old 12-Jul-2009, 09:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Design a function to write the data in a matrix into files


Quote:
Originally Posted by nanchuangyeyu
Hi,
My coding objective is quite simple: design a function to write the data in a matrix into files.
Here's the thing: In order for the compiler to generate code in the function to acess elements of a 2-dimensional declared array, it hast to know the size of the second dimension. The following will work:

CPP / C++ / C Code:
int matwrite(float mp[][3],int row_sz,int col_sz,string mat_file);
.
.
.
	matwrite(myMat,3,3,file_name);	
.
.
.

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:
int matwrite(float (* mp)[3],int row_sz,int col_sz,string mat_file);

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:
    float myMat[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
This emphasizes that myMat is an array of three arrays. Each of the three is enclosed in braces.

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 GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 19:06.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.