routine to return number of rows and columns in a matrix

Discussion in 'C Programming' started by kilter, Aug 23, 2006.

  1. kilter

    kilter Guest

    Anyone know of a routine that will return the number of rows and
    columns in a matrix?
    kilter, Aug 23, 2006
    #1
    1. Advertising

  2. 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.
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Aug 23, 2006
    #2
    1. Advertising

  3. kilter

    kilter Guest

    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
    kilter, Aug 23, 2006
    #3
  4. kilter

    Bill Reid Guest

    kilter <> wrote in message
    news:...

    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
    Bill Reid, Aug 23, 2006
    #4
  5. 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.
    Schraalhans Keukenmeester, Aug 23, 2006
    #5
  6. kilter

    George Guest

    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.
    George, Aug 23, 2006
    #6
  7. kilter

    Flash Gordon Guest

    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
    Flash Gordon, Aug 23, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. helpful sql
    Replies:
    0
    Views:
    795
    helpful sql
    May 19, 2005
  2. =?Utf-8?B?RGFuaWVs?=
    Replies:
    2
    Views:
    321
    =?Utf-8?B?RGFuaWVs?=
    Feb 27, 2007
  3. Replies:
    1
    Views:
    328
    A. Sinan Unur
    Mar 7, 2006
  4. Emacs Row
    Replies:
    2
    Views:
    334
    Emacs Row
    Apr 17, 2006
  5. Alessandro Barracco

    matrix: exchange rows + exchange columns

    Alessandro Barracco, Jan 25, 2011, in forum: Ruby
    Replies:
    0
    Views:
    150
    Alessandro Barracco
    Jan 25, 2011
Loading...

Share This Page