Thread: Identity Matrix

  1. #1
    Unregistered
    Guest

    Identity Matrix

    I'm having trouble printing an identity matrix. What am I doing wrong?

    2
    3 #include <stdio.h>
    4
    5 #define N 10
    6
    7 main()
    8 {
    9 int a[N][N];
    10 int *p;
    11 int count = 0;
    12
    13 for (p = &a[0][0]; p <= &a[N-1][N-1]; p++)
    14 count += 1;
    15
    16 if (N == count)
    17 *p = 1;
    18 else
    19 *p = 0;
    20
    21 for(p = &a[0][0]; p <= &a[N-1][N-1]; p++)
    22 printf("%d ", *p);
    23
    24 printf("\n");
    25
    26 return 0;
    27 }

  2. #2
    Unregistered
    Guest

    Question

    Do you mean display a 10x10 matrix?
    ==========================

    #include <stdio.h>

    #define N 10

    void init_matrix(int matrix[N][N]); /* fill matrix */
    void prnt_matrix(int matrix[N][N]); /* display matrix */

    main()
    {
    int a[N][N];

    init_matrix(a);
    prnt_matrix(a);

    return 0;
    }

    /**************************/
    /* init_matrix() */
    /**************************/
    void init_matrix(int matrix[N][N])
    {
    int i, j;

    for (i=0; i<N; i++)
    for (j=0; j<N; j++)
    matrix[i][j] = j;
    }

    /**************************/
    /* prnt_matrix() */
    /**************************/
    void prnt_matrix(int matrix[N][N])
    {
    int i, j;

    for (i=0; i<N; i++)
    {
    for (j=0; j<N; j++)
    printf("[%d]", matrix[j][i]);
    printf("\n");
    }
    }

  3. #3
    Unregistered
    Guest
    Yes, that's it, but I have to use a pointer instead of i and j.

  4. #4
    Unregistered
    Guest

    Question

    What about this? (using my smaller 2x2 array)
    ==============

    #include <stdio.h>

    #define N 2

    main()
    {
    int *ptr, i;
    int arr[N][N]={1,2,3,4};

    ptr = arr;

    for (i=0; i<N*N; i++)
    printf("[%d] ", *ptr + i);

    return 0;
    }
    ================

    I can't seem to initialise the 2x2 array with a for() loop using
    *ptr + i
    prints out fine though using pointer arithmetic

  5. #5
    Unregistered
    Guest
    I don't know what I'm playing at there - sorry, that prog is a pile of poop and just doesn't work =) (damn you pointers)
    =======================================
    #include <stdio.h>

    #define N 2

    main()
    {
    int *ptr, i;
    int arr[N][N];

    ptr = arr;

    for (i=0; i<N*N; i++)
    {
    ptr = ptr + (sizeof(int) * i);
    *ptr = i;
    }

    ptr = (int*) arr;
    for (i=0; i<N*N; i++)
    {
    ptr = ptr + (sizeof(int) * i);
    printf("[%d] ", *ptr);
    }

    getchar();
    return 0;
    }
    ==========================
    Right that one actually works - it fills the matrix up too

  6. #6
    Unregistered
    Guest

    Thumbs up

    Thanks!!

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    This uses only pointers , but I am not sure whether this is what you wanted

    #include <stdio.h>
    #define N 10

    int main(void)
    {
    int matrix[N][N]={0},i,j;
    for(i=0;i<N;i++) matrix[i][i]=1;
    for(i=0;i<N;i++)
    {
    for(j=0;j<N;j++)
    printf(" %d ",*(*(matrix+i)+j) );
    putchar('\n');
    }
    return 0;
    }

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Sorry , I understood what you meant , the above clearly is not a solution .
    Stupid me .

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Salem
    > Yes, that's it, but I have to use a pointer instead of i and j.
    This can't be done in ANSI-C (although it probably works in most real implementations).

    http://www.eskimo.com/~scs/C-faq/q6.19.html

    There is always the implicit assumption that a[i+1][0] immediately follows a[i][n-1]
    Can't you kind of simulate a 2D array with pointers where you pass say an int**, which points to an array of int *s, each which points to an array of ints?

    Something like:

    int i, ** matrix;
    matrix = new int * [N];
    for (i = 0; i < N; i++){
    matrix[i] = new int[N];
    }

    // now, you can use matrix[i][j] for read/write

    Now, I know new is C++ only, but you can do the exact same thing with malloc(): essentially allocate space for an array of N pointers to arrays each containing N integers. Make an array of pointers to arrays, essentially.

    Now, this array will almost certainly NOT be contiguous in memory, but it should work, unless I'm just being retarded.

    Or am I completely misunderstanding the question and answer?
    Last edited by The V.; 10-11-2001 at 10:15 AM.

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    about the first version of the code you post 'Unregistered'

    change the text condition on the line 16
    Code:
    14 {
    15 count+=1;
    16 if ((count % N) == 1) 
              *p = 1;
         else
              *p = 0;
         }
    here is tested for 'one' becouse you increment 'count' variable BEFORE the test and also add brackets - '{' and '}' around the statement where you try to assing 1 to elements of the main diagonal of the matrix so it will be executed within the for statement :-)



    so when count is divided to N without a reminder set the item to 1
    otherwise initialize i with zero.

    when you dump the identity matrix, just use the same tehnique -
    test the same condition and output an optional '/n'
    Last edited by damyan; 10-11-2001 at 01:52 AM.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    This works, but how do I print it so that there are only 10 characters per line?

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Ah, I see what you're saying, regarding flattening 2D arrays. And you're right, of course, with the ** method you can only assume that each row of the array is contiguous.

    Although, with clever use of malloc, you could MAKE the whole array contiguous, come to think of it, by allocating all the array's data memory in one call, and then manually assigning pointers to the beginnings of each row.

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. Slots Simulator. Identity Matrix.
    By valaris in forum C Programming
    Replies: 1
    Last Post: 07-21-2008, 07:39 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 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