Thread: Help with code, trying to learn c language

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    3

    Help with code, trying to learn c language

    There is a countBits function which i do not know how to complete.
    Can someone show me with an explanation/comments






    Code:
       #include <stdio.h>
        #define TEST(v,x) (countBits(v) == x) ? puts("Passed") : 
        puts("Failed")
    
    
    
    
        //Function prototype
    
    
         //unsigned char countBits(const unsigned char u);
    
    
    
    
       
    
    
        //This function passes different values to the function countBits, and 
        checks the returned value is correct
    
    
        // int main(int argc, const char * argv[]) {
         puts("Begin tests");
         TEST(0, 0);             //Minimum edge case
       
         TEST(1u, 1);            //smallest for 1 result
         TEST(1u<<1, 1);
         TEST(1u<<7, 1);         //largest for 1 result
       
         TEST(0x03, 2);          //Smallest for 2 result
         TEST(0xC0, 2);          //Largest for 2 result
       
         TEST(0xFF, 8);          //Maximum edge case
       
         return 0;
          }
    
    
        //FUNCTION UNDER TEST
    
    
        // Function countBits
        //
        // Return value: returns the number of '1's in the binary 
        representation of u
        //
        // Examples.
        // If u = 12, this is represented as binary 00001100
        // There are two '1's in this number, so the function will return 2
        //
        // If u = 13, this is represented as binary 00001101
        // There are three '1's in this number, so the function will return 3
        //
        unsigned char countBits(const unsigned char u)
        {
         unsigned char numberOfBits = 0;
         //  BEGIN  CODE HERE 
    
    
    
    
          
      
         return numberOfBits;
          }




    Also, could someone show me a simple example code to demonstrate the 'switch-case' and 'if-else if else' and statements with comments/ explanation?












    And this code-






    Code:
     unsigned short x1;
    
    
        /*
         * The code below is supposed to request the user to input 
        a numeric value
         * If the value is not between 20 and 50, it should ask 
        again
         * If 20 <= x <= 50, then the code should proceed.
         *
         * where are the bugs?
         * only use while loop
         */
        
    
    
        do {
            puts("Enter a whole number x, such that 20 <= x <= 
        50");
            scanf("%hu", x1);
        } while ( (x1>=20) && (x1 <50) );
    Last edited by HappyC4mper; 10-19-2017 at 04:56 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You haven't done anything though. The code you posted and the questions you ask seem like homework, try to solve them instead of asking for freebies.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    i've only had 2 days crash course of c so far and these are just practice questions. I just want someone to explain how I could do it. Since I'll be using what i've learnt from these practice questions to program a robot next week :/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well do you know what << does?
    What about other bitwise operators like & | !

    What you need is a loop which rotates a 1-bit through every position so you can test your value.

    Your over ambitious times are another issue...
    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. Which language to learn next ?
    By manasij7479 in forum General Discussions
    Replies: 14
    Last Post: 04-21-2012, 05:01 AM
  2. What language to learn
    By ajrillik in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 08-15-2005, 06:36 PM
  3. Should I learn a FULL language first?
    By Raeliean in forum Game Programming
    Replies: 8
    Last Post: 07-16-2005, 06:59 PM
  4. What Language to learn???
    By Serpentineocean in forum Game Programming
    Replies: 7
    Last Post: 06-10-2004, 09:07 PM
  5. which language should I learn
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-07-2002, 06:26 PM

Tags for this Thread