473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Array Overflow Prevention

/**
A palindrome is a number that is the same whether it is read from
left-to-right or right-to-left. For example, 121 and 34543 are both
palindromes. It turns out that nearly every integer can be transformed
into a palindrome by reversing its digits and adding it to the original
number. If that does not create a palindrome, add the reverse of the new
number to itself. A palindrome is created by repeating the process of
reversing the number and adding it to itself until the number is a
palindrome.Create the method palindrome, which takes a number N
that is to be transformed and returns a number that is the resultant
palindrome from this process.Of course if N is already a palindrome,
return it without changing it.Though it is theorized that all numbers
can be transformed to palindromes in this way,some numbers do not
converge in a reasonable amount of time.

**For instance, 196 has been carried out to 26,000 digits without
finding a palindrome.
So if the method finds that the resultant palindrome must be greater
than 1,000,000,000,return the special value -1 instead.
**

DEFINITION
Method: palindrome
Parameters: int
Returns: int
Method signature (be sure your method is public): int palindrome(int N);

NOTES
- Leading zeroes are never considered part of a number when it is
reversed. For instance, 12's reverse will always be 21 regardless of
whether it is represented as 12, 012, or 0012. Examples with leading
zeroes use the leading zeroes for clarity only.

EXAMPLES
Worked examples:
Example 1: N = 28
28 + 82 = 110
110 + 011 = 121, a palindrome. Return 121

Example 2: N = 51
51 + 15 = 66, a palindrome. Return 66

Further examples:
Example 3: N = 11, return 11
Example 4: N = 607, return 4444
Example 5: N = 196, return -1
**/

__________________________________________________ _________________________

My question is how do i check if the resultant palindrome is greater
than 1,000,000,000 ?My code uses an array to store the digits of the
sum,n i cannot have an array that darn long.

Please suggest some ideas.Also let me know if there are any other bugs.

PS: the code will crash with the inputs 196 and 197 as well.However
apart from these non-converging numbers i do obtain the correct result.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 10000000UL
#define ARR 50000UL
int check_if_palindrome(int *str,int length)
{
int *back = str + length - 1;
while(str <= back)
{
if(*str++ != *back--)
return 0;
}
return 1;
}

int palindrome(int num)
{
int digit=0,rev_num=0,sum=0;
int store_num=num,store_sum=0;
int i=0,n=1,k=0;
int hold[ARR];

while(num/10!=0)
{
digit=num%10;
hold[i++]=digit;
num=num/10;
}
hold[i++]=num;
hold[i]='\0';
if(check_if_palindrome(hold,i))
return store_num;
for(k=0;k<i;k++)
rev_num+=(hold[k]*((int)pow(10,i-k-1)));
sum=store_num+rev_num;
memset(hold,0,ARR);
i=0;store_sum=sum;

while(sum/10!=0)
{
digit=sum%10;
hold[i++]=digit;
sum=sum/10;
}
hold[i++]=sum;
hold[i]='\0';

if(check_if_palindrome(hold,i))
return store_sum;
else
palindrome(store_sum);
}

