Thread: Assignment help

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    there's a new problem now though. the program runs properly and makes an identity matrix but then for some reason it prints an extra row of 0's? it's not terminating properly for some reason? there are 9 columns which is correct but there are 10 rows which is incorrect and I don't know why it is doing this?

    here's the exact code which I am using to print it out and everything:

    Code:
    #include <stdio.h>
    
    #define N 10
    
    int main()
    
    {  float a[N][N];
       float *p;
       int zeros;
    
    a[0][0]=1;
    
    for (p = &a[0][1]; p <= &a[N-1][N-1];  p++) {
      zeros++;
      if ( zeros == N+1 ) {
        *p = 1.0;
        zeros = 0;
      } else {
        *p = 0.0;
      }
    }
    
     for (p = &a[0][0]; p <= &a[N-1][N-1]; p++) {
       zeros++;
       if (zeros == N) {
         printf("\n");
         zeros = 0;
       }     
       else
        printf ("%.0f ", *p);
    }
    
    return 0;
    }

  2. #17
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Initialize the variable-zeros before incrementing it.It is having some junk value without initialization

  3. #18
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    ok, I did that but it is still printing an extra row. I figured out that each identity matrix is supposed to have N-1 "1s" printed. I can make it so that it keeps track of the number of ones printed and when it reaches N-1 number of "1s" it will terminate out of the loop. how would I make it break out off this loop though? I know the break command, but putting it only makes it break out of the currecnt "else if (ones == N-1) statement when I tried it.

    this is the code I tried but it just prints the exact same thing as the original:

    Code:
    #include <stdio.h>
    
    #define N 15
    
    int main()
    
    {  float a[N][N];
       float *p;
       int zeros=0, ones=0;
    
    a[0][0]=1;
    
    for (p = &a[0][1]; p <= &a[N-1][N-1];  p++) {
      zeros++;
      if ( zeros == N+1 ) {
        *p = 1.0;
        zeros = 0;
        ones++;
          if (ones == N-1)
            break;
       }
       else {
        *p = 0.0;
      }
    }
    
     for (p = &a[0][0]; p <= &a[N-1][N-1]; p++) {
       zeros++;
       if (zeros == N) {
         printf("\n");
         zeros = 0;
       }     
       else
        printf ("%.0f ", *p);
    }
    
    return 0;
    }
    Last edited by the_winky_files; 10-02-2005 at 03:08 PM.

  4. #19
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by the_winky_files
    how would I make it break out off this loop though? I know the break command, but putting it only makes it break out of the currecnt "else if (ones == N-1) statement when I tried it.



    Code:
    
    for (p = &a[0][1]; p <= &a[N-1][N-1];  p++) {
      zeros++;
      if ( zeros == N+1 ) {
        *p = 1.0;
        zeros = 0;
        ones++;
             }
       else {
        *p = 0.0;
      }
    if (ones == N-1)
            break;
    }
    what you are trying to do with your second for loop???
    Last edited by cbastard; 10-02-2005 at 03:29 PM.

  5. #20
    Registered User
    Join Date
    Sep 2005
    Posts
    52
    Ok, sorry everyone, I was majorly confused. My printf statement was screwed. I had it where when it reached the last element in the row, it would print and new line, which I wanted it to do, but I forgot to make it print the value as well. Thanks for everyone's input 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