unix programming

Pages: 123
write a program in unix which display a following pattern
1
01
101
0101
10101
That would be 20€ (to be paid in advance).
I wont do it for you, but I'd suggest a recursive function that outputs 1 and 0.
come on people. help with the homework already

 
 echo '1\n01\n101\n0101\n10101'
truedream143 wrote:
write a program in unix which display a following pattern
1
01
101
0101
10101

Okay, done.
Done with the writing :)

For the actual code you'll have to give me 10€ :) (which is discount on Athar's price :)
I'll further undercut the price and do it for £1 (€1.22).
I changed my mind. I'll do it for no money, all you have to do is tell me your parents' credit card details.
Last edited on
I'll do it for free! Just pay shipping and handling.
write a program in unix which display a following pattern
Write a program in unix
you should be careful.

these sort of people usually end up as project managers.
Ok guys. Stop BASHing the poor guy!
I'll have a little fun with this one...
1
2
3
4
5
6
std::string s( "1" );
for( int i = 5; i > 0; --i )
{
    std::cout << s << std::endl;
    s = ( i % 2 ? "0" : "1" ) + s;
}
1
01
101
0101
10101
Last edited on
Why did you do it for him?
Hope this helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

unsigned r( unsigned x, unsigned y ) {
    return ( y & 1 ) && !( y & ~1 ) ? x : ( y ^ ( ( y >> 1 ) << 1 ) ) * x +
        ( r( x, ( y & ~1 ) >> 1 ) << 1 );
}

int main() {
    unsigned x = !!( r( 10, 1 ) ^ ~r( 1, 10 ) );
    unsigned c = !( ( x & ~x ) + 1 );
    do {
        switch( ( ( c >> 1 ) << 1 ) ^ c ) {
            default: std::cout << 1;
            case 0: case 1 << 1: case 1 << 1 << 1: if( c - ( ( c >> 1 ) << 1 ) )
            case 1: case 1 + ( 1 << 1 ): case ( 1 + 1 ) << 1 + 1: std::cout << 0;
        }
        std::cout << x << std::endl;
        x = r( x, c & 1 ? ( ( ( 011 << 1 ) + ( 010 << 1 << 1 ) )
            << 1 ) : 1 ) | ( c ^ ( ( c >> 1 ) << 1 ) );
    } while( ++c < ( ( ( 1 + 1 ) ^ 1 ) << 1 ) - 1 );
}


PS: If you need any help, ask soon because I'll forget how I got this to work in about 15 seconds.
Last edited on
You like bitwise operators, don't you?
Is this now a "who can do this in the most ridiculously complex way" competition?
Well, the trick was to make the program only use 1s and 0s, because executable programs are binary files,
and binary files obviously can only have 1s and 0s. So, are you saying there was a simpler way?
jsmith, nice code. :P
Im busy deciphering it, this is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

unsigned int r( unsigned int x, unsigned int y ) {
    return ( y & 1 ) && !( y & -2 ) ? x : y%2 * x +
        ( r( x, ( y & -2 ) >> 1 ) << 1 );
}

int main() {
    unsigned int x = 1;
    unsigned int c = 0;
    do {
        switch( c ) {
            default: std::cout << 1;
            case 0:
            case 2:
            case 4: break;
            case 1:
            case 3:
            case 8: std::cout << 0;
        }
        std::cout << x << std::endl;
        x = r( x, c & 1 ? 100 : 1 ) | c%2;
    } while( ++c < 5 );
}
Last edited on
@jsmith,
The question doesn't make sense. Of course there's a simpler way (of getting the same output); moorecm posted one. His way is more easily read and understood than yours, and it is also shorter, so it is provably simpler (or, at least, less complex). But your question seems to ask "is there a simpler way of getting the same output in the same way" which doesn't make sense, because it implicitly means "doing the same thing", which makes your question void (HEUHEUHEU).
Last edited on
Chris, just look up. I did exactly the same as jsmith, I really only removed redundant parts and changed the spacing.
Pages: 123