[C] Best way to save matrix counters

This is a discussion on [C] Best way to save matrix counters within the C Programming forums, part of the General Programming Boards category; Hi, I need to know what's the best way to save a matrix counters... I mean, by default I have ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    158

    Best way to save matrix counters

    Hi,
    I need to know what's the best way to save a matrix counters... I mean, by default I have a matrix defined with 25 lines and 80 columns, that's the max the matrix can have, however, my app asks the user to input his own values for lines and columns (which can't be more than 25 and 80 for each one of them)

    I was trying to do the following:

    Code:
    void function2(char (*m)[80]) {
    	int lines, columns;
    
    	// here I'll read the values for lines and columns with scanf
    
    	// (A) read below...
    	m[1][0] = lines;
    	m[0][1] = columns;
    }
    
    void function1(void) {
    	char matrix[25][80];
    
    	function2(matrix);
    }
    (A) That's what I was trying to do, but than I realized I was setting an int value to a char array, which would have unwanted results as you know...

    I was trying this same approach as you do with a simple array... array[50] where 50 is the max but at one point the array will only use 10 values, so you could save the value 10 at array[0] instead of using another unnecessary variable... and all operations with that array would go from array[1] to array[10]... I know i'm not explaining this in a good way but it's hard for me to type all this programming stuff in english... but I hope you understand.

    So, what's the best way to save the counters for the matrix values? I know a simples solution would be to save them in 2 variables and pass them as arguments thorugh all the functions I need, but I was hoping to do that in a simple way, if possible...
    Last edited by Nazgulled; 03-23-2006 at 01:05 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > (which can't be more than 25 and 80 for each one of them)
    So do
    void function2(char (*m)[80], int requestedRows, int requestedCols );

    You end up with some wasted space, but it's easier on the coding.

    > I was setting an int value to a char array, which would have unwanted results as you know...
    So make it an int array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    but passing the lines and columns through functions is something I didn't want cause it's not just one function, it's several... that's why I was hoping to have a better option to do it...

    I don't know exactly how can I do what I really want with an int array, but I just thought of something and will take a look into the code to see what I can do...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    How else are you proposing to store them - as the first couple of elements of the matrix?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    Quote Originally Posted by Salem
    How else are you proposing to store them - as the first couple of elements of the matrix?
    yep, for lines I just saved it on position [0][1] and columns on [1][0]... anyway, I solved my problem, the matrix is now of int type and whenever I need some character I'll just save the ascii decimal value.

    thanks

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why not use a structure?
    Code:
    struct matrix
    {
       int rows, cols;
       char data[25][80];
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    this is for school, we haven't reached that yet so I can't use them...

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    By using the positions you specify, do you know that you can never have a full 25x80 matrix.
    Code:
    m[ 0][0] m[ 0][1] m[ 0][2] ... m[ 0][78] m[ 0][79]
    m[ 1][0] m[ 1][1] m[ 1][2] ... m[ 1][78] m[ 1][79]
       . .        .             .         .
       . .        .             .         .
       . .        .             .         .
    m[23][0] m[23][1] m[23][2] ... m[23][78] m[23][79]
    m[24][0] m[24][1] m[24][2] ... m[24][78] m[24][79]
    Have you really created a 24x79 matrix that wastes an additional 102 entries?
    Last edited by Dave_Sinkula; 03-24-2006 at 04:43 PM. Reason: [1] Heh. Transposed the array. [2] More coloring.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21