Program to display this...
SD123 (1)
Sep 5, 2010 at 5:48am UTC
................................... *
...................................* *
..................................* * *
.................................* * * *
................................* * * * *
...............................* * * * * *
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 Sep 5, 2010 at 5:57am UTC
mcleano (922)
Sep 5, 2010 at 12:43pm UTC
What have you got so far?
hamsterman (4435)
Sep 5, 2010 at 1:06pm UTC
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;
}
Bazzy (6281)
Sep 5, 2010 at 1:46pm UTC
You love BF don't you?
buffbill (416)
Sep 6, 2010 at 3:19am UTC
try doing a right angled triangle first which is marginally simpler especially when filled.
hamsterman (4435)
Sep 6, 2010 at 12:56pm UTC
@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...
Thumper (376)
Sep 7, 2010 at 12:38am UTC
Lol hamsterman, I think it's safe to say that's the coolest thing I've ever seen.
NGen (630)
Sep 7, 2010 at 2:53am UTC
Gah, I hate that exercise. My friend asked me to help him with it and I just brute-forced it all in. :<
wtf (664)
Sep 14, 2010 at 6:20pm UTC
hamsterman: There's no for loops in that though.
hamsterman (4435)
Sep 14, 2010 at 6:38pm UTC
what do you mean? There are plenty of them. ++++[-/*do something here*/ ] is a for loop
chrisname (6191)
Sep 14, 2010 at 6:57pm UTC
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 Sep 14, 2010 at 6:58pm UTC
wtf (664)
Sep 14, 2010 at 7:08pm UTC
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.