Thread: converting an integar into an array?

1. converting an integar into an array?

I am trying to come up with a program that converts a number
say:-

1359 and the program turns it into £13.59.

I know of the atoi function that converts a string into a number
but i want to do the opposite so that i can convert

int num=1359;
int numArray[]=function(num);

so that numArray[0] =1
numArray[1]=3
numArray[2]=5
numArray[3]=9

does such a function exist or am i approaching the problem all
wrong here?

Any help appreciated!

2. Here is a hint:

1359 % 10 = 9
135 % 10 = 5
13 % 10 = 3
13 / 10 = 1 (as you are using integers)

3. I am still working on the same program and i have discovered
something that totally baffles me(even more so than normal)
Can any one explain how the variable num is changed from 14
to 1 .

Code:
```int main(void)
{
int num=314;
int numArray[]={0};

int num2=num/100;
numArray[0]=num2;
num=num%100;

num2=num/10;

cout << num << endl;

numArray[1]=num2;		// how is this line changing
// num from 14 to 1 ?
cout << num << endl;

return(0);
}```

4. i have discovered if i declare

int numArray[5]={0} with a value instead of
int numArray[]={0} it works as i would expect but
I dont know how big i want the array to be when the
program starts is this where i would use Dynamic Memory Allocation?

Thanks!

5. The code below more or less does what i set out to do:-

Code:
```// this program converts the value of
// a variable and puts it into array

#include<iostream>
using namespace std;

int main(void)
{
const int MAX=4;		// Maximum size for array
int numToConvert=1392;	// This number will be represented as cash
int array[MAX]={0};
int num=1000;			// number used to divide

for(int i=0; i<MAX; i++)
{

int numPutinArray=numToConvert/num;
array[i]=numPutinArray;
numToConvert%=num;

num/=10;
}

for(i=0; i<MAX; i++)
{
cout << array[i] << endl;
}
cout << endl;

return(0);
}```
but i am not sure that the program design is that good . Can anyone offer any constructive advice?

6. as long as numToConvert is always 4 digits long your code looks and works fine. But what happens if you have input other than 4 digits long? If you use the style suggested by minesweeper you can do the same thing with an integer having any number of digits, the only problem being the numbers are retrieved in reverse order, but that's easily overcome, and your program becomes much more generalized.

7. I was struggling to complete the solution used by minesweeper.

1359 % 10 = 9
135 % 10 = 5
13 % 10 = 3
13 / 10 = 1 (as you are using integers)

i couldnt think of how to lose the 9 to leave 135 .
I will have another think about because that was the main reason
i didnt like my solution because of the limitation to 4 digits.

As you said the numbers are recieved in reverse order so then is
it just a case of starting at the end of the array then working backwards?

8. Try working through this:

int buffer80[];
int i = -1;
while(numToConvert > 0)
{
buffer[++i] = numberToConvert % 10;
numberToConvert /= 10;
}

for(i; i >= 0; i--)
{
cout << buffer[i];
}

9. why not have a loop to test which tens place the number is in then do the math. And if the last two digits are always after the decimal then you could always do something like this

Code:
```int number = 1359
char str[10];
sprintf(str, "£%d.%d", number/100, number%100);
//<edit>had a typo in the code</edit>```

10. Unregistered dude thanks . Your code worked great . Just
gotta digest it now.

Code:
```int main(void)
{
int buffer[80];
int numberToConvert=2532;
int i = -1;
while(numberToConvert > 0)
{
buffer[++i] = numberToConvert % 10;
numberToConvert /= 10;
}

for(i; i >= 0; i--)
{
cout << buffer[i] << endl;
}

return(0);
}```

11. master5001 I havent really learnt anything about c programming
was your code the c style of doing things?

Once i have my array of numbers . I have got this code to
output as cash. e.g. £135.94.

Does this look about right to you guys?

Code:
```#include<iostream>
using namespace std;

void NumToMoney(int array[], int arraySize);

int main(void)
{
int numArray[]={1,3,5,9,4};

NumToMoney(numArray, (sizeof numArray/sizeof numArray[0]));

return(0);
}

void NumToMoney(int array[], int arraySize)
{

cout << char(156);

for(int i=0; i<arraySize; i++)
{
cout << array[i];
if(i==arraySize-3)
{
cout << ".";
}
}

cout << endl;
}```

12. Here is my solution:

Code:
```#include <iostream.h>
#include "apvector.h"

int getlength(int sent);
int getmember(int sent, int member);
int power(int num, int pow);
void outmoney(int nonmoney);

int main()
{
outmoney(321231);
outmoney(231);
outmoney(897668);
outmoney(13594);
outmoney(1);
outmoney(12);
return 0;
}

int getlength(int sent)
{
int count=1;
while(sent>=10)
{
sent/=10;
++count;
}
return count;
}

int getmember(int sent, int member)
{
int sentlength, sent2, temp1, temp2, converted;
sentlength=1;
sent2=sent;
while(sent2>=10)
{
sent2/=10;
++sentlength;
}
temp1=power(10,sentlength-member);
temp2=power(10,sentlength-(member+1));
converted=(sent%(temp1))-(sent%(temp2));
while(converted>=10)
converted/=10;
return converted;
}

int power(int num, int pow)
{
if(pow==0)
return 1;
int returned;
returned=num;
while(pow>1)
{
returned*=num;
--pow;
}

return returned;
}

void outmoney(int nonmoney)
{
int length=getlength(nonmoney);
if(length==1)
cout<<char(156)<<'.'<<0<<nonmoney;
else
{
apvector<int> moneyarray(length);
for(int f1=0;f1<length;++f1)
moneyarray[f1]=getmember(nonmoney,f1);
cout<<char(156);
for(int f2=0;f2<length;++f2)
{
if(f2==length-2)
cout<<'.'<<moneyarray[f2];
else
cout<<moneyarray[f2];
}
}
cout<<endl;
}```
I wrote the above funtions for previous programs and they have been very usefull.

The functions getlength and getmember are used to typecast ints into arrays (or vectors). getlength returns the length of an int as if it were an array of ints less than 10. getmember returns the member specified of your virtual array of ints less than 10 (or any int . I used vectors in my example because I can't stand how unflexable plain arrays are.

You like?
()Q()

13. ()Q() I tried to run your program but i dont have that header
file.

As a c++ programmer gets more experience do they use vectors
nearly all the time as opposed to normal arrays?

14. I reckon you're solution is a bit of overkill.

Code:
```#include <iostream>
using namespace std;

int ShowMoneyValue(int value)
{
cout << char(156) << value / 100 << "." << (value % 100 < 10 ? "0" : "") << value % 100 << endl;
return(1);
}

int main(void)
{
ShowMoneyValue(2930);
ShowMoneyValue(2000);
ShowMoneyValue(10);
ShowMoneyValue(30478);
ShowMoneyValue(0);
return(1);
}```
cheers
U.

15. To answer your question.... if you can use arrays, then use them. They are much faster than vectors. Vectors are used alot when you need an array whose size changes all the time. It makes it easier to control.

But if you dont need to resize arrays, then use plain old arrays.

Better still, if you dont need to, then dont use arrays at all!

U.

Popular pages Recent additions