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

Postby taymo2020 » Sat Jul 30, 2005 6:39 pm

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..

Postby devil_slayer » Sat Jul 30, 2005 6:52 pm

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);
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby taymo2020 » Sat Jul 30, 2005 7:19 pm

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..

Postby tomcant » Sat Jul 30, 2005 7:31 pm

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.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby devil_slayer » Sat Jul 30, 2005 8:06 pm

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?
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby Emery » Sat Jul 30, 2005 8:24 pm

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
--~~~~
User avatar
Emery
 
Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

Postby taymo2020 » Sat Jul 30, 2005 8:43 pm

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

I'm trying to find more Kaprecar's constants.
http://math.about.com/od/recreationalma ... prekar.htm
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Postby taymo2020 » Sat Jul 30, 2005 8:56 pm

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..

Postby Emery » Sat Jul 30, 2005 9:33 pm

You're passing a pointer so sizeof is giving you the size of the pointer.
--~~~~
User avatar
Emery
 
Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

Postby devil_slayer » Sun Jul 31, 2005 4:36 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.

One thing. Instead of:
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.

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

gives 99 instead of 100?
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby devil_slayer » Sun Jul 31, 2005 5:02 am

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;
}
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby tomcant » Sun Jul 31, 2005 5:33 am

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

gives 99 instead of 100?


[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.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby devil_slayer » Sun Jul 31, 2005 9:16 am

Well I compiled it under bloodshed and got this wierd result... Quite strange...
User avatar
devil_slayer
 
Posts: 489
Joined: Wed Oct 01, 2003 3:44 am
Location: Warsaw, POLAND

Postby Emery » Sun Jul 31, 2005 11:51 am

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.
--~~~~
User avatar
Emery
 
Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

Postby MXP » Sun Jul 31, 2005 1:42 pm

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

Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 1 guest