## Array to Int

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### Array to Int

I'm trying to make a algorithm to turn a array of ints to a single int. Here's what I have so far:

Code: Select all
`for(int j = 0; j < sizeof(array); j++){     a_int += array[j] * pow(10.0,j);       }`

I'm just not getting the results that I want.
taymo2020

Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

What do you mean an array of ints. What ints are in that array. Aren't you affraid of overflow?

Depending on the declaration of the array, sizeof(array) might not give its number of elements.
Don't forget setting a_int to zero before enetering the loop, too.

Maybe you want:
Code: Select all
`int array[]={1,3,4,2,3};int n=sizeof(array);int a_int=0;for(int j=0; j<n; j++)    a_int+=array[j]*pow(10.0,n-j);`

devil_slayer

Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

What do you mean an array of ints

Uhhhh I mean:
Code: Select all
` int array[];`

Why should I be afraid of overflow?
taymo2020

Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

devil_slayer wrote:Maybe you want:
Code: Select all
`int array[]={1,3,4,2,3};int n=sizeof(array);int a_int=0;for(int j=0; j<n; j++)    a_int+=array[j]*pow(10.0,n-j);`

Close, but `n' will not be the size of the array; it will be the size of the array in bytes. Try this:
Code: Select all
`int array[]={1,3,4,2,3};int n=sizeof(array) / sizeof(int);int a_int=0;for(int j=0; j<n; j++)    a_int+=array[j]*pow(10.0,n-j);`
If it wasn't for C, we would be using BASI, PASAL and OBOL.

tomcant

Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

taymo2020 wrote:Why should I be afraid of overflow?

Well, what if your array of ints sums up to more then it fits in ine int. Remeber an int can have a max of 2^31 or in other words: 2 147 483 648.

If your array contains more than that the int will overflow.

Can you specify what you are using this for perhaps?

devil_slayer

Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

devil_slayer wrote:
taymo2020 wrote:Why should I be afraid of overflow?

Well, what if your array of ints sums up to more then it fits in ine int. Remeber an int can have a max of 2^31 or in other words: 2 147 483 648.

If your array contains more than that the int will overflow.

Can you specify what you are using this for perhaps?
No it wont. It should just truncate to 0.

> "2 147 483 648"
2, 147, 483, 647 acctually
--~~~~

Emery

Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

devil_slayer wrote:Can you specify what you are using this for perhaps?

I'm trying to find more Kaprecar's constants.
taymo2020

Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Ok I tryed this:
Code: Select all
`int arrayToInt(int* array){    int a_int = 0;    int n = sizeof(array)/sizeof(int);    for(int j = 0; j < n; j++)    {        a_int += array[j] * pow(10.0,j);           }    return (a_int);}`

And with a array that looks like this:
Code: Select all
` int array[] = {8,9,9,9,9,9,9,9,9,9,9};`

All that a_int ends up with is '8';
taymo2020

Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

You're passing a pointer so sizeof is giving you the size of the pointer.
--~~~~

Emery

Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

RITZ wrote:You're passing a pointer so sizeof is giving you the size of the pointer.

Ritz is right. If you send it as an arguemnt through the function it just won't work.

RITZ wrote:No it wont. It should just truncate to 0.

To me that's an overflow. In any case it's a fault in the program.

a_int += array[j] * pow(10.0,j);
It should be:
a_int += array[j] * pow(10.0,n-j-1);

The number you specified in the array is too big for a signed int. Try using unsigned long for a_int variable. It takes anything up to 18 446 744 073 709 551 615.

Code: Select all
`int c=4;int d=(int)pow(10,c-2);`

devil_slayer

Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Code: Select all
`#include <stdio.h>int f(int array[],int n){    int a_int=0,j;    int tens=1;    for(j=n-1; j>=0; j--)    {        a_int += array[j] * tens;        printf("D:%d\n",a_int);        tens*=10;    }}int main(){    int a[]={4,1,2,3};        printf("%d\n",f(a,4));    return 0;}`

devil_slayer

Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

devil_slayer wrote:Can someone please explain why:
Code: Select all
`int c=4;int d=(int)pow(10,c-2);`

[syntax="cpp"]#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int c=4;
int d=(int)pow(10,c-2);

cout<<d;
cin.get();
}[/syntax]

The value of `d' is 100 after the call to pow(...). Must be something your doing slightily different.
If it wasn't for C, we would be using BASI, PASAL and OBOL.

tomcant

Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Well I compiled it under bloodshed and got this wierd result... Quite strange...

devil_slayer

Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

devil_slayer wrote:
RITZ wrote:
RITZ wrote:No it wont. It should just truncate to 0.

To me that's an overflow. In any case it's a fault in the program.

By definition that is not an overflow. An overflow is when you write past the end of a buffer into uncharted territories and an overrun is when you read past the end of a buffer. This is the cause of a mathmatical instruction which cannot complete it's routine due to lack of memory. The opposite of a buffer overflow, it avoids a buffer overflow by defaulting to 0.

About that 99 instead of 100 things. I think I remember this coming up before on some other forum but I can't remember for the life of me where it came up or what the solution turned out to be.
--~~~~

Emery

Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

Overrun is when you write past the end of a buffer. Overflow is when the value of a calculation is greater than the datatype can store.

As for 99/100, pow() works on floating point numbers and so the value of pow(10.0, 2) might actually be 99.999999 or something. So, when you truncate it the value becomes 99.

If the array is reversed so that the most significant digit is in the first index (since right now it is in the last index) then the algorithm can be changed to not use pow():

Code: Select all
`int sum = 0;for (int i = 0; i < size; ++i) {   sum = sum * 10 + array[i];}`
Need information on a function I've posted? Chances are it's at the MSDN.
MXP

Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Next