A question about an exercise in <Algorithms for Visual Design>

Currently I am reading the book by Kostas Terzidis.

I meet a question doing the exercise1-9
9. Write the shortest possible procedural code that can generate the following number pattern using only standard arithmetic operations(+, -, *, 0 and %):

I post the answer below:

for(int i=0; i<20;i++){
int x=(i%4)/3*(-1)+1;

But there is a point that I still don’t understand,
(i%4) generate 0123012301230123
In the cases of 0 and 3, it leads to 1 and 0.
However, in the cases of 1 and 2,
I think int(1/3) and int(2/3) count 0?

1 Like

Remember that int values cannot hold decimal numbers.

The numbers 1 and 3 are both int values by default, so the result of 1 / 3 is also an int. Since int values cannot hold decimal numbers, the value gets truncated to be just 0.

Try running some example code to test it out:

println( 1 / 2);
println ( 1.0 / 2.0);

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.



Thank you Kevin, your example is really helpful!
I also think int(1/3) and int(2/3) count 0.
But the answer is given by the book, and I tested in the processing, it does lead to 1110111011101110.
I don’t understand how it goes if int(1/3) and int(2/3) count 0.

Are you saying the code works but you don’t know why, or that the code does something different from what the book says?

I don’t know exactly what the book says, but could it be that the book was speaking generally and not in a particular language? Maybe you need to adapt whatever the book says so it makes sense in the particular language you’re using?

If you’re still having trouble, I’d suggest breaking this line down into multiple steps:

int x=(i%4)/3*(-1)+1;

Something like this:

int stepOne = i % 4;
int stepTwo = stepOne / 3;
int stepThree = stepTwo * -1;
int stepFour = stepThree + 1;

Then you can isolate just the step that’s confusing you, and it’ll be easier to help you.

1 Like

Thank you for your detailed answer!!

Yes, the code works but I don’t know why.

Breaking down into multiple steps does help me to understand.
But it will turn stepTwo and stepThree into int at once.

I try to specify my question:
In the case of i=1,

int x=(i%4)/3*(-1)+1;

The final answer will be different under these two situations.
If it turns the calculation into int at every step, then it leads to one.
If it turns the calculation into int only at the final step, then it leads to zero.

I tried it in the computer, it seems that the first situation is right, but why the computer interrupt the calculation to turn it into int at every step? I find it confusing.

Can you please narrow it down to a single step that you’re confused about? I don’t know which part of the equation is confusing you.

The computer isn’t interrupting anything to convert a value to int. The values are already ints.

1/3 is an int that evaulates to 0. (1/3)*10 is an int that evaluates to 0. Any literal number without a decimal point is by default an int, and any calculation involving two int values is also an int. Any decimal places in an int are automatically truncated. There is no interruption happening.

1 Like

Thank you.

I think you have unravelled the mystery surrounding the int for me.
The example " (1/3)*10 is an int that evaluates to 0."
And "Any decimal places in an int are automatically truncated. " make me clear.

The truncation will happen automatically at every step.
I thought it only happens at last.

Thank you so much:)