Thread: Help? [2dArray]

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

    Join Date
    Nov 2012
    Location
    ミンダナオ、フィリピン
    Posts
    4
    Rep Power
    0

    Help? [2dArray]


    May I know how to make a right to left square matrix?
    I only made a left to right so far orz (I'm still learning so don't bite my head off please!)

    This is my left to right code:


    Code:
    #include<stdio.h>
    #define COL 5
    #define ROW 5
    
    int main(){
        
    int array[COL][ROW];
    int i,k;
    
    for(k=0;k<COL;k++){
    for(i=0;i<ROW;i++){
                      
    if(k==i){
             array[k][i]=0;
    }
    if(k<i){
             array[k][i]=1;     
    }
    if(k>i){
             array[k][i]=-1;
    }
    }
    }
                                
    for(k=0;k<COL;k++){
    for(i=0;i<ROW;i++)                
    printf("%5d",array[k][i]);
    printf("\n");
    
    }
    
    printf("\n");                   
                       
    system("pause");
    return 0;
    }
    May I know what operation was to reverse it to right to left?
    It should be like this...
    1 1 1 1 0
    1 1 1 0 -1
    1 1 0 -1 -1
    1 0 -1 -1 -1
    0 -1 -1 -1 -1
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,963
    Rep Power
    481
    yes, you're actually allowed to use arithmetic to compute indexes.
    Code:
    #include<stdio.h>
    #define COL 5
    #define ROW 5
    
    int main(){
        
      int array[COL][ROW];
      int i,k,j;
    
      for(k=0;k<COL;k++){
        for(i=0;i<ROW;i++){
          j = ROW-(1+i);
          if(k==i)
    	array[k][j]=0;
          if(k<i)
    	array[k][j]=1;     
          if(k>i)
    	array[k][j]=-1;
        }
      }
                                
      for(k=0;k<COL;k++){
        for(i=0;i<ROW;i++)                
          printf("%5d",array[k][i]);
        printf("\n");
    
      }
    
      printf("\n");                   
                       
      system("pause");
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    ミンダナオ、フィリピン
    Posts
    4
    Rep Power
    0
    AHH! THANK YOU SO MUCH! マジどうもありがとございます!ww

    Comments on this post

    • b49P23TIvg agrees : google translation is awesome.

IMN logo majestic logo threadwatch logo seochat tools logo