Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

for(i=-NUM; i<=NUM; i++) { for(j=-NUM; j<=NUM; j++) { if( abs(i)+abs(j)<=NUM) . please explain the condition by assigning values for i and j.

OpenStudy (e.mccormick):

So what did you have trouble with?

OpenStudy (anonymous):

just wanted the explanation for printing the stars by putting different values in the for loop.

OpenStudy (anonymous):

and i and j values for the if condition also.

OpenStudy (anonymous):

hmm.. i'll take a shot at moving the conversation forward. this looks like c++, yes? and some kind of iteration device? can you provide context? did you write this code? find it? what, more precisely, are you trying to learn?

OpenStudy (e.mccormick):

It could also be Java. With printing stars, I am guessing this is one of those early assignments that does a pattern in the output. The fragment is incomplete in any case, which makes it hard to say what it is doing and why. See, if I format it, I get this: ``` for(i=-NUM; i<=NUM; i++) { for(j=-NUM; j<=NUM; j++) { if(abs(i)+abs(j)<=NUM) ```

OpenStudy (kainui):

I played around with it in Java and fixed a few things to try to see if I could make it do anything interesting for the heck of it. ---- public class Star { public static void main(String[] args) { int NUM = 4; for (int i = -NUM; i <= NUM; i++) { for (int j = -NUM; j <= NUM; j++) { if (Math.abs(i) + Math.abs(j) <= NUM) { System.out.print("*"); } } System.out.println(""); } } } ---- That prints out something like: * ** *** **** *** ** * ---- And depending on what number you plug in for NUM, that will change it to create a larger or smaller thing like this. Kinda like pascal's triangle actually. Anyways, good luck figuring out what this question is really asking.

OpenStudy (e.mccormick):

Math.abs(j) vs abs(j) Ah HA! Yah,m it would probably be C or C++ rather than Java. But they are close cousins, so hopefully that will show them what they need to do.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!