int main(void)
{
int num,test=0,ch;
printf("Enter the integer");
test=scanf_s("%d",&num);
while(test!=1)
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value");
test=scanf_s("%d",&num);
}
printf("The palindrome is%d",palindrome(num));
return 0;
}
Sep 27 '07 #1
4 2347
Tarique wrote:
/**
A palindrome is a number that is the same whether it is read from
left-to-right or right-to-left. For example, 121 and 34543 are both
palindromes. It turns out that nearly every integer can be transformed
into a palindrome by reversing its digits and adding it to the original
number. If that does not create a palindrome, add the reverse of the new
number to itself. A palindrome is created by repeating the process of
reversing the number and adding it to itself until the number is a
palindrome.Create the method palindrome, which takes a number N
that is to be transformed and returns a number that is the resultant
palindrome from this process.Of course if N is already a palindrome,
return it without changing it.Though it is theorized that all numbers
can be transformed to palindromes in this way,some numbers do not
converge in a reasonable amount of time.

**For instance, 196 has been carried out to 26,000 digits without
finding a palindrome.
196 is the smallest number that doesn't form a palindrome in this
manner. As the numbers get bigger, fewer form palindromes.
My question is how do i check if the resultant palindrome is greater
than 1,000,000,000 ?My code uses an array to store the digits of the
sum,n i cannot have an array that darn long.
Use a non-standard extended precision arithmetic library (e.g.
MIRACL) or write your own bignum code for addition; it's not too
difficult.
>
Please suggest some ideas.Also let me know if there are any other bugs.
Check out this link: http://www.jasondoucette.com/worldrecords.html

Also, see: Scientific American, April 1984, Computer Recreations

David
Sep 27 '07 #2
Tarique wrote:

....snip..
PS: the code will crash with the inputs 196 and 197 as well.However
apart from these non-converging numbers i do obtain the correct result.
.....snip...

Ive just modified the code a bit n it still crashes with the input 89 (A
Stack overflow occurs..i'm using visual studio 2005)although it takes
care of 196 n 197 for which it used to crash earlier.
I tried tracing it but could not figure out the reason.Can anyone help
me out?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define ARR 50000UL
int check_if_palindrome(int *str,int length)
{
int *back = str + length - 1;
while(str <= back)
{
if(*str++ != *back--)
return 0;
}
return 1;
}
int palindrome(int num)
{
int digit=0,rev_num=0,sum=0;
int store_num=num,store_sum=0;
int i=0,k=0;
int hold[ARR];

while(num/10!=0)
{
digit=num%10;
hold[i++]=digit;
num=num/10;
}
hold[i++]=num;
hold[i]='\0';

if(check_if_palindrome(hold,i))
return store_num;
for(k=0;k<i;k++)
rev_num+=(hold[k]*((int)pow(10,i-k-1)));
sum=store_num+rev_num;
if(sum>50000)return -1;
memset(hold,0,ARR);
i=0;store_sum=sum;

while(sum/10!=0)
{
digit=sum%10;
hold[i++]=digit;
sum=sum/10;
}
hold[i++]=sum;
hold[i]='\0';
if(check_if_palindrome(hold,i))
return store_sum;
else
palindrome(store_sum);
}

int main(void)
{
int num,test=0,ch;
printf("Enter the integer");
test=scanf_s("%d",&num);
while((test!=1)|| (num<=1)||(num>=10000))
{
while (('\n' != (ch = getchar())) && (EOF != ch));
puts("Enter an integer value within 1 and 10000 inclusive");
test=scanf_s("%d",&num);
}
if(palindrome(num)==-1)
printf("Sorry the number does not converge within the given range\n");
else
printf("The palindrome is%d\n",palindrome(num));
return 0;
}
Sep 28 '07 #3
Tarique <ta*****@invalid.comwrites:
Tarique wrote:

...snip..
>PS: the code will crash with the inputs 196 and 197 as well.However
apart from these non-converging numbers i do obtain the correct
result.
....snip...

Ive just modified the code a bit n it still crashes with the input 89
(A Stack overflow occurs..i'm using visual studio 2005)although it
takes care of 196 n 197 for which it used to crash earlier.
I tried tracing it but could not figure out the reason.Can anyone help
me out?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define ARR 50000UL
int check_if_palindrome(int *str,int length)
{
int *back = str + length - 1;
while(str <= back)
{
if(*str++ != *back--)
return 0;
}
return 1;
}
int palindrome(int num)
{
int digit=0,rev_num=0,sum=0;
int store_num=num,store_sum=0;
int i=0,k=0;
int hold[ARR];
50,000 ints in automatic storage (on your system "on the stack").
while(num/10!=0)
{
digit=num%10;
hold[i++]=digit;
num=num/10;
}
hold[i++]=num;
hold[i]='\0';

if(check_if_palindrome(hold,i))
return store_num;
for(k=0;k<i;k++)
rev_num+=(hold[k]*((int)pow(10,i-k-1)));
sum=store_num+rev_num;
This is not the main point, but if you calculate the result of the
reverse (and the sum) as an int, what is the point of using an array
for the digits? If you are prepared to limit the search to those that
produce intermediate results that fit into an integer variable, there
is not need for the array. If you need the array to represent very
large intermediate values (quite likely) then you can't use the above
to calculate the reverse and the sum.
if(sum>50000)return -1;
memset(hold,0,ARR);
i=0;store_sum=sum;

while(sum/10!=0)
{
digit=sum%10;
hold[i++]=digit;
sum=sum/10;
}
hold[i++]=sum;
hold[i]='\0';
if(check_if_palindrome(hold,i))
return store_sum;
else
palindrome(store_sum);
A recursive call that will require a fresh 50,000 int array. Some
numbers will require dozens of recursive calls. This is very likely
to be a problem.
}
PS. Use more white space and copy one of the common indentation
styles used for C.

--
Ben.
Sep 28 '07 #4
Tarique wrote:
Tarique wrote:

...snip..
>PS: the code will crash with the inputs 196 and 197 as well.However
apart from these non-converging numbers i do obtain the correct result.
....snip...

Ive just modified the code a bit n it still crashes with the input 89 (A
Stack overflow occurs..i'm using visual studio 2005)although it takes
care of 196 n 197 for which it used to crash earlier.
I tried tracing it but could not figure out the reason.Can anyone help
me out?
If you are restricting your program to numbers that fit into an
int, it doesn't make sense to use an array to hold the digits.
For numbers too large to fit into native data types you could use
arrays of chars that simulate numbers, each element holding one
digit, then write code that "adds" these arrays by adding
corresponding elements, implementing carrys if necessary. Also,
you should be able to set some limit for the number of
reversals/adds for the cases where the starting numbers do not
form palindromes.

I wrote a reverse/add palindrome program that uses this approach.
The size of the numbers is limited only by the amount of
available memory. It's too long to post here but if you would
like to post a working email address I'll send it to you.

David
Sep 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
3
by: dw | last post by:
Hi, all. Are there specific security precautions (input validations for example) that are documented somewhere to prevent hackers from compromising a credit card ASP application? In particular, can...
2
by: dati_remo | last post by:
Which is the best tool for buffer overflow to use during debug? What about Rational Purify? Best Regards
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
27
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
10
by: vashwath | last post by:
Hi all, Is there any free tool available for detecting array overflow? I found one which detects overflow of dynamic arrays. But I need a tool(or a special compiler) which detects static array...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.