Thread: Assignment help

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    52

    Assignment help

    Ok, I have an assignment for my programming class but I really don't understand what the question is asking of me. I was wondering if someone could help me understand what I'm actually supposed to do. Here is the question:

    "Secion 8.2 had a code fragment in which two nested for loops initialized the array ident for use as an identity matrix. Rewrite this code, using a single pointer to step through the array one element at a time. Hint: Since we won't be using row and col index variables, it won't be easy to tell where to store 1. Instead, we can use the fact that the first element of the array should be 1, the next N elements should be 0, the next element should be 1, and so forth. Use a variable that keeps track of how many consecutive 0s have been stored; when the count reaches N, it's time to store 1."

    Here is the code it refers to from section 8.2:

    Code:
    #define N 10
    
    float ident [N] [N];
    int row, col;
    
    for (row = 0; row < N; row++)
       for (col = 0; col < N; col++)
          if (row == col)
             ident [row] [col] = 1.0;
          else
             ident [row] [col] = 0.0;
    I don't know if any of you guys will need this but this could be some helpful information from the section 8.2:

    "In mathematics, an identity matrix has 1's on the main diagonal, where the row and column index are the same, and 0's everywhere else."

    Please help. I have no idea what this question is asking me to do maybe because I don't understand the idea of an "identity matrix."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But surely you can see what your example code does and how it sets ident [row] [col] = 1.0;
    Draw an example out on paper and see what it looks like

    Just do the same, with the suggested counters to do the same thing

    float *flattenedArray = &ident[0][0];

    Oh, and you might like to mention to your tutor that array flattening isn't within the standard.
    http://www.eskimo.com/~scs/C-faq/q6.19.html
    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.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I was just wondering if this might be good start or somewhat close to what I need?

    Code:
    #define N 10;
    
    float a[N] [N];
    int *p;
    
    for (p = &a[0] [0]; p <= a[N-1] [N-1];  p++)
       [  Something here, I'm not really sure what though.  Some conditions
           that will make this thing an identity matrix.  ];

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, something like that - when you've fixed the types, and added the counters as per the hints in the assignment.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    what do you mean by fix the types?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you have an int pointer and an array of floats....
    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.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Would this be an identity matrix then?

    10000000
    01000000
    00100000
    00010000
    00001000
    00000100
    00000010
    00000001

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I'm not sure ? I don't really understand what they are.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    From dictionary.com

    identity matrix
    n.
    A square matrix with 1's along the diagonal from upper left to lower right and 0's in all other positions.

    or

    identity matrix

    n : a scalar matrix in which all of the diagonal elements are unity [syn: unit matrix]

    So I think that my example was right...hope it helps you somehow!

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    yes, I used the orginal code and printed it and that's exactly what it looked like. Now the question is, how do I get it do do it with pointers. Can someone please just give me a hint because the one in the book isn't working for me. I understand what the hint is saying but putting it in to code is giving me some trouble.

    is this on the right track:

    Code:
    #include <stdio.h>
    
    #include <stdio.h>
    
    #define N 10
    
    int main()
    
    {  float a[N][N];
       float *p;
       int zeros;
    
    a[0][0]=1;
    
    for (p = &a[0][0]; p <= &a[N-1][N-1];  p++)
      for (zeros = 0; zeros <= N; zeros++)  
         { if (zeros == N)
               *p = 1.00;
             else
               *p = 0.00;
         }
    
    return 0;
    
    }
    For some reason this code makes every element of the matrix equal to 1.
    Last edited by the_winky_files; 10-02-2005 at 02:15 PM.

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah, you are only missing one thing (besides the typos and errors).. shouldnt you be incrementing p in the second for loop?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I don't understand why I would want to increment p again in the second for loop?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > For some reason this code makes every element of the matrix equal to 1.
    Because your inner loop finishes when zeros == N

    You're on the right track, but you only need a single loop
    Code:
    for (p = &a[0][0]; p <= &a[N-1][N-1];  p++) {
      zeros++;
      if ( zeros == N ) {
        *p = 1.0;
        zeros = 0;
      } else {
        *p = 0.0;
      }
    }
    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.

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    I understand all but the:
    Code:
    zeros = 0;
    What is this line of code for? Thanks for the help by the way. I tried something like this with one for loop but I didn't know how to make zeros++ increment with p++. I was doing:
    Code:
    for (p = &a[0][1]; p <= &a[N-1][N-1];  p++, zeros++)
    which was giving me weird output.

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, nevermind. I understand the line now. if it gets to that point then it needs to reset back to 0 so it can post the 0s over again. sorry for the confusion. thank you for the help though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM