Project Euler Palindrome #4 with C -




i found few posts regarding problem using c. of elements in code work on own iteration @ beginning causing problems reason. first, i'm getting "exited non-zero status" error message. when run program smaller range , b, don't message. i'm guessing there's problem rev_array , for_array variables created. i'm sure i'm doing dumb right here apologize in advance that.

but when use smaller range , b (like 10 25), program still showing two-digit numbers (even 11, 22, 33, 44) not same forward , backward. used printf check this.

i made similar program used fixed values , b instead of iterating on range of values , worked fine. couldn't figure out why 1 isn't working.

#include <stdio.h> #include <stdlib.h> #include <math.h>  int max; int a; int b; int prod; int m = 0; int rev_array[10000]; int for_array[10000]; int c; int d; int same = 0;  int main(void) {   // iterate on 3 digit numbers in lines 19-21   for(a = 10; <= 25; a++)   {     for(b = 10; b <= 25; b++)     {       max = 0;       prod = * b;       /* zach scrivena following formula converting integer array of integers posted on stackoverflow on february 5, 2009 under subject "convert integer number array"       */       int n = prod;       while(n != 0)       {         rev_array[m] = n % 10;         n /= 10;         m++;       }       /* jordan lewis following int length formula posted stackoverflow on june 18, 2010 in response "finding length of integer in c"       */       int length = floor(log10(abs(prod))) + 1;       // create forward array of ints in prod        for(c = length - 1, d = 0; c >= 0; c--, d++)       {         for_array[d] = rev_array[c];       }       // compare forward , reverse arrays see if match       for(int e = 0; e < length; e++)       {         if(for_array[e] != rev_array[e])         {           // if don't match set same equal 1 following step           same = 1;         }       }       /* if prod greater max , forward , reverse arrays identical, replace max prod       */       if(prod > max && same == 0)       {         max = prod;       }       // reset same , repeat process       same = 0;     }   }   // print final, greatest number fits preceding criteria   printf("new max: %i \n", max);   return 0; } 

answers provided in comments:

you need reset m 0 each time. – johnny mopp

you don't need calculate length, since m should contain length after while loop (but need reset 0 @ top). , these variables should local, , of them (like m, n, prod) should defined inside inner loop, limited scope. max 1 needs preserved between iterations. – groo





wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

python - Minimize function with Scipy minimize -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -