sample code for thise .. more Idea's

1
22
333
4444
55555
666666
7777777
88888888
999999999
88888888
7777777
666666
55555
4444
333
22
1

I would suggest you look into for loops.

EDIT: I tried it and found a typo, i+=1 instead of j+=1 in the inner for loops. Here is the code rewritten.

I hope you take your time to understand the approach, for it is indeed simple. Also, write it rather than copy-paste it.
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
#include <iostream>

int main()
{
	//the ascension to 9
	for(int i = 1; i <= 9; i += 1)
	{
		//print it x-times like this
		for(int j = 1; j <= i; j += 1)
		{
			std::cout << i;
		}
		//end line
		std::cout << std::endl;
	}
	//decend from 9.
	for(int i = 8; i >= 1; i -= 1)
	{
		//write it x-times
		for(int j = 1; j <= i; j += 1)
		{
			std::cout << i;
		}
		//end line
		std::cout << std::endl;
	}
	system("pause");
	return 0;
}


Please don't crucify me for using system("pause") it works for learning purposes with a debugger. Also, this is a rather peculiar thing to ask for...
Last edited on
I hope you take your time to understand the approach, for it is indeed simple. Also, write it rather than copy-paste it.


Nice sentiment but... You ARE kidding right??? There is a reason this site has a policy for not giving out complete code for obvious homework questions.
yes! complete codes should not be given!
For requests like this I've always wanted to give them some insanly Rube-Goldberg type code, with overloaded operators that don't accomplish their origional task (Like using the '?' operator to assign data to a variable, and later to do the type of comparison it was intended to) and pointless loops with process forks just to eat up CPU time. They would then have to stand there and explain to their instructor what is going on in the code. I'm wondering if that's the stance we should adopt to discourage these posts.
Last edited on
I apologize for breaking the site's policy.

Also, TS, you should really have a look at for loops. and have it loop from 1 to 9, within the loop have another that loops x times (say x = number you're on), and prints the number x times. Wonder what manner of instructors give this peculiar task...
Last edited on
closed account (zb0S216C)
Computergeek, do you mean something like this:

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
#include <process.h>
#include <iostream>
#include <vector>

std::vector < int * > vecIntAllocators;
bool bLoaded( false );

void Thread_LoadVector( void *pDummyArgs )
{
    for( int iIndex( 0 ); iIndex < 30; iIndex++ )
    {
        try
        {
            vecIntAllocators.push_back( new int( 10 ) );
        }
        
        catch( std::bad_alloc ) 
        {
            std::cout << "Bad allocation exception" << std::endl;
            vecIntAllocators.at( iIndex ) = NULL;
            return;
        }
    }
    bLoaded = ( ( !bLoaded ) ? true : false );
}

int main( )
{
    _beginthread( &Thread_LoadVector, 0, NULL );
    for( unsigned int iIndex( 0 ); iIndex < vecIntAllocators.size( ); iIndex++ )
        if( vecIntAllocators.at( iIndex ) )
            std::cout << *vecIntAllocators.at( iIndex ) << std::endl;

    while( !vecIntAllocators.empty( ) )
    {
        delete vecIntAllocators.back( );
        vecIntAllocators.pop_back( );
    }

    std::cin.get( );
    return 0;
}

Note: This code may not work. This was a quick type-up.

Wazzak
Last edited on
Here's mine:

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
#include <iostream>
#include <string>

void print_stuff(int min_n, int max_n)
{
    struct local
    {
        static void build_stuff(
                        int min_n, int max_n, int cur_n,
                        int cur_c, int max_c, bool inc,
                        std::string & ret)
        {
            if (cur_c == max_c)
            {
                if (cur_n == min_n && !inc) return;

                ret += '\n';

                if (cur_n == max_n) inc = false;

                int new_n = inc ? cur_n + 1 : cur_n - 1;

                return build_stuff(
                        min_n, max_n, new_n,
                        0, new_n, inc, ret);
            }

            ret += '0' + cur_n;

            return build_stuff(
                    min_n, max_n, cur_n,
                    cur_c + 1, max_c, inc, ret);
        }
    };

    std::string stuff;

    local::build_stuff(
            min_n, max_n, min_n,
            0, min_n, true, stuff);

    std::cout << stuff;
}

int main()
{
    print_stuff(1,9);

    return 0;
}
Last edited on
LOL! Those are awsome!
Topic archived. No new replies allowed.