You have to write an ITERATIVE procedure write_digit(d,x) that receives a digit d and a natural number x, and writes x times the digit d in the standard output (cout). For example, the call write_digit(3,5) writes 33333, whereas the call write_digit(5,3) writes 555.
You have also to write an ITERATIVE procedure write_expanded(n) that receives a natural number n > 0, and writes in the standard output (cout) each digit of n as many times as the digit value plus 1. For instance, write_expanded(315) writes 333311555555 in the cout, whereas write_expanded(204) writes 222044444. Your implementation of write_expanded(n) must conveniently use the function write_digit(d,x).
Your implementation of both procedures must be ITERATIVE and use the following C++ code, modifying only the parts that are indicated. Notice that the main procedure below writes an end-of-line after each call to write_expanded. Thus, the procedures must not write the end-of-line.
INPUT
315
204
112233
332211
200000
456789
999999
100000
OUTPUT
333311555555
222044444
111122222233333333
333333332222221111
22200000
444445555556666666777777778888888889999999999
999999999999999999999999999999999999999999999999999999999999
1100000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
// define here additional functions and/or procedures
// if you need them
// Pre: 0<=d<=9 and 0<=x.
void write_digit(int d,int x) {
// insert here your (ITERATIVE) code
}
// Pre: 0<n.
void write_expanded(int n) {
// insert here your (ITERATIVE) code
}
int main() {
int n;
while (cin >> n) {
write_expanded(n);
cout << endl;
}
}
|
I made a program that does what the program asks but couts the inversed result. Did it intentionally because if I had done it normal it would be imposible because I do not know the number of digits the input will have.
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
|
#include <iostream>
using namespace std;
void write_digit(int d, int x) {
for (int i = 0; i < x; ++i) cout << d;
}
void write_expanded(int n) {
while (n != 0) {
if (n/10 > 0) {
int d = n%10;
int x = n%10 +1;
write_digit(d,x);
n /= 10;
d = 0;
x = 0;
}
else {
int d = n;
int x = n+1;
write_digit(d,x);
n = 0;
}
}
}
int main() {
int n;
while (cin >> n) {
write_expanded(n);
cout << endl;
}
}
|
To try to output the correct result, I inverted the input and now it gives me the proper solution in some cases. If there are leading zeroes, the program gives incorrect output. for example:
Input: (1000 -> 0001 = 1 "inverted input")
Output: (1100000 -> 11 "cout output")
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
|
#include <iostream>
using namespace std;
void write_digit(int d, int x) {
for (int i = 0; i < x; ++i) cout << d;
}
void write_expanded(int n) {
int reverse = 0;
while (n != 0) {
int remainder = n%10;
reverse = reverse*10 + remainder;
n /= 10;
}
while (reverse != 0) {
if (reverse/10 > 0) {
int d = reverse%10;
int x = reverse%10 +1;
write_digit(d,x);
reverse /= 10;
d = 0;
x = 0;
}
else {
int d = reverse;
int x = reverse+1;
write_digit(d,x);
reverse = 0;
}
}
}
int main() {
int n;
while (cin >> n) {
write_expanded(n);
cout << endl;
}
}
|
What can I do to make this program finally work as it is supposed to?