Thread: Rewrite Function Using Pointers

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    Rewrite Function Using Pointers

    Hi all, I'm new here and I'd first like to say hi! Anyway, I've been programming in Java for a while, and one of my courses recently required that I pick up C. I've tried searching and reading up on pointers already, but would still require help with the following:

    Actual Code:
    Code:
    int mystery1(int n) {
    	int bit;
    	if (n == 0)
    		return 0;
    	else {
    		if (n & 1)
    			bit = 1;
    		else
    			bit = 0;
    		return bit + mystery1(n/2)*2;
    
    	}
    
    }
    To be rewritten using the following function prototype:
    Code:
    void mystery1(int *n, B C)
    Where B and C are just placeholders. I'd just need some help in pointing me in the right direction, such as what B and C are? Thanks!

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Do your own homework.

    B and C are nothing, unless teh instructor gave you instructions on what they are and you didnt provide that to us.

    That function also is subject to an infinite loop for any even value over 1
    Last edited by abachler; 08-28-2008 at 11:59 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why would it be an infinite loop?

    And you would need at least a type in the prototype.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by tabstop View Post
    Why would it be an infinite loop?

    And you would need at least a type in the prototype.
    oh my bad, i was readign it as returning mystery1((n/2)*2), i see now its not an infinite loop, it just coutns the number of bits that are 1's

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    abachler: I've done my homework and that's why I'm asking here. I'm not even asking for the code, I just want to know what B and C might be.

    And no, it will not generate an infinite loop. What actually happens is that for a positive number x, the output will also be x. Doing a printf() of bit will show the number in it's binary form. When the input is a negative number x, the output will be the positive form of x.

    Also, what I've written is what I'm given. I too, would like to at least know if B C is "int bit" or something, but that was not given =(.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    there is no way of knowign what it could be, its undefined, and will generate a ciompile error. There is no logical way of determining what it migth be either, it coudl be anything. Ask teh instructor what he intends it to be.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I too, would like to at least know if B C is "int bit" or something, but that was not given =(.
    What is this, an algebra problem? "Fill in the blanks" with C code? Clearly the function should be rewritten like this if your teacher really didn't elaborate at all on what needs to be done:
    Code:
    #include <stdio.h>
    
    int mystery1 ( int *n, const char *msg )
    {
      if ( msg != NULL )
        puts ( msg );
    
      return *n;
    }
    
    int main ( void )
    {
      int x = 12345;
    
      printf ( "%d\n", mystery1 ( &x, "Mr. Teacher Guy, you're a doofus." ) );
    
      return 0;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    Thanks for the replies so far. Anyway, the original function returns an int, while the prototype is a void function. Apparent B is the type and C is the identifier. Since the function has no return type now, I'm guessing that *n is used instead to some sort of give a return value. I still can't figure out what B C should be for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM