Click here to Skip to main content
15,915,509 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalquick c++ problem for an exam Pin
VIP-CoMmAnDo17-Jan-08 10:13
VIP-CoMmAnDo17-Jan-08 10:13 
GeneralRe: quick c++ problem for an exam Pin
Mark Salsbery17-Jan-08 10:39
Mark Salsbery17-Jan-08 10:39 
GeneralRe: quick c++ problem for an exam Pin
CPallini17-Jan-08 10:48
mveCPallini17-Jan-08 10:48 
JokeRe: quick c++ problem for an exam Pin
Iain Clarke, Warrior Programmer18-Jan-08 2:51
Iain Clarke, Warrior Programmer18-Jan-08 2:51 
JokeRe: quick c++ problem for an exam Pin
David Crow18-Jan-08 3:02
David Crow18-Jan-08 3:02 
JokeRe: quick c++ problem for an exam Pin
CPallini18-Jan-08 3:09
mveCPallini18-Jan-08 3:09 
JokeRe: quick c++ problem for an exam Pin
Mark Salsbery18-Jan-08 7:30
Mark Salsbery18-Jan-08 7:30 
GeneralRe: quick c++ problem for an exam Pin
Dr. Emmett Brown17-Jan-08 10:55
Dr. Emmett Brown17-Jan-08 10:55 
Here you have a couple of solutions in easy C++, suposing you have a certain idea of the size of the integer(MAX_DIGITS).
Solution 1 makes it step by step. Solution 2 avoids unecessary initialization.

SOLUTION 1
----------

int a;	// Supose this is the integer<br />
int separated_a[MAX_DIGITS];	// Here you'll store the result<br />
<br />
// Initialize the array:<br />
for (int i = 0; i < MAX_DIGITS; i++)<br />
	separated_a[i] = 0;<br />
<br />
int current_digit = 0;<br />
while (a>0)<br />
{<br />
	// In C++, the operator % stands for the module operation<br />
	separated_a[current_digit] = a % 10;<br />
	a = a/10;<br />
	current_digit++;<br />
}


SOLUTION 2
----------

int a;	// Supose this is the integer<br />
int separated_a[MAX_DIGITS];	// Here you'll store the result<br />
<br />
for (int i = 0; i < MAX_DIGITS; i++)<br />
{<br />
	if (a>0)<br />
	{<br />
		separated_a[i] = a % 10;<br />
		a = a/10;<br />
	}<br />
	else<br />
	{<br />
		separated_a[i] = 0;<br />
	}<br />
}	


Example:
Input: a = 245
MAX_DIGITS = 5
Output:
separated_a = [0,0,2,4,5]

Warning: I didn't compile them, but I hope they help you. Poke tongue | ;-P

Cheers

rotter

GeneralRe: quick c++ problem for an exam Pin
Mark Salsbery17-Jan-08 11:11
Mark Salsbery17-Jan-08 11:11 
GeneralRe: quick c++ problem for an exam Pin
CPallini17-Jan-08 11:27
mveCPallini17-Jan-08 11:27 
GeneralRe: quick c++ problem for an exam Pin
Mark Salsbery17-Jan-08 11:29
Mark Salsbery17-Jan-08 11:29 
GeneralRe: quick c++ problem for an exam Pin
CPallini17-Jan-08 11:38
mveCPallini17-Jan-08 11:38 
GeneralRe: quick c++ problem for an exam Pin
VIP-CoMmAnDo18-Jan-08 14:56
VIP-CoMmAnDo18-Jan-08 14:56 
GeneralRe: quick c++ problem for an exam Pin
CPallini19-Jan-08 4:26
mveCPallini19-Jan-08 4:26 
GeneralRe: quick c++ problem for an exam Pin
VIP-CoMmAnDo19-Jan-08 5:13
VIP-CoMmAnDo19-Jan-08 5:13 
GeneralRe: quick c++ problem for an exam Pin
David Crow17-Jan-08 14:15
David Crow17-Jan-08 14:15 
GeneralRe: quick c++ problem for an exam Pin
Luc Pattyn17-Jan-08 11:33
sitebuilderLuc Pattyn17-Jan-08 11:33 
GeneralRe: quick c++ problem for an exam Pin
VIP-CoMmAnDo17-Jan-08 11:15
VIP-CoMmAnDo17-Jan-08 11:15 
QuestionEh, why can't VS2005 find my include files? Pin
@largeinsd17-Jan-08 6:20
@largeinsd17-Jan-08 6:20 
AnswerRe: Eh, why can't VS2005 find my include files? Pin
CPallini17-Jan-08 6:23
mveCPallini17-Jan-08 6:23 
GeneralRe: Eh, why can't VS2005 find my include files? Pin
@largeinsd17-Jan-08 6:36
@largeinsd17-Jan-08 6:36 
GeneralRe: Eh, why can't VS2005 find my include files? Pin
Mark Salsbery17-Jan-08 7:16
Mark Salsbery17-Jan-08 7:16 
GeneralRe: Eh, why can't VS2005 find my include files? Pin
@largeinsd20-Jan-08 7:44
@largeinsd20-Jan-08 7:44 
GeneralRe: Eh, why can't VS2005 find my include files? Pin
Mark Salsbery21-Jan-08 10:51
Mark Salsbery21-Jan-08 10:51 
QuestionWay to enable ICS automatically in code? Pin
Member 427710417-Jan-08 6:15
Member 427710417-Jan-08 6:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.