| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Reading matrix data to 2d array
The input is a similarity matrix.
I have tried the following to read in the file and store it into a 2 d array It is able to compile yet not able to output. The error doesn't lie in the input file. Pls help. int main(int argc, char *argv[]) FILE *file; init_M(ncol); if ( (file = fopen( FN, "r" )) == NULL ) errorfile( fn_dataset, "read" ); printf("scanning"); for (i=0;i<ncol;i++){ for (j=0;j<ncol;j++) { fscanf( file, "%d", &M ); } } for (i=0;i<ncol;i++){ for(j=0;j<ncol;j++){ printf("%d",M[i][j]); } } fclose(file); } |
|
#2
|
|||
|
|||
|
This line -
fscanf( file, "%d", &M ); probably needs to contain &(M[i][j]) instead of just &M although I haven't got a compiler here to test it. Hope this helps, -KM- |
|
#3
|
|||
|
|||
|
yup.typo error.has put that in.
I tried using int for the 2 d array it doesn't work, however, upon changing to float, it works. What is the reason behind it? <puzzeled> anyway, thanks alot kode_monkey. Quote:
|
|
#4
|
|||
|
|||
|
Can you post a sample from the data file, that might help us figure out why floats work and ints don't.
-KM- |
|
#5
|
|||
|
|||
|
Just a question, can I store a string in a 1d array?
for eg, a[0]={string}? I am facing a problem with insufficient memory as the data to be processed is too large if i store as array.Thanks. |
|
#6
|
|||
|
|||
|
It depends what your array is of. A string is an array of characters itself (well in c anyway) but its possible to have an array of strings (the class) in c++. Could you elaborate a bit more please.
-KM- |
|
#7
|
|||
|
|||
|
M[][] is a similarity matrix with some values one.
For example, the matrix with 7 nodes as shown below. 1 2 3 4 5 6 7 1 x 2 1 x 3 1 1 x 4 0 1 1 x 5 1 1 1 1 x 6 0 0 1 0 1 x 7 0 0 0 0 0 1 x I have tried using a 2 d array to store such as M[1][1] = 2;M[1][2] = 3;M[1][3] = 0;M[1][4] = 0;M[1][5] = 1;etc.However, my dataset would have to many nodes to deal with thus insufficient memory.Right now, I would like to change it to M[1]={2,3}, M[2]={3,4,5},M[3]={4,5,6} etc so that I would have adequate memory to process the data. As for my knowledge so far, string[size] is available and whatever in the brackets is the size instead of the array index thus I'm stuck. |
|
#8
|
|||
|
|||
|
If you've got the memory available to store it in string format then I'm pretty sure you would have enough to have the numbers stored in memory in a 2d array of integers.
-KM- |
|
#9
|
|||
|
|||
|
wouldn't it be alot of wastage for 2 d array as those with zeros were also taken into account?
for string, it would be minimising storage sin't it?Thanks for yr guidance.. |
|
#10
|
|||
|
|||
|
Just how big are we talking here?
Generally I would go with the wasted space rather than extra processing required to manipulate the strings. If space is an issue consider using each integer in memory to hold more than one number (if they are small, which you're example suggests they are). Or maybe dynamically allocate the memory so you don't allocate (or use) the second half of the matrix. -KM- |
|
#11
|
|||
|
|||
|
Quote:
if u declared the array in main: char array[size]; then to store a string u simply state: array = "string"; since array names are pointers to the first memory location and array[0] is the first spot of teh array capable of holding only 1 character('c'), integer(15), double(14.5) etc. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Reading matrix data to 2d array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|