Thread: C prob plz help

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    C prob plz help

    C prob plz help-3-png



    Code:
    #include <stdio.h>
    #include <math.h>
    int reverse(int a)
    {
     int i=0,j, sum=0, scan;
     scan=a;
     while(a>0)
     {
      a=a/10;
      i++;
     }
     a=scan;
     for(j=0;j<i;j++)
     {
      sum+=(a%(int)pow(10,i-j))/(int)pow(10,i-j-1)*(int)pow(10,j);
     }
     return sum;
    }
    void main()
    {
     int b;
     scanf("%d", &b);
     printf("%d\n", reverse(b));
    }
    I got the results right, but my professor wants me to finish the program without using math.h header or any functions.

    Please help me!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hint: make use of modulo operation and integer division.
    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

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, make a function equivalent to "pow". Since you're only using integers, it shouldn't be that hard.

    EDIT: The use of no functions confuses me...
    Last edited by GReaper; 04-21-2012 at 10:13 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    If your professor is using void main(), you should show him this.

    > The use of no functions confuses me...
    I think he means no externally declared functions, like the <math.h> he mentioned.

    Also, @op, you could also use a temporary char array, use modulo splitting to fill it (similar to what you have), and then take them out in reverse order.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by memcpy
    Also, @op, you could also use a temporary char array, use modulo splitting to fill it (similar to what you have), and then take them out in reverse order.
    I think that that would be an unnecessary complication though. The use of multiplication is likely to be simpler.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another prob ppl
    By Sonia in forum C Programming
    Replies: 1
    Last Post: 10-13-2010, 05:14 AM
  2. not sure what the prob is
    By Rykaerdoe in forum C Programming
    Replies: 2
    Last Post: 12-01-2004, 02:09 PM
  3. map[y][x+1] prob
    By Maurad3r in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2004, 03:01 AM
  4. IDE prob
    By MicroFiend in forum C++ Programming
    Replies: 1
    Last Post: 03-04-2004, 04:28 PM
  5. a prob?
    By o0o in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2004, 11:27 PM