Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Nested For loop help?

Name: Anonymous 2014-11-12 2:55

I need help with a For loop.

Using only For loops and a print function, let's say I wanted to print something like:

# # # # # #

Where the #'s and " "'s (spaces) are printed by a print function, one character at a time.

Notice how there's three spaces between the first and last sets of #'s?

How the heck can I get the For loops to only print three characters between the first and last #'s, but print six spaces for the inner ones?

Name: Anonymous 2014-11-12 2:56

Damn it, the spaces were left out. This is what I'm talking about:

#<3 spaces>#<6 spaces>#<6 spaces>#<6 spaces>#<3 spaces>#

Name: Anonymous 2014-11-12 3:16

That's why there's a preview button Mr. I'm-too-stupid-to-write-a-loop.

Name: Anonymous 2014-11-12 3:45

print '#'
loop over the following code 5 times
{
if we are on the first or last iteration, print out three spaces.
Otherwise, print out six spaces.
Print '#'
}

Name: Anonymous 2014-11-12 9:15

I did this by experiment hope it helps :)
> (define (gen x) (+ (* 2.24429 (sin x)) (* -1.02712 (cos x)) 4.18379))
> (define (flip n) (if (= 0 (mod n 2)) floor ceiling))
> (define (pad n) (begin (write-char #\space) (if (> (- n 1) 0) (pad (- n 1 )))))
> (define (loop n) (begin (write-char #\#) (if (< n 5) (begin (pad ((flip n) (gen n))) (loop (+ n 1))))))
> (define (hashes) (loop 0))
> (hashes)
# # # # # #

Name: Anonymous 2014-11-12 18:35

>>5
define
define
define
Die in a cockstorm, you queer. Or learn to use "def".

Name: Anonymous 2014-11-12 23:48

int k[5] = {2,5,5,5,2};
for(i = 0; i <= 4; i++){
print("#");
for(j = 0; j<=k[i]; j++){
print(" ");
}
}
print("#");

Don't change these.
Name: Email:
Entire Thread Thread List