Using C-Programming nested for() loops, write a program that displays all
possible combinations of a 6 bit binary number.

Sample Run: 000000, 000001, 000010, 000011, ...........111110, 111111

Can anybody please help me in solving this question?

Recommended Answers

All 20 Replies

Nobody's going to do your homework for you. Show some work and we'll help. Although it isn't a best practice, since it's a 6-bit number, you could do it with 6 for-loops, which I think is what their looking for.

You may want to check this out.

Although I agree that is the proper way to handle something like this, the question says to use nested for-loops.

Although I agree that is the proper way to handle something like this, the question says to use nested for-loops.

Given that this is the C forum and the link was for a C++ function, I'll go out on a limb and suggest that np complete was recommending that you look at the implementation of next_permutation(). Every one I've seen uses an iterative approach for maximum efficiency.

Member Avatar for I_m_rude

sir, Can you give how nested loop are helping in this ? can you give a algo? I am not asking for any code or part of any code here. I am just asking for algo as that i can code it in C. thanks to deceptikon in advance.

Actually, I was thinking that nested loops may make the program run slow. That's why I suggested that link.

Aren't we getting a bit off topic here? This looks like a homework question, and a fairly specific one at that. It looks like something from a programming intro course/tutorial, not an algorithms course. Some teachers/professors/markers may give you bonus marks; most, in my experience, take away marks for not doing what the question asked.

It doesn't seem like a question that is concerned with efficient algorithms, it seems like a question concerned with for loops...

This looks like a homework question, and a fairly specific one at that. It looks like something from a programming intro course/tutorial, not an algorithms course.

Probably very true, but we have a long-standing guideline that when you simply post requirements with no details, no questions, and no code you are seen as someone wanting us to do your homework for you. So you get what you get in the way of advice.

sir, Can you give how nested loop are helping in this ?

There's an implicit loop in next_permutation(), and you call it multiple times (ie. in a loop) to get all permutations. That's nested loops right there.

can you give a algo?

next_permutation() is a template function. Open up the <algorithm> header for your compiler and read how it works. In fact, doing this is better than just being given an algorithm because it forces you to read and understand code so that you can derive an algorithm for porting to other languages.

Member Avatar for I_m_rude

firstly, How to open up the algorithm header for your compiler ? means where it is ?

secondly, I am trying to ask you that if i don't want to use next_permutaion function, then how will i do using nested loops ?

thanks to you in advance.

firstly, How to open up the algorithm header for your compiler ? means where it is ?

check the library or lib folder of your compiler and look for the file
In my Windows using cygwin I found it at C:\cygwin\lib\gcc\i686-pc-mingw32\3.4.4\include\c++

in ubuntu probably at User/Include/gcc or g++ file

then how will i do using nested loops ?

already answered from previous posts you'd use 6 loops nested inside one another

in ubuntu probably at User/Include/gcc

On my Debian machine, the algorithm file can be found at one of the two locations:
* /usr/include/c++/4.4
* /usr/include/c++/4.4.5

It should be similar on Ubuntu. Remember that on Linux, file paths are case-sensitive!

  • /usr/include/c++/4.4
  • /usr/include/c++/4.4.5

right can't check right now, logged in on my Windows :)

Member Avatar for I_m_rude

hey, Ya i know it can be solved using 6 loops. but how to do that ? means i want the algo simply, not any code or something. like 6 loops are going, i,j,k,l,m,n,o then what is the logic to print that ? thanks.

-use an array of integers for the variable
-each loop has an initialize value of zero, and a condition that it only increments up to 1
-the statement inside each loop is composed of changing the leftmost or rightmost value of the array going right if following from the former or left if from the latter
-in the innermost loop print the array

here's an example using 2 digit binary

int a[2];
    for(int i = 0; i<2; i++){
        a[0] = i;
        for (int j = 0; j<2; j++ ){
            a[1] = j;
            /*prints the whole array from the innermost loop*/
            for(int k = 0; k<2; k++){
                printf("%d", a[k]);  
            }
            printf("\n");

        }
    }
commented: nice explaination :) +2

2 loops are enough for all permutation of 'n' bit binary number.

for(i=0;i<(1<<n);i++)
 {for(j=0;j<n;j++)
 {

 if(bit is set) print "1";
 else print "0";
 }
 }
Member Avatar for I_m_rude

@zeroklin that means for 6 numbers i need 7 loops as the inner most is for printing it ?

@zeroklin that means for 6 numbers i need 7 loops as the inner most is for printing it ?

yes with the 7th/innermost one for printing the array

#include<stdio.h>
main()
{
int a[64][6],i,j,k,d,c,n;
for(k=0;k<64;k++) //6 digit binary so 2^6 =64 is no: of combinations 
{
    for(j=0;j<6;j++)
    {
        a[k][j]=0;
    }
}

for(i=0;i<64;i++)
{
    n=i;

    for(j=5;j>=0;j--)
    {

        while(n!=0)
        {
            a[i][j]=n%2;
            j--;    

            n=n/2;      
        }
    }
}

for(k=0;k<64;k++)
{
    for(j=0;j<6;j++)
    {
        printf("%d",a[k][j]);
    }
    printf("\n");
}

}
Member Avatar for I_m_rude

what is that ? can you explain what you trying to do ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.