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

From XPUB & Lens-Based wiki
< User:Lieven Van Speybroeck
Revision as of 20:05, 6 November 2010 by Lieven Van Speybroeck (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

PROTOTYPING SESSION 02/11/2010:

To prepare for some C scripting, some basic instructions are welcome...:

#include "stdio.h" // include a core library that is needed for compiling your code
#include "string.h" // include a library for basic string functions


// 2 sorts of variables:
// char -> character variable
// int  -> number variable

int i = 65;
char i2 = 'A';
char name[]="Lieven"; // define a variable that is a string/array ( [] ) of characters (char). Each character in the string can be interpreted as a seperate element of an array

int main () { // defining the 'main' function - standard/mandatory for every C program

	i = 1;
	while (i < 100) {
		printf("hello world %d\n", i); //'%d' -> 'paste a number', followed by a variable to be pasted as number at the position of %d (ASCII numvalues for characters)
		printf("hello world %c\n", i); //'%c' -> 'paste a character', followed by a variable to be pasted as a character at the position of %c (ASCII charvalues for numbers)
		printf("%s\n", name); //'%s' -> 'paste a string', followed by a variable to be pasted as a string at the position of %s
		printf("%c\n", name[3]); // -> paste the character at position '3' (4th letter) of the string that is inside the variable 'name'
		i = i + 1;
	}
}

// to run this code in terminal: 
// ~$ gcc filename_of_script.c -o filename
// ~$ ./filename

// debugger for C in GNU environment: DDD
// to debug your program:
// ~$ gcc -g filename_of_script.c -o filename
// ~$ ddd filename
// -> then set a breakpoint in your code where you want to start checking/debugging


Then, use this knowledge to create Clapping Music in C

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 modulo 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
		}
		printf ("SHIFT!\n"); // show where the pattern actually shifts after each chunk of repetitions
		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
  
SHIFT!
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
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
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