Program to display this...

................................... *
...................................* *
..................................* * *
.................................* * * *
................................* * * * *
...............................* * * * * *

This thing should form a triangle, there are NO dots

I need to use the 'for loop', i guess
Anybody to help?
Last edited on
That sounds neat, good luck with it.
What have you got so far?
Here you go:
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
#include <iostream>

const int memory_size = 256;
const int stack_size = 32;

void brainfuck(const char* code){
    char memory[memory_size] = {0};
    int ip = 0, bp = 0, sp = 0;

    sp = 0; ip = 0;
    while(code[ip]){
        switch(code[ip]){
            case '+': ++memory[bp]; break;
            case '-': --memory[bp]; break;
            case '>': ++bp; break;
            case '<': --bp; break;

            case '[':
                if(memory[bp] == 0){
                    int tmp_sp = 0;
                    do{
                        if(code[ip] == '[') tmp_sp++;
                        if(code[ip] == ']') tmp_sp--;
                        ip++;
                    }while(code[ip] && tmp_sp != 0);
                }else ++sp;
                break;

            case ']':
                if(memory[bp] != 0){
                    int tmp_sp = 0;
                    do{
                        if(code[ip] == '[') tmp_sp++;
                        if(code[ip] == ']') tmp_sp--;
                        ip--;
                    }while(ip >= 0 && tmp_sp != 0);
                }else ++sp;
                break;

            case ',': memory[bp] = std::cin.get(); break;
            case '.': std::cout.put(memory[bp]); break;
        }
        ip++;
    }
}

int main(){
    brainfuck("++++++++++>>++++[-<++++++++>]>+++++++[-<++++++>]"
              "++++++ [[->+>+>-<<<]->[-<+>]>[-<<<<.>>>>]"
              ">+++++++[-<<<<.<.>>>>>]<<<<<<.>>>],");
    return 0;
}
You love BF don't you?
try doing a right angled triangle first which is marginally simpler especially when filled.
@Bazzy,
Indeed I do. I'll try to be more original in the future though, as I've already posted similar code once. I was considering a genetic algorithm, but couldn't come up with a logical fitness function..
Oh well, I guess there will still be plenty of opportunities to do somebodies homework in the most unhelpful possible way...
^^^ I thoroughly enjoy that logic.
Lol hamsterman, I think it's safe to say that's the coolest thing I've ever seen.
Gah, I hate that exercise. My friend asked me to help him with it and I just brute-forced it all in. :<
hamsterman: There's no for loops in that though.
what do you mean? There are plenty of them. ++++[-/*do something here*/] is a for loop
I'm writing the be-all, end-all ASCII triangle drawing program. You can have the code for free when I'm done. Enabling output will cost £10.
Last edited on
hamsterman: Would you kindly explain it to me? This is not a homework question, for me at least.

chrisname: I'd personally be more apt to pay you for the sorce code than anything else.
Topic archived. No new replies allowed.