Question

I have a project for school. I created a grid which looks like so:

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

using the following loop:

int img[10][10];
for(int i=0;i<10;i++) {
   for(int j=0;j<10;j++) {
        img[i][j] = 0; 
   }
}

I want to achieve this:

0000000000
0011100000
0011100000
0011100000
0000000000
0000000000
0000000000
0000000000
0000000000
0000000000

My attempted code (very wrong I already know)

int img[10][10];
int startRow, startCol, width, height, color;
startRow = 2;
startCol = 3;
width = 3;
height = 3;
color = 1

for(int i=0;i<10;i++) {
   for(int j=0;j<10;j++) {
       img[startRow][] = width + i; // so wrong
       img[startRow][startCol] = height + i; // what am i doing
       img[i + width][j + height] = color; // lets just crash the program 
   }
}

It doesn't work. I am lost on how to achieve this.

Any help on this would be great.

Was it helpful?

Solution

Surely you created the initial grid using this code:

int img[10][10];
for(int i=0;i<10;i++) {
   for(int j=0;j<10;j++) {
        img[i][j] = 0; 
   }
}

and not this:

img[i][j] = j; 

I would just add this second loop (run after the first) to fill in the ones:

for(int i=1;i<=3;i++) {
   for(int j=2;j<=4;j++) {
        img[i][j] = 1; 
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top