1. ## Need help simplifying

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
int n,i,temp1,temp2,temp3,temp4,temp5,remainder,j,k,p,count,count2,a;
double bits;
bits = pow(2,n);

printf("Please enter an integer between 1 and 33:");
scanf("%d",&n);

bits = pow(2,n);

printf("\nNumber n of bits in the integers: %d\n",n);
printf("Number of integers with n=4 bits: %d\n",bits);

printf("\n\nAll integers with 0 bits equal to 1 listed in ascending order:\n");

i = 1;
temp1 = n;
temp2 = n;

printf("0\t");

while (temp1 != 0)
{
printf("0");
temp1--;
}

printf("\n\n");

int l;

for(l=1;l<=n;l++)
{
printf("All integers with %d bits equal to 1 listed in ascending order:\n",l);
i=1;

while(i != bits)
{

int temp3 = i;
int temp4 = i;
int count = 0;

while(temp3 != 0)
{
temp3 = temp3 / 2;
count++;

}

int k = count - 1;

int sum = 0;
int count3 = 0;

while (count != 0)
{
int power1 = (int) pow(2,k);
double power2 = pow(10, k);

int a = temp4 / power1;

temp4 = temp4 % power1;
sum = sum + a * power2;

if(a == 1)
{
count3++;
}

k--;
count --;
}
int need1 = sum;

if (count3 == l)
{
int f;

printf("%d\t",i);

count2=0;

while (need1 != 0)
{
need1 = need1 / 10;
count2++;

}
f = n - count2;

while(f != 0)
{
printf("0");
f--;

}
printf("%d\n",sum);

}

i++;
}
printf("\n");
}
system("pause");
}```

2. Code:
```#include <stdio.h>

int main()
{
puts("Hello World!");
return 0;
}```

3. the first call to
Code:
`bits = pow(2,n);`
n here contains garbage value.

4. As long as n is smaller than 32, 2 ^ n can be calculated by 1 << n.

Your code is fairly complex for what it does (and I don't quite understand what the specification was for the project).

--
Mats

5. At least you realize this is way too complex for what you're trying to accomplish. When this happens, you should take a step back and ask yourself what you're really trying to do.

Here's pseudo-code which generates the same output as your program.

Code:
```Read in bit_length
for each bit_count from 0 to bit_length-1
for each number from 0 to 2^bit_length-1
if (count_one_bits(number) == bit_count)
print number in decimal and binary```
To count the number of bits set to one
Code:
```count = 0
while number is non-zero
if the least significant bit of number is 1, increment count
shift number one bit to the right```
To print the number in binary
Code:
```for each bit from bit_length - 1 to 0
if number right-shifted by bit length has its least significant bit set
print 1
else
print 0```
Implementing this pseudo-code is pretty much a line to line translation to C, which means that anything in your current code that doesn't line up with something in there is probably extra stuff you can eliminate.

You can use either mod (%) or bitwise and (&) to check if a particular bit is set.
The right-shift operator is >> in C.

I'd use unsigned integers for anything you're doing bitwise operations on. You'll thank me later.

Don't use a special case for bit_count == 0 - that'll save some code.

You don't need any floating point code in this at all.

6. Originally Posted by Snafuist
Code:
```#include <stdio.h>

int main()
{
puts("Hello World!");
return 0;
}```
If by that you mean that he would be best to delete the existing code and start over, then I agree.

7. This is what I wrote to make it simple but it doesnt print out the way I would like it to :S pff
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
unsigned int number,n,count,j,k,l,m,x;
int i;

printf("Please enter an integer between 1 and 33: ");
scanf("%d",&n);
number=pow(2,n);
printf("\nNumber n of bits in the integers: %d\n",n);
printf("Number of integers with n=4 bits: %d\n\n",number);
count=0;

for(k=0;k<number;k++)
{

j=k;
l=k;
for(i=0;i<number;i++)
{

if((j%2)==1)
count++;
j=j>>1;

}
printf("%d\t",k);
for(i=n-1;i>=0;i--)
{

x = 1 << i;
m = k & x;
if( m == 0)
printf("0");
else
printf("1");

}
printf(" Bit count:--> %d\n",count);
count=0;

}
system("pause");
}```

8. What does it print out, and how would you like it to print out? Figuring out what that difference is will go a long way in getting your problem solved.

Quzah.

9. Go back and look at the pseudo-code I posted again. There are two main loops (plus two smaller ones to count bits and print them out). You only have one. There's a comparison there between the number of bits in the current number and another variable. That's also missing in your code.

That's assuming you want to do what your original program did. As Quzah points out, we're not mind readers.

I would also double-check this line:

for(i=0;i<number;i++)

The stuff inside that loop is working on j, which is a copy of k. Why are you going through the loop <number> times? That's not the number of bits in the value you're counting bits in. It works, but it's doing way too many loops where j equals zero because you've shifted all of the bits out of it already (hint).