0

I'm making a function that reverses numbers less than 100000000. For example, if the input is 1234 then it should return 4321. But I am getting time limit exceeded TLE, I have made break points of my for loops but don't know why. Can you tell me what's wrong with this code?

int reverse(int n){
    int i, j=1, d[100000000]={0}, rev=0;  

    for(i=10;  ;i*10){
        if(n%i==n){
            d[j]=(n%i)/(i/10);
            break;
        }
        d[j++]=(n%i)/(i/10);
    }

    for(j=1; ;j++){
        rev+=(d[j]*(i/10));
        i/=10;
        if(i==10)
            break;
    }
 return rev;
}

int main(){
    printf("%d",reverse(321));
    return 0;
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Sangmin J
  • 13
  • 3
  • 1
    You're making a 400MB array. Few systems would be able to run your program if you place a 400MB array on the stack. – Some programmer dude Feb 19 '17 at 12:34
  • 2
    Also, don't try to learn how to program using competition sites. They are really bad for that purpose. Instead [get a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Feb 19 '17 at 12:35
  • @alk for getting digits of the number – Sangmin J Feb 19 '17 at 12:39
  • @Someprogrammerdude I changed 100000000 to 10000 or 1000 but doesn't work.. It doesn't stop but nothing shows on the console – Sangmin J Feb 19 '17 at 12:40
  • @Someprogrammerdude thanks for the advice but I gotta take this algorithm problem solving test in 2 monthes, so I gotta start practicing..such lack of time .. – Sangmin J Feb 19 '17 at 12:42
  • @SangminJ If you need to learn algorithm problem solving, learn the algorithms first. You can't write the code unless you thoroughly understand what it is supposed to do and how it will do it. – nicomp Feb 19 '17 at 13:25
  • @Someprogrammerdude finally figured it out. you're right. It does work after fixing the inner codes and when I changed the array to [100000],, if there are more than 5 zeros it doesnt work. which is 400MB. Where do you get to know the information that if you put more than a 400MB it doesn't work? – Sangmin J Feb 19 '17 at 13:51
  • @nicomp thanks for the advice.. but as you can see this is just a fuction problem, no special algorithms are used. I admit I need to concrete the basics first. Like I made for loop condition i*10; LOL but as I said I'm running out of time so I'm kinda rushing : – Sangmin J Feb 19 '17 at 13:54
  • While the location of local variables is an implementation-detail, they are usually stored on the stack. The stack is a limited resource. On Windows the default stack for a process is a single MB. On Linux the default size is 8MB. Trying to use more will lead to *stack overflow*. – Some programmer dude Feb 19 '17 at 13:56
  • @SangminJ every program ever written has an algorithm in it. Well, some JavaScript programs may not, but ... – nicomp Feb 19 '17 at 16:22

1 Answers1

1

Use basic knowledge of how numbers work: place value. If you use the % operator to extract the least significant digit from the number, you can build up the reversed number. Try this:

int reverse(int n)
{
        int result = 0;

        while ( n > 0 )
        {
                result = result * 10 + (n % 10);
                n /= 10;
        }
        return result;
}
Nick
  • 4,820
  • 18
  • 31
  • 47