User:Lieven Van Speybroeck/Prototyping/3-Clapping Music in C
< User:Lieven Van Speybroeck
Revision as of 18:26, 2 November 2010 by Lieven Van Speybroeck (talk | contribs)
This was the exercise we did during the prototyping lesson of 02/11/2010:
So how to "print" the Clapping Music in Terminal with a C script:
#include "stdio.h"
#include "string.h"
char pat[]="xxx xx x xx "; // the pattern we'll be working with: a string of characters
int pointer = 0;
int shift = 0;
int repeat = 0;
int main() {
while (shift <= strlen(pat)) { // play the whole score + play the first part 1 more time
while (repeat < strlen(pat)) { // repeat each block 12 times before there's a shift
while (pointer < strlen(pat)) { // go through the pattern and print the letter of the position of the pointer
printf("%c", pat[pointer]); // 1 pattern stays the same every time
printf("%c\n", pat[(pointer+shift) % strlen(pat)]); /* use modulus operator to check when the "shift" is equal to the length of the string, if so, go back to 0 so the shift doesn't exceed the amount of characters of the string -> this also makes it possible to play the score 1 more time without the shift at the end */
pointer++; // move the pointer 1 position
}
pointer = 0; // reset the pointer position
repeat++; // add up 1 to the repetition counter
}
repeat = 0; // reset the repeat
shift++; // add up 1 to the shift counter
}
}
if executed in the terminal, this would be the output:
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
xx
x
x
xx
x
x
x
x
xx
x
x
xx
xx
x
x
xx
x
x
x
x
xx
x
x
xx
xx
x
x
xx
x
x
x
x
xx
x
x
xx
xx
x
x
xx
x
x
x
x
xx
x
x
xx
xx
x
x
xx
x
x
x
x
xx
x
x
xx
xx
x
x
xx
x
x
x
x
xx
x
x
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