Drawing with if sentences

Im trying to figure out the exercise below:

This question uses a 20x20 array of binary pixels as a canvas. Initially the canvas is white. We intend to draw different shapes on this canvas using black.

b) Which pixels will be affected if the following code snippet is run? The origin of the image is in the upper left corner. Make your answer as an illustration of the image.

1
2
3
4
5
6
7
8
9
10
11
12
13
 unsigned int x = 0;
 while (x < 400)
 { 
if(x/20 == x%20) 
   img[x%20][x/20] = false;
if(x/20 + x%20 > 35)
   img[x%20][x/20] = false;
if(x/20 - x%20 > 15) 
   img[x%20][x/20] = false;

   x++;
 }


I would like to be systematic, but my math/programming skills are giving me a hard time.

First of all x/20 when x is an integer means that when x has a value between 1-39, x is 1?
When x has the value 40 then x is 2?
X-value from 41-59 then x is 2 and so forth?

x%20 is just the number itself up 19 then at 20 its 0 and from 21-39 it is 1-19?

Are these first assumptions correct?
Yes, I think you have the operations understood correctly.
Ok thx, so the values where x/20 == x%20 are:

1, 21,41,61,81,101,121,141,161,181,201,221,241,261,281,301,321,341,361,381

I cant really get my head around the next sentence: if(x/20 + x%20 > 35)

What is the system in that one?
Not quite:
Lets look at some cases:
1
2
3
x = 1;
x/20 = 0;
x%20 = 1; 

This statement is not true when x == 1;

x/20 is asking us "How many times can x be divided by 20?". Just use regular division and drop the remainder. In this case 1/20 = 0 and 41/20 = 2.

Now x%20 gives us that remainder. So 1 % 20 = 1 and 41 % 20 = 1.

The values that satisfy this if condition are:
0, 21, 42, 63, 84, 105, 126, 147, 168, etc...

In your image, this would look like a straight line with a slope of 1 (points 1,1 2,2 3,3 4,4 5,5 etc.


Since this is probably a programming assignment, you could also try compiling this and checking the outputs by using a space for img[x][y] == true and an 'x' for img[x][y]==false;
Solutions:
0, 21, 42, 63, 84, 105, 126, 147, 168, 189, 210, 231, 252, 273, 294, 315, 320, 336, 340, 341, 357, 359, 360, 361, 362, 378, 378, 379, 380, 381, 382, 383, 397, 398, 399, 399.

1
2
3
4
5
6
7
8
9
10
while (x < 400)
    { 
        if(x/20 == x%20) 
            cout << x << ", ";
        if(x/20 + x%20 > 35)
            cout << x << ", ";
        if(x/20 - x%20 > 15)
            cout << x << ", ";
        x++;
    }

:)

in real life, if you divide any number less than 20 by 20, you will get 0.xxxx. Since you are using an integer, you will get a whole number which will NOT be rounded up; c++ just drops off the digits.
in this case
1
2
int x = 1
cout << x/20;

0


the remainder (%) operator example:
1
2
int x = 43
cout << x/20;

3

20 can fit in 43 two times to give you 40
and what remains is 43-40 which in this case is 3
@ Stewbond

Im not really sure how to use a 2D array, when printing the pattern... I have made the code below, but it doesnt get to look right:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main ()
{
  
unsigned int x = 0;
    
    while (x < 400)
         { 
            if(x/20 == x%20)    
                   cout << " ";
             else
                 cout << "X";
            if(x/20 + x%20 > 35)
                   cout << " ";
            else
                cout << "X";
            if(x/20 - x%20 > 15)  
                   cout << " ";
            else
                cout << "X";
             
             x++;
            
         }
    
      return 0;
}


How would I do it right with the use of a 2D array as you suggest?
Topic archived. No new replies allowed.