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. Advertisements

  2. 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. Advertisements

  3. kilter

    kilter Guest

    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

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

    Flash Gordon Guest

    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, Aug 23, 2006
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.