User:Lieven Van Speybroeck/Prototyping/3-Clapping Music in C

From XPUB & Lens-Based wiki
< User:Lieven Van Speybroeck
Revision as of 19:19, 2 November 2010 by Lieven Van Speybroeck (talk | contribs) (Created page with "'''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: <source lang="C"> #include "st...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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 ";
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
	}
}