Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > routine to return number of rows and columns in a matrix

Reply
Thread Tools

routine to return number of rows and columns in a matrix

 
 
kilter
Guest
Posts: n/a
 
      08-23-2006
Anyone know of a routine that will return the number of rows and
columns in a matrix?

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      08-23-2006
kilter wrote:
> Anyone know of a routine that will return the number of rows and
> columns in a matrix?


As C has no matrices, you should explain further what you mean
and want.
 
Reply With Quote
 
 
 
 
kilter
Guest
Posts: n/a
 
      08-23-2006

Nils O. Selåsdal wrote:
> kilter wrote:
> > Anyone know of a routine that will return the number of rows and
> > columns in a matrix?

>
> As C has no matrices, you should explain further what you mean
> and want.


Thanks for your quick reply. The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.

I am a novice really and your help is very much appreciated.

Thank you

 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      08-23-2006

kilter <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

Nils O. Selåsdal wrote:
> kilter wrote:
> > Anyone know of a routine that will return the number of rows and
> > columns in a matrix?

>
> As C has no matrices, you should explain further what you mean
> and want.


> > The idea is that I open a file that has
> > numerical data in a form of a matrix but I don't know a priori the
> > number of columns and rows of this file. I want to know the number of
> > rows and columns of that data matrix, after I read it and store it as a
> > matrix in C, in order to apply operations to its entries.


This will count all the rows and columns in an OPENED FOR READING
COMMA-SEPARATED TEXT data file where the file consists of nothing
else but data rows (if your file has a different format, well, you'll have
to
modify it in some way):

#define LINEMAX 512

unsigned char_idx=0;
unsigned line_length,num_cols=0,num_rows=0;

fgets(line,LINEMAX,fl_strm);
num_rows++;

line_length=strlen(line);
while(char_idx<line_length) {

if(line[char_idx]=='\n'||'\0') {
num_cols++;
break;
}

if(line[char_idx]==',') num_cols++;

char_idx++;
}

while((fgets(line,LINEMAX,fl_strm))!=NULL)
num_rows++;

---
William Ernest Reid



 
Reply With Quote
 
Schraalhans Keukenmeester
Guest
Posts: n/a
 
      08-23-2006
kilter wrote:
> Nils O. Selåsdal wrote:
>> kilter wrote:
>>> Anyone know of a routine that will return the number of rows and
>>> columns in a matrix?

>> As C has no matrices, you should explain further what you mean
>> and want.

>
> Thanks for your quick reply. The idea is that I open a file that has
> numerical data in a form of a matrix but I don't know a priori the
> number of columns and rows of this file. I want to know the number of
> rows and columns of that data matrix, after I read it and store it as a
> matrix in C, in order to apply operations to its entries.
>
> I am a novice really and your help is very much appreciated.
>
> Thank you
>

It depends. If the numerical data is actually stored as a (ascii or
similar) matrix, and each value is separated by spaces, tabs, what have
you, and rows are written line by line you read in one number at a time,
keep track and signal the EOL.
If it's in some binary/structured form you probably want to read in a
structure like that all at once.

If it's just a consecutive row of numbers in a flat file, you may have
to look for delimiter values in the data ending each row, and if these
aren't there you can't be sure how the matrix looks like, as long as
their are multiple m,n where m * n satisfies the total count of numbers
read.

You could have a look at sscanf(), scanf() functions on how to read in
data in a formatted way. Maybe browsing the c-faq, in particular chapter
12 http://www.c-faq.com/stdio/index.html isn't such a bad idea, it must
have some useful stuff on reading in date from files (or basically any
data stream, as C treats them all the same)

For a good primer on C, Kernighan and Ritchie still is considered of
unparallelled value by many, I my self have learnt(ed?) a great deal of
(the little bit of) what I know from
http://publications.gbdirect.co.uk/c_book/
Pretty dated stuff, but clear, concise and to the point nonetheless.

