1

I have a for loop generating integers.

For instance:

for (int i=300; i>200; i--)
    {(somefunction)*i=n;
    cout<<n;
    }

This produces an output on the screen like this:

f=00000000000100023;

I want to store the 100023 part of this number (i.e just ignore all the zeros before the non zero numbers start but then keeping the zeros which follow) as an array.

Like this:

array[0]=1;
array[1]=0;
array[2]=0;
array[3]=0;
array[4]=2;
array[5]=3;

How would I go about achieving this?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
user103572
  • 489
  • 1
  • 6
  • 9
  • If your number is stored as an integer f, it will not be stored as f=0000000...whatever. Integers do not store leading zeros. – Paul Sonier May 18 '09 at 19:34
  • duplicate: http://stackoverflow.com/questions/515612/convert-an-integer-number-into-an-array – lothar May 18 '09 at 20:13
  • 1
    @lothar-since the question is different in what it proposes I don't see it as being a duplicate – TStamper May 18 '09 at 20:22
  • 1
    @McWafflestix: Sorry to be a stickler. but an integer WILL be stored with the leading zeros because regardless of value it will use up the whole 32 bits of storage regardless of the numeric value. Of course, you are completely correct that the string representation of that integer will not by default display leading zeros making my nitpick a moot point. – JohnFx May 18 '09 at 20:25
  • 2
    You have an impressive compiler if it can solve "(somefunction)*i=n"; but i think it's more likely a typo. – Max Lybbert May 18 '09 at 23:10

5 Answers5

4

This is a mish-mash of answers, because they are all there, I just don't think you're seeing the solution.

First off, if they are integers Bill's answer along with the other answers are great, save some of them skip out on the "store in array" part. Also, as pointed out in a comment on your question, this part is a duplicate.

But with your new code, the solution I had in mind was John's solution. You just need to figure out how to ignore leading zero's, which is easy:

std::vector<int> digits;
bool inNumber = false;

for (int i=300; i>200; i--)    
{
    int value = (somefunction) * i;

    if (value != 0)
    {
        inNumber = true; // its not zero, so we have entered the number
    }

    if (inNumber)
    {
        // this code cannot execute until we hit the first non-zero number
        digits.push_back(value);
    }
}

Basically, just don't start pushing until you've reached the actual number.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
3

In light of the edited question, my original answer (below) isn't the best. If you absolutely have to have the output in an array instead of a vector, you can start with GMan's answer then transfer the resulting bytes to an array. You could do the same with JohnFx's answer once you find the first non-zero digit in his result.


I'm assuming f is of type int, in which case it doesn't store the leading zeroes.

int f = 100023;

To start you need to find the required length of the array. You can do that by taking the log (base 10) of f. You can import the cmath library to use the log10 function.

int length = log10(f);
int array[length];

length should now be 6.

Next you can strip each digit from f and store it in the array using a loop and the modulus (%) operator.

for(int i=length-1; i >= 0; --i)
{
    array[i] = f % 10;
    f = f / 10;
}

Each time through the loop, the modulus takes the last digit by returning the remainder from division by 10. The next line divides f by 10 to get ready for the next iteration of the loop.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • bill thats a great answer but i made a mistake saying the number was an integer. sorry! thanx you for your help. – user103572 May 18 '09 at 20:02
2

The straightforward way would be

std::vector<int> vec;
while(MyInt > 0)
{
  vec.push_back(MyInt%10);
  MyInt /= 10;
}

which stores the decimals in reverse order (vector used to simplify my code).

stefaanv
  • 14,072
  • 2
  • 31
  • 53
2

Hang on a second. If you wrote the code generating the integers, why bother parsing it back into an array?

Why not just jam the integers into an array in your loop?

int array[100];

for (int i=300; i>200; i--)    
{
    array[i]= (somefunction)*i;    
}
JohnFx
  • 34,542
  • 18
  • 104
  • 162
  • it is necessary to get rid of the preceeding zeros as the number required will only be of value if it is the correct order, hope that helps – user103572 May 18 '09 at 20:08
  • I still think this will work. The leading zeros in an integer are just logical. If they are showing up from your programming that is just a formatting issue not a storage issue. In fact, it takes EXTRA work to get leading zeros to show up like that. The code you provided in the original question would not produce the output you described. – JohnFx May 18 '09 at 20:21
  • 1
    After looking at your code some more, I think you might be confusing what cout displays and what is actually stored. You might have, for example, setfill('0') somewhere in the code that will pad the number when printed out via COUT. – JohnFx May 18 '09 at 20:31
  • @noob09: I would just do what JohnFX has here, then loop through the array at the end to find the first non-zero digit, get the size of the result from that, then copy the remaining digits into a new array. – Bill the Lizard May 18 '09 at 20:43
  • Why loop through the array? At the end of the code I posted @noob09 should have the final output he was looking for with no additional work required. – JohnFx May 18 '09 at 21:22
  • @JohnFx: You'll have all 100 digits in your array, including leading zeroes. The extra loop is just to find the first non-zero digit and return the remainder of the array. – Bill the Lizard May 19 '09 at 02:21
  • I was assuming that (somefunction) returned an integer. Are you thinking he meant it returned a single character? – JohnFx May 19 '09 at 06:25
1

Since the leading zeros are not kept because it represents the same number

See: convert an integer number into an array

Community
  • 1
  • 1
TStamper
  • 30,098
  • 10
  • 66
  • 73