Thread: how to display a matrix ?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    how to display a matrix ?

    hello everybody!
    I have a problem which i need some help to solve. Suppose i have a matric with char matrix[3][3] element. i want to write a function which take this matrix as argument and display it in this way written below (with an empty matrix)
    | | | |
    | | | |
    | | | |

    and for example if matrix[1][2] is 'o' and matrix [0][1] is '+' (the rest is still empty) i want to display
    | | +| |
    | | |o|
    | | | |
    can somebody tell me/give me some hint how i should design/implement this function?

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    For loops and putchar() should suffice. Function prototype could be void print_matrix (char matrix[3][3])

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    ok but how do i do if

    say i have a char matrix[3][3] and i update only oneement!
    is there any smart way to print only this new element and not iterate over the matrix again. I want still to print all the element in the matrix but when i change a new element to use the print from before and update only this new element?
    i hope you understand what i mean?

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    If you're printing the matrix in a console window then you'd need to find a way to jump to the element that is to be changed and overwrite that character.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    ok but how do i solve this

    i can't do it with a for sats like th example this example (se below not complete code just example to show )
    Code:
    int i, j;
    for (i=0;i <3; i++)
     for (j=0;j<3; j++)
     printf("| %c",putchar(matrix[i][j]));
    because i will lose the position in x direction and always write it in the same position. how can i do so i can move the position for both | and the character when i want to write a new character?
    Last edited by kobra_swe; 08-17-2006 at 06:25 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by kobra_swe
    i can't do it with a for sats like th example this example (se below not complete code just example to show )
    int i, j;
    for (i=0;i <3; i++)
    for (j=0;j<3; j++)
    printf("| %c",putchar(matrix[i][j]));
    because i will lose the position in x direction and always write it in the same position. how can i do so i can move the position for both | and the character when i want to write a new character?
    I'll try and bear with you as I'm sure you speak English better than I speak Swedish, but I'm having trouble understanding this. Try providing pictures as you did in your first post, they help. Also, please use CODE tags.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    ok here is another attempt

    sorry i am quite tired and therefore the formulation in english is not the best you can see from me this time
    As i mention before i want to display the content of a char matrix (3*3 in this example to simplify things) so i can display the content in this way (assume that matrix[0][1] contain '+'
    the rest is empty
    | |+| |
    | | | |
    | | | |
    if i do it with a for statement
    for (j=0; j<3;j++)
    for(i=0; i<3; i++)
    {
    printf("| %c",putchar(matrix[i][j]));
    if (i==2)
    printf("| \n");
    }
    i will never move from the first positon and i will only write on the first position
    se below
    |char|
    |char|
    |char|
    and char will be the content of the element matrix[i][2];
    so my question is how do i move the position so i can write | and the character on the right position
    i hope you understund my question better now

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    From my understanding, the OP doesn't want to redraw the entire matrix for each update to it.

    What you're trying to do is OS and compiler specific. As already pointed out, you'll have to use something like gotoxy, the curses library, or something similar. Then, whenever you update your matrix, call the appropriate function and "repaint" the cell you've just updated.

    You may want to write a wrapper function which does both for you. It would fill the appropriate cell, then call your repaint function.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    See, when you don't use code tags, noone can read you code properly, and things like the fact that you have a putchar in a printf slips by some.
    Code:
        int i, j;
        for (j=0; j<3;j++)
           for(i=0; i<3; i++) {
              printf("|%c",matrix[i][j]);
              if (i==2)
                 printf("| \n");
           }
    This should output your result fine. Use [CODE] tags!

    To Quzah's response: Is that what he wanted? I couldn't for the life of me understand what he was saying.

    Anyway, if you go into the Linux console and run some very professional programs that do alot of updating to the screen, you'll find that if you scroll up a bit, you'll see the last update of the screen that appeared to be changing infront of your eyes. It's honestly not a bad thing to have a program just page down a bunch and write again. It's worked in the past it will work in the future.
    Last edited by SlyMaelstrom; 08-17-2006 at 02:08 AM.
    Sent from my iPadŽ

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have no idea if that's what he really wanted. His last post came in when I was typing mine. *shrugs*


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    19
    ok but my question is more simple than so!
    I just want to display every element in the matrix as i described before and i can accept to redraw the entire matrix everytime i make an update in the matrix.
    assume i have a function
    Code:
     
    void repaint (char matr[3][3]))
    {
    int i, j;
        for (j=0; j<3;j++)
           for(i=0; i<3; i++) {
              printf("|%c",matrix[i][j]);
              if (i==2)
                 printf("| \n");
           }
    }
    my problem is that in the printf("|%c",matrix[i][j]) statement i want to print the sign | and the character to a new position every time i make an interation in the inner loop. This is my simple question for now
    sorry but i don't know how to display code tags and the intendetion is therefore so ugly.
    Last edited by kobra_swe; 08-17-2006 at 06:27 AM.

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I see the word "question" a lot... yet, I see no question mark.

    What your saying, really, really doesn't make sense. It seems to me like your saying, for some reason your program is writing a hidden carrige return after each printf() statement, however, I'm sure that's not true. Let me make a template of what I want you to do.
    Code:
    The initial matrix:
    | | |
    | | |
    | | |
    
    What happens next:
    
    The matrix after that happens:
    | | |
    | | |
    | | |
    
    What happens next:
    
    The matrix after that happens:
    | | |
    | | |
    | | |
    Now, quote my post, copy the template INCLUDING THE CODE TAGS, and fill it in. You can repeat the steps as many times as you want.

    Now are you saying, you want the output to look like this:
    Code:
    |2|8|4|
    |6|1|7|
    |9|9|5|
    but for some reason you get this:
    Code:
    |4|
    |7|
    |5|
    ? If that's the case, then I can't help you because that code looks, and by the way, outputs fine to me.
    Last edited by SlyMaelstrom; 08-17-2006 at 02:23 AM.
    Sent from my iPadŽ

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    int i,j;
    
    for (i=0;i<numRows;i++)
    {
      for (j=0;j<numCols;j++)
      {
        printf("|%c",matrix[i][j]);
      }
      printf("|\n");
    }
    You don't need to check for the i==2 part. When the inner loop is completed you know you are at the end of the current row. Just print your last character and a carriage return to prepare for the next line.

    However, may I suggest:

    Code:
    int iOffset;
    int i;
    int row,col;
    
    iOffset=0;
    row=0;
    col=0
    
    for (i=0;i<numElements;i++)
    {
      printf("|%c",matrix[iOffset]);
      iOffset++;
      col++;
      if (col>numCols)
      {
        col=0;
        row++;
        printf("|\n");
      }
    }

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    ok SlyMaelstrom you are right

    but can somebody explain/tell me why i get a run time crash when i call this
    Code:
    void test(char matrix[][3]) {
    	 int i, j;
        for (j=0; j<3;j++)
           for(i=0; i<3; i++) {
              printf("|%c",matrix[j][i]);
              if (i==2)
                 printf("| \n");
           }
    	}
    int main () {
    	char ma[3][3];
    	int i, j;
    	for(i=0; i<3; i++)
    		for(j=0;j<3;j++)
    			ma[i][j]=' ';
    	ma[0][0]='*';
        ma[0][1]='+';
    	 ma[2][0]='i';
        ma[1][0]='k';
    test(ma[3][3]);
    but it doesn't when i do like this

    Code:
    int main () {
    char ma[3][3];
    	int i, j;
    	for(i=0; i<3; i++)
    		for(j=0;j<3;j++)
    			ma[i][j]=' ';
    	ma[0][0]='*';
        ma[0][1]='+';
    	 ma[2][0]='i';
        ma[1][0]='k';
    	for (j=0; j<3;j++)
           for(i=0; i<3; i++) {
              printf("|%c",ma[j][i]);
              if (i==2)
                 printf("| \n");
    Last edited by Ken Fitlike; 08-17-2006 at 03:25 AM. Reason: inserted code tags

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    but can somebody explain/tell me why i get a run time crash when i call this
    Not until you start using code tags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM