what is the output of the following c programs?

edited March 2014 in General Discussion
main()
{
inti,j;
for(i=1;i<=2;i++)
  {
   for(j=1;j<=2;j++)
    {
     if (i==j)
     continue;
     printf("\n%d  %d",i,j);
        }
       }
 )

how can we know this program answer??????

Answers

  • edited March 2014
    0 1
    1 0 
    

    sorry my mistake :P I didn't see the loop start value

    1 2
    2 1
    
  • What happened when you compiled and ran it?

  • Answer ✓

    The code can be easily translated to Processing code and it's output will be:

    1 2
    2 1
    

    And the Processing code:

    void setup() {
        for(int i = 1; i <= 2; i++)
            for(int j = 1; j <= 2; j++) {
                if(i == j)
                    continue;
                println(i, j);
            }
    }
    
  • Nitpick: I would recommend (and the standard Java conventions do as well) always using curly brackets with if statements and for loops, even when they only contain a single line of code:

    void setup() {
        for(int i = 1; i <= 2; i++){
            for(int j = 1; j <= 2; j++) {
                if(i == j){
                    continue;
                }
                println(i, j);
            } //end j loop
        } //end i loop
    } //end setup()
    
  • edited March 2014

    @KevinWorkman: Following the Java code conventions you should also always define all variables at the beginning of your function.

    My personal opinion: If you are (and will be) the only one working with your code, or you are coding just for fun - do what feels right. Develop your own programming style and use what the language gives you.

  • edited March 2014

    I assume you're talking about section 6.3 Placement, which does indeed recommend declaring variables at the beginning of blocks. However, it also says that for loops are the exception.

    The reason for always using curly brackets is not just a style choice, but is recommended because it avoids problems in the future. For example, if I wanted to add a print statement after the continue in your code, I might just put it right under the continue. However, that would cause errors in my code, as I forgot to add the curly brackets! This is a very common problem, which is why it's recommended to just add them right from the beginning.

    Anyway, there's the section of the code conventions that mentions defining variables at the beginning of blocks, except for for loops:

    6.3 Placement Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.

    void myMethod() { int int1 = 0; // beginning of method block

    if (condition) {
        int int2 = 0;     // beginning of "if" block
        ...
    }
    

    }

    The one exception to the rule is indexes of for loops, which in Java can be declared in the for statement:

    for (int i = 0; i < maxLoops; i++) { ... }

    Avoid local declarations that hide declarations at higher levels. For example, do not declare the same variable name in an inner block:

    int count; ... myMethod() { if (condition) { int count = 0; // AVOID! ... } ... }

  • edited March 2014

    @KevinWorkman: Sorry, I have totally overread this section. Anyhow, you could simply add curly brackets afterwards using a code formatter.

  • Agreed. I just wanted to point out the little detail, as it's a problem I've seen on the forums before!

  • edited March 2014

    If your programming style causes your programm to crash or makes it harder to maintain, I agree, maybe code conventions give you some direction to avoid the former or improve the latter. But actually I never had any problems to maintain my code. I have even some code snippets 4 or 5 years older than the afore mentioned convention docs from 1999 that haven't lost there readability at all.

    I think the most important part about programming is: Never forget to comment your code. ;)

    Edit: Oh, and never use gotos! (Sorry @GoToLoop) :D

  • edited March 2014

    ... always using curly brackets with if statements and for loops,...

    Nah! Too verbose! Python and CoffeeScript style rules! :ar!

  • Python, where indentation matters? Almost none of the code people post here would work! Ack!

  • edited March 2014

    please can you teach me? how can the code translated?? i am really new i dont know how to translate programs like this :/

  • edited March 2014

    i have not got it how can be the answer 1 2 2 1 come? could you pleaseeee tell me? @Poersch

  • shania, read To newcomers in this forum: read attentively these instructions to see how to format your code (and to choose a category!).

    I removed the off-topic questions you asked in a thread you hijacked.

  • i dont got it please tell me the basics how can i translate the code? @PhiLho

  • What exactly are you confused about?

    The code has already been "translated". Why are you asking about C code in a Processing forum? If you need to know what the C code does, why don't you just run it?

  • because i have a exam and the question like this will be include in paper ?? and i dont know how to read this code so can you please teach me how to read this codes and know whats the output? @KevinWorkman

  • Perhaps your main doubt is about printf() function?
    https://en.wikipedia.org/wiki/Printf()

  • It's easy to translate C into processing ---

    What the code does: a for loop executes all between the { } as long as the condition is true (so i is first 1 then 2 and then stops)

    since within these { } there is another for loop this gets executed twice

    But all in this for loop { } gets executed 4 times (2 times 2 )

    The if ask if it is true and if so it prints it

  • So when they are the same, continues applies, so next for loop is started

    Only when they are not the same, println is applied and shows both numbers

Sign In or Register to comment.