HTH
Sh.
 
Reply With Quote
 
George
Guest
Posts: n/a
 
      08-23-2006
Thanks very much for your replies, they are great!

Regards,

George
Schraalhans Keukenmeester wrote:

> kilter wrote:
> > Nils O. Selåsdal wrote:
> >> kilter wrote:
> >>> Anyone know of a routine that will return the number of rows and
> >>> columns in a matrix?
> >> As C has no matrices, you should explain further what you mean
> >> and want.

> >
> > Thanks for your quick reply. The idea is that I open a file that has
> > numerical data in a form of a matrix but I don't know a priori the
> > number of columns and rows of this file. I want to know the number of
> > rows and columns of that data matrix, after I read it and store it as a
> > matrix in C, in order to apply operations to its entries.
> >
> > I am a novice really and your help is very much appreciated.
> >
> > Thank you
> >

> It depends. If the numerical data is actually stored as a (ascii or
> similar) matrix, and each value is separated by spaces, tabs, what have
> you, and rows are written line by line you read in one number at a time,
> keep track and signal the EOL.
> If it's in some binary/structured form you probably want to read in a
> structure like that all at once.
>
> If it's just a consecutive row of numbers in a flat file, you may have
> to look for delimiter values in the data ending each row, and if these
> aren't there you can't be sure how the matrix looks like, as long as
> their are multiple m,n where m * n satisfies the total count of numbers
> read.
>
> You could have a look at sscanf(), scanf() functions on how to read in
> data in a formatted way. Maybe browsing the c-faq, in particular chapter
> 12 http://www.c-faq.com/stdio/index.html isn't such a bad idea, it must
> have some useful stuff on reading in date from files (or basically any
> data stream, as C treats them all the same)
>
> For a good primer on C, Kernighan and Ritchie still is considered of
> unparallelled value by many, I my self have learnt(ed?) a great deal of
> (the little bit of) what I know from
> http://publications.gbdirect.co.uk/c_book/
> Pretty dated stuff, but clear, concise and to the point nonetheless.
>
> HTH
> Sh.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-23-2006
kilter wrote:
> Nils O. Selåsdal wrote:
>> kilter wrote:
>>> Anyone know of a routine that will return the number of rows and
>>> columns in a matrix?

>> As C has no matrices, you should explain further what you mean
>> and want.

>
> Thanks for your quick reply. The idea is that I open a file that has
> numerical data in a form of a matrix but I don't know a priori the
> number of columns and rows of this file. I want to know the number of
> rows and columns of that data matrix, after I read it and store it as a
> matrix in C, in order to apply operations to its entries.
>
> I am a novice really and your help is very much appreciated.


Since you don't know the size in advance you will have to use dynamic
memory allocation, something that takes a bit of work to get right.
Since this is a 2D matrix you will also need to work out how to
dynamically allocate a 2D array. Question 6.20 of the comp.lang.c FAQ
will help with this. I suggest you also read through the rest of section
6 to help you avoid a lot of other potential areas of confusion.

If you can specify the format of the file it will simplify things if you
say that the first line just tells you how many rows and columns.

Reading the file can be done using fgets (don't use gets) and then parse
it one line at a time. Precisely how you parse it depends on how the
file is formatted.

This should get you started.

The comp.lang.c FAQ is available at http://c-faq.com/ and you should
always check it to see if it answers your questions.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
matrix: exchange rows + exchange columns Alessandro Barracco Ruby 0 01-25-2011 07:26 PM
counting the number of rows & displaying columns that are not nul =?Utf-8?B?RGFuaWVs?= ASP .Net 2 02-27-2007 01:54 PM
Routine for eigenvalues of *complex* valued matrix Emacs Row C++ 2 04-17-2006 04:22 PM
reading the number of unknown rows and columns k_baum217@hotmail.com C Programming 1 03-07-2006 11:53 PM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM



Advertisments