#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    2d array help(matrix)


    Hello

    i need assistance.
    i was task by my instructor to solve this problem:

    Write a program that fills the left-to-right diagonal of a square matrix of order 5 (5x5 array) with zeros, the lower left triangle with -1s and the upper right triangle with 1s.

    and the output should be

    0 1 1 1 1
    -1 0 1 1 1
    -1 -1 0 1 1
    -1 -1 -1 0 1
    -1 -1 -1 -1 0


    Use nested loops in the assignment. Manual assignment using initializer is not allowed.

    please i need help
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Help with what?

    Can you for example
    - declare the matrix
    - print a matrix
    - completely fill a matrix with a constant number
    - just write the diagonal of a matrix

    Your homework is YOUR homework

    Post YOUR effort, and we'll guide you for the rest.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    Help with what?

    Can you for example
    - declare the matrix
    - print a matrix
    - completely fill a matrix with a constant number
    - just write the diagonal of a matrix


    Post YOUR effort, and we'll guide you for the rest.
    i just need someone to explain how to do it
    you don't need to code it
    i haven't caught up with the logic yet.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    okay I am so close now

    what i only need now is that the numbers must be 0s, and the lower left triangle must be all -1s, and the upper right triangle 1s.

    how do I do this?

    #include<stdio.h>
    #define ROW 5
    #define COL 5

    int main(){
    int array[ROW][COL];
    int r, c;
    for (r=0; r<ROW; r++){
    for (c=0; c<COL; c++)
    array[r][c] = (r+1)-(c+1);
    }
    for (r=0; r<ROW; r++){
    for (c=0; c<COL; c++)
    printf("%5d", array[r][c]);
    printf("\n");
    }

    printf("\n\n");
    system("pause");
    return 0;
    }
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    The diagonal is when r == c

    Perhaps put this as a condition in your loop.
    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
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    The diagonal is when r == c

    Perhaps put this as a condition in your loop.
    it still gives the same output ?
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Really?
    And where is your latest code, showing us how you used the latest hint?

    Remember, post your code between [code][/code] tags for readability.
    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. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    Really?
    And where is your latest code, showing us how you used the latest hint?

    Remember, post your code between [code][/code] tags for readability.
    more hints please?
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    I'm not going to give you another hint until you've shown us exactly how you tried to use the last hint I gave.

    Or more precisely, how you mis-used it to arrive at "it still gives the same output ?".

    Besides, I find it hard to believe you came up with array[r][c] = (r+1)-(c+1); to fill the array with +/-1 to then come over all begging and clueless when it comes to filling a diagonal with zeros.
    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Code:
    #include<stdio.h>
    #define ROW 10
    #define COL 10
    
    int main(){
        int array[ROW][COL];
        int r, c;
        
        for (r=0; r<ROW; r++){
            for (c=0; c<COL; c++)
                array[r][c] = (r+1)*(c+1);
        }
        for (r=0; r<ROW; r++){
            for (c=0; c<COL; c++)
                printf("%5d", array[r][c]);
            printf("\n");
        }
        printf("\n\n");
        system("pause");
        return 0;
    }
    i got the idea "array[r][c] = (r+1)-(c+1);" with this previous activity

    it's my 1st time learning 2D arrays so yeah I'm not a genius
  20. #11
  21. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    Originally Posted by salem
    The diagonal is when r == c

    Perhaps put this as a condition in your loop.
    So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed at all.

    It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
    Code:
    if ( r == c ) {
        banana;
    } else {
        custard;
    }
    > i got the idea "array[r][c] = (r+1)-(c+1);" with this previous activity
    Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
    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
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed at all.

    It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
    Code:
    if ( r == c ) {
        banana;
    } else {
        custard;
    }
    > i got the idea "array[r][c] = (r+1)-(c+1);" with this previous activity
    Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
    yeah yeah
    and i'm an alien learning c programming
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed at all.

    It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
    Code:
    if ( r == c ) {
        banana;
    } else {
        custard;
    }
    > i got the idea "array[r][c] = (r+1)-(c+1);" with this previous activity
    Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
    4 hours wasn't wasted
    finally figured it out

    Code:
    #include <stdio.h>
    #define row 5
    #define col 5
    
    int main(){
    
        int arr[row][col];
        int i,y,w;
        int r=0, c=0;
    for(i=0;i<5;i++)
    {for(y=0;y<5;y++)
    {arr[i][y]=0;
    for(w=1;w<5;w++)
    { arr[0][w]=1; }
    for(w=2;w<5;w++)
    { arr[1][w]=1; }
    for(w=3;w<5;w++)
    { arr[2][w]=1; }
    for(w=4;w<5;w++)
    { arr[3][w]=1; }
    for(w=5;w<5;w++)
    { arr[4][w]=1; }
    for(w=1;w<5;w++)
    { arr[w][0]=-1; }
    for(w=2;w<5;w++)
    { arr[w][1]=-1; }
    for(w=3;w<5;w++)
    { arr[w][2]=-1; }
    for(w=4;w<5;w++)
    { arr[w][3]=-1; }
    }}
    for(i=0;i<5;i++)
    { printf("\n");
    for(y=0;y<5;y++)
    {
    
    
    if(arr[i][y]==1)
    {
    for(i=0;i<row;i++){
    for(y=0;y<col;y++)
    printf("%5d",arr[i][y]);
    printf("\n");
    }
    }
    if(arr[i][y]==0)
    {
    for(i=0;i<row;i++){
    for(y=0;y<col;y++)
    printf("%5d",arr[i][y]);
    printf("\n");
    }
    }
    if(arr[i][y]==-1)
    {
    for(i=0;i<row;i++){
    for(y=0;y<col;y++)
    printf("%5d",arr[i][y]);
    printf("\n");
    }
    }
    
    
    
    
    }}
    
    
    getch ();
    return 0;
    }
    no thanks to you
  26. #14
  27. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Trivial in j.
    Code:
       i. 5           NB. integers
    0 1 2 3 4
    
       -/~ i. 5       NB. subtraction table of the integers
    0 _1 _2 _3 _4
    1  0 _1 _2 _3
    2  1  0 _1 _2
    3  2  1  0 _1
    4  3  2  1  0
    
       * -/~ i. 5       NB. sign of subtraction table of the integers
    0 _1 _1 _1 _1
    1  0 _1 _1 _1
    1  1  0 _1 _1
    1  1  1  0 _1
    1  1  1  1  0
    
       - * -/~ i. 5     NB. negative sign of subtraction table of the integers
     0  1  1  1 1
    _1  0  1  1 1
    _1 _1  0  1 1
    _1 _1 _1  0 1
    _1 _1 _1 _1 0
    
       |: * -/~ i. 5     NB. transpose sign of subtraction table of the integers
     0  1  1  1 1
    _1  0  1  1 1
    _1 _1  0  1 1
    _1 _1 _1  0 1
    _1 _1 _1 _1 0
    
       * -~/~ i. 5     NB. sign of (passive (meaning swapped) subtraction) table of the integers
     0  1  1  1 1
    _1  0  1  1 1
    _1 _1  0  1 1
    _1 _1 _1  0 1
    _1 _1 _1 _1 0
    
       * -/~ i. _5     NB. sign of subtraction table of the descending integers
     0  1  1  1 1
    _1  0  1  1 1
    _1 _1  0  1 1
    _1 _1 _1  0 1
    _1 _1 _1 _1 0
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo