Thread: Need help simplifying

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    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. #2
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Code:
    #include <stdio.h>
    
    int main()
    {
            puts("Hello World!");
            return 0;
    }
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    the first call to
    Code:
    bits = pow(2,n);
    n here contains garbage value.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Snafuist View Post
    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.
    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"

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simplifying arithmetic using visitor pattern
    By MyglyMP2 in forum C++ Programming
    Replies: 0
    Last Post: 10-07-2007, 01:29 AM
  2. Simplifying Code
    By Myst1caL in forum C Programming
    Replies: 6
    Last Post: 09-08-2007, 03:57 PM
  3. Simplifying code
    By Taka in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 02:12 AM
  4. Simplifying fractions in c++
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2002, 06:58 PM