Thread: Intro to C programming help

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    1

    Question Intro to C programming help

    I am trying to figure out how to convert this recursive Scheme definition into a non-recursive loop function in C. I've just started learning C after Scheme and loops are still confusing to me.

    This is the recursive version in scheme:
    Code:
    #lang racket
    (define (powmod2 x e m)
      (define xsqrm (remainder (* x x) m))
      (cond 
        [(= e 0) 1]
        [(even? e) (powmod2 xsqrm (/ e 2) m)]
        [(odd? e) (remainder (* x (powmod2 xsqrm (/ (sub1 e) 2) m)) m)]))
    This is my non-working code in C:
    Code:
    int powmod2(int x, int e, int m) {
       int ans = 1;
       while (e != 0) {
          x=x*x%m;
          if(e%2 == 0) {
             e=e/2;
          }
          else {
             e=(e-1)/2;
             ans=(ans*x)%m;
          }
        }
        return ans;
    }
    Any guidance would really be appreciated!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try getting a recursive version working first.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong:
    Code:
    x=x*x%m;
    I am not much of a schemer, but I believe that your Scheme code does not do that computation at that point. Rather, it just defines a function to do that, and then calls the function with arguments depending on the subsequent condition. As such, an appropriate translation is to define a xsqrm function in C, and call that from your powmod2 function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Now that I think about it, forget about a recursive version. In fact, forget about the scheme (lisp) program completely and think about how you would do it iteratively. You want to multiply 1 by x e times, modulus m.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intro to c programming assignment help
    By aargoo in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2010, 05:47 PM
  2. Intro to embedded programming
    By ohanna in forum C Programming
    Replies: 6
    Last Post: 03-13-2008, 03:16 PM
  3. Intro + help
    By D2k204 in forum C++ Programming
    Replies: 4
    Last Post: 07-30-2007, 02:17 PM
  4. Intro to C++, NEED HELP!!!
    By romeoz in forum C++ Programming
    Replies: 14
    Last Post: 06-11-2003, 08:19 PM
  5. Intro To Programming...need Advice Plz
    By dox82 in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2003, 12:25 PM