C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 1st, 2004, 07:26 AM
paner paner is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 paner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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);
}

Reply With Quote
  #2  
Old September 1st, 2004, 08:22 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
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-

Reply With Quote
  #3  
Old September 1st, 2004, 08:27 AM
paner paner is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 paner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile

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:
Originally Posted by kode_monkey
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-

Reply With Quote
  #4  
Old September 1st, 2004, 08:30 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Can you post a sample from the data file, that might help us figure out why floats work and ints don't.

-KM-

Reply With Quote
  #5  
Old September 1st, 2004, 08:46 AM
paner paner is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 paner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Red face

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.

Reply With Quote
  #6  
Old September 1st, 2004, 09:01 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 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-

Reply With Quote
  #7  
Old September 1st, 2004, 09:09 AM
paner paner is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 paner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #8  
Old September 1st, 2004, 09:37 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
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-

Reply With Quote
  #9  
Old September 1st, 2004, 09:45 AM
paner paner is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 5 paner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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..

Reply With Quote
  #10  
Old September 1st, 2004, 11:21 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
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-

Reply With Quote
  #11  
Old February 21st, 2005, 11:09 PM
JediArmadillo JediArmadillo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 2 JediArmadillo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 25 sec
Reputation Power: 0
Quote:
Originally Posted by paner
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.


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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Reading matrix data to 2d array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT