Output a sequence of strings

Let's assume I want to create a program whose output will be

1
1??2
1??2??3
1??2??3??4
1??2??3
1??2
1

Where the ? are question marks, not unknown other numbers.

How could I proceed, provided that:

1) I CANNOT use trivial things like a simple cout, and I mean that a program like


1
2
3
4
5
6
7
8
9
10
  #include<iostream>
using namespace std;

int main ( ) 

{ 

cout << "1\n1??2\n1??2??3\n1??2??3??4\n1??2??3\n1??2\n1\n";

}


would be invalid and trivial.


2) I Cannot use a for cycle the same.

Is there another cool / simple / elegant way to proceed?

Thank you so much!!
Last edited on
What do you mean when you say "cannot"? Do you mean you aren't allowed to use a for loop? (by the way, in English it would be "loop", not "cycle").

Are you allowed to use a while loop? You need to either use loops, or recursion. Recursion is when a function calls itself, e.g.
 
int f(n) { if (n == 10) return 1; else return 1 + f(n+1); }
Last edited on
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>
#include <iomanip>
using namespace std;

void digit( int n, int nmax )
{
   if ( n == 1 ) cout << 1;
   else          cout << setfill( '?' ) << setw( 3 ) << n;
   if ( n < nmax ) digit( n + 1, nmax );
   else            cout << '\n';
}

void sequence( int n, int nmax )
{
   digit( 1, n );   if ( n >= nmax ) return;
   sequence( n + 1, nmax );
   digit( 1, n );
}

int main()
{
   sequence( 1, 4 );
}
1
1??2
1??2??3
1??2??3??4
1??2??3
1??2
1
Last edited on
Topic archived. No new replies allowed.