Understanding problem

Hi good morning from Colombia

i would like to share to you a problem.

I was giving a try and this is what i got so far.

i will be keep an eye over your suggestion.

best regards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* 
6.24 (Separating Digits) Write program segments that
accomplish each of the following:
A. Calculate the integer part of the quotient when integer is
divided by integer .
B. Calculate the integer remainder when integer is divided
by integer .
C. Use the program pieces developed in (a) and (b) to write a
function that inputs an integer between and and
prints it as a series of digits, each pair of which is separated
by two spaces. For example, the integer should print
as follows:

*/

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

void numberSeparatedDigits(int);

int main(int argc, char** argv)
{
	
	int numero;
	
	cout << "Enter NUMBER  between 1 - 32767 ----> ";
	cin >> numero;
	
	cout << "The number separeted in digits: " ; 
	numberSeparatedDigits(numero);
		
	return 0;
}

void numberSeparatedDigits(int number)
{
	int store = 0; 
	int store2 = 0;
	
	if(number > 0)
	{
		if(number < 10)
	    {
			store = number / 1;
			cout << store;	   
	    }
	    
	    //case for a number with 2 digits
	    else if(number < 100)
		{	
			store = number / 10;
			store2 = number % 10;
			cout << store << "  ";
				
			store = store2 / 1;
			cout << store;	
		}
		
		//if the number contains 3 digits
		else if(number < 1000)
		{
			store = number / 100;
			store2 = number % 100;
			cout << store << "  ";
				
			store = store2 / 10;
			cout << store << "  ";	
			
			store2 = number % 10;
			store = store2 / 1;
			cout << store; 	
		}
		
		//four digits
		else if(number < 10000)
		{
			store = number / 1000;
			store2 = number % 1000;
			cout << store << "  ";
				
			store = store2 / 100;
			cout << store << "  ";	
			
			store2 = number % 100;
			store = store2 / 10;
			cout << store << "  "; 
			
			store2 = number % 10;
			store = store2 / 1;
			cout << store;	
		}
		
		//Finallly 5 digits
			else if(number < 100000)
		{
			store = number / 10000;
			store2 = number % 10000;
			cout << store << "  ";
				
			store = store2 / 1000;
			cout << store << "  ";	
			
			store2 = number % 1000;
			store = store2 / 100;
			cout << store << "  "; 
			
			store2 = number % 100;
			store = store2 / 10;
			cout << store << "  ";	
			
			store2 = number % 10;
			store = store2 / 1;
			cout << store;
		}		
	}
	
}
Not bad, you solved C before A and B. Now analyse your solution what may be used for A and what for B. (Hint: just look at your 5-digits procedure, what is constant, what changes.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

void print_seperate_digits( int number )
{
    const char separator[] = "  " ; // two spaces

    if( number < 0 ) // if the number is negative
    {
        std::cout << '-' << separator ; // print a negative sign
        print_seperate_digits( -number ) ; //and then the digits of the positive number
    }

    // invariant: when we reach this point, number >= 0

    else if( number < 10 ) std::cout << number << separator ; // if it is a single digit number, print the digit

    else // two or more digits
    {
        print_seperate_digits( number/10 ) ; // print the more significant digits
        std::cout << number%10 << separator ; // print the last digit
    }
}

int main()
{
    for( int n : { 0, -123456, 98765432 } )
    {
        std::cout << n << "  =>  " ;
        print_seperate_digits(n) ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/c36a299fbbe6c5aa
Topic archived. No new replies allowed.