Thread: Generate Possible combination of 6-bit Binary number

  1. #46
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    ok i tried for 2 variables but i am getting another problem of printing alternate 0's and 1's :/

  2. #47
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Azeem View Post
    ok i tried for 2 variables but i am getting another problem of printing alternate 0's and 1's :/
    If you tell us what problems you have and if you even show your code someone here will probably help you.

    Bye, Andreas

  3. #48
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    Code:
    #include<stdio.h>
    int main(void)
    {
        int a,b,i;
        for(i=1; i<=4; i++)
        {
            for(a=0; a<=0 ; ++a)
            {
                printf("%d",a);
    
    
            }
    
    
            for(b=0; b<=0; b++)
            {
                printf("%d",b);
    
    
            }
            printf("\n");
    
    
        }
        return 0;
    }
    this is the code. it generates all zeros. But i want to generate truth table of 2 bits.

  4. #49
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    for(a=0; a<=0 ; ++a)
    {
         printf("%d",a);
    }
    How many times will the body be executed? Or asked another way: Is it possible that a gets another value than 0?

    Bye, Andreas

  5. #50
    Registered User
    Join Date
    Aug 2012
    Posts
    26
    I have modified my code by using compound termination condition
    Code:
    #include<stdio.h>
    int main(void)
    {
        int a,b,i;
        for(i=1; i<=4; i++)
        {
            for(a=0; a<=0 ; ++a)
            {
                printf("%d",a);
    
    
            }
    
    
            for(b=0; b<=1 && !(i%2==0); b++)
            {
                printf("%d",b);
    
    
            }
            printf("\n");
    
    
        }
        return 0;
    }
    I am getting 001 in first line. But i want 00 in first line, 01 in second, 10 in third, 11 in fourth.

  6. #51
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    "Nested" means one inside the other.

    This is NOT nested:
    Code:
    for (a=0; a<2; a++)
    {
    }
    
    for (b=0; b<2; b++)
    {
    }
    This IS nested:
    Code:
    for (a=0; a<2; a++)
    {
        for (b=0; b<2; b++)
        {
            // do printing here
        }
    }
    And get rid of the 'i' loop. It is not necessary.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #52
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I think you are on the wrong track so step back.

    Write one simple for-loop which will print the following two lines:
    Code:
    0 0
    0 1
    Use two variables (one for each column) but notice that only one changes its value. Additionally use only one printf()-call for each line.

    And forget the outermost for-loop because you won't need it.

    Bye, Andreas

  8. #53
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by AndiPersti View Post
    I think you are on the wrong track so step back.

    Write one simple for-loop which will print the following two lines:
    Code:
    0 0
    0 1
    Use two variables (one for each column) but notice that only one changes its value. Additionally use only one printf()-call for each line.

    And forget the outermost for-loop because you won't need it.

    Bye, Andreas
    That can't be the solution, because the OP stated:

    Yeah it is mandatory to use 6 nested loop
    Six integer variables - with the previously suggested six nested loops (required!), with one printf("%d%d...\n",i1,i2...); line of code, inside the innermost for loop.

    Obviously the variables will be assigned to 0 at first, and obviously they will stop at less than 2.

    Seems right to me.

  9. #54
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    That can't be the solution, because the OP stated:
    I just wanted to guide Azeem step by step to the correct solution but there are too many people already trying to help. Thus I will get out of here. You and oogabooga gave him almost the whole solution. It's up to Azeem now to carefully read both posts :-).

    Bye, Andreas

  10. #55
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would start with a program that will print just a single 0 on one line and then a single 1 on one line.
    Then once that works, do the same again, but the first time prepend a zero and the second time prepend a 1. This will give you 00, 01, 10, and 11 on each of four lines.
    Then once that works, do the same again, prepending a zero the first time and a 1 the second time. etc...

    The only other hint I will add for now is that it is quite rare for for loop conditions to use <= (less-than-or- equal). They usually use < (less-than).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #56
    Registered User
    Join Date
    Sep 2012
    Posts
    1
    azeem if you have solve this question.kindly guide me too

  12. #57
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by Wardah View Post
    azeem if you have solve this question.kindly guide me too
    Please post your own attempt in your own thread.
    Don't just bump an old thread with basically a request to "give me the code".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. generate number
    By cheeta in forum C Programming
    Replies: 9
    Last Post: 05-03-2010, 07:49 AM
  2. Replies: 8
    Last Post: 09-27-2008, 07:32 PM
  3. binary combination.
    By Moony in forum C Programming
    Replies: 1
    Last Post: 02-24-2008, 12:55 AM
  4. To generate bar code from a number
    By darkducke in forum C Programming
    Replies: 18
    Last Post: 01-16-2008, 06:33 AM
  5. generate a random number
    By waxydock in forum C++ Programming
    Replies: 5
    Last Post: 06-05-2005, 07:43 PM