C - Write algorithm

Discussion in 'C' started by wahsunny, Apr 9, 2007.

  1. wahsunny

    wahsunny New Member

    Joined:
    Apr 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Q. 1 Write algorithm for the following :

    a) to check whether an entered number is odd / even.

    b) to calculate sum of three numbers.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Would you post some code that you have attempted for the same.
     
  3. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    Code:
    int is_numeven(int number)
    {
    	if(!(number % 2)) /* Mod
    		return true;
    
    	return false;
    }
    
    int SumThreeNumbers(int num1, int num2, int num3)
    {
        return num1+num2+num3;
    }
    The first example divides the given parameter by the smallest even number that can be divided with and then it checks the remainder. (correct me if I'm wrong)

    The second example should speak for itself.
     
    Last edited by a moderator: Apr 10, 2007
  4. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    Ignore the comment '/* Mod' I didn't finish the comment.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I would suggest that producing someone's work for them is not in their best interests, when it comes to learning. It is, in fact, discouraged in the "Before you make a query" thread:
    It is better for the poster to post attempts and ask for help, or for the respondent to perhaps provide some pseudo-code or design guidelines.

    You may think otherwise, of course.
     
  6. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    You're right, but the topic starter can ask questions after reading the small example. I mean, I just wrote a few lines of code for him and explained it a little bit.

    Not sure what I'm doing wrong, however I agree to your point that it might be better if I'd have just given a few guidelines or references to documents.
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Also, please put code you write into code tags to preserve its indentation and formatting. I heartily recommend reading the "Before you make a query" thread, linked at the upper right of this page.

    There is nothing wrong with writing code or rewriting code -- provided that the learner is showing that he or she is putting forth effort to learn. Getting one's homework done entirely for free (or even for a price) is doing a disservice to the poster and to potential employers and coworkers who have to discover that the supposed skills were not actually learned.
     
  8. bughunter2

    bughunter2 New Member

    Joined:
    Feb 27, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Netherlands
    Home Page:
    http://reversemasters.nl/
    I didn't look into it that way, I guess you're right. Thank you.

    (btw on the code tags, I saw I should have added them but couldn't edit my post - where is the edit button?)
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I have done that for you and to edit your own posts you need to have minimum no of posts before you can do that.
     
  10. wahsunny

    wahsunny New Member

    Joined:
    Apr 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thank you all....
     
  11. sameer.chaudhari

    sameer.chaudhari New Member

    Joined:
    Aug 10, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    // if even then return 1 else return 0

    Code:
    int even_odd ( int num) 
    {
            if (num & 1) 
                return 0;
            return 1;
    }
     
    Last edited by a moderator: Aug 10, 2007
  12. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Code:
    bool even (int n)
    {
        return !(n & 1);
    }
     
  13. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    just changing the way
    we acn also get the info from last bit


    Code:
    int is_numeven(int number)
    {
    	if(!(number & 1)) 
    		return true;
    
    	return false;
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice