Auto generate 2 dimensional Array,

Currently I have a 2D array, with values of 1 or 0. I hard coded 0 and 1’s in their position in the array as i need them in this sequences.

  //{ { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },N
  //    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },S
 //  { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 }, W
//  { 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 } };E

Is there a better solution to do this instead of placing each value of the array exactly like in the example above. As if i had to do this for 100 X 100 array it would be take several days

Answer

you want like this

count=0;
for(i=0;i<=4;i++)
{
    for(j=0;j<=25;j++)
    {
       if(i==0)
       {
            if(j>5)
                a[i][j]=0;
            else    
                a[i][j]=1;
       }
       else if(i==1)
       {
            if(j<19)
                a[i][j]=0;
            else    
                a[i][j]=1;
       }
       else if(i==2)
       {
            if(count==0)
            {
                a[i][j]=0;
            }   
            else    
                a[i][j]=1;
        if(count==4) 
           count=0;
         else  
           count=count+1;
       }
       else if(i==3)
       {
            if(count==4)
            {
                a[i][j]=0;
            }   
            else    
                a[i][j]=1;
        if(count==4) 
           count=0;
         else  
           count=count+1;
       }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *