User:Yiorgos Bagakis/prototyping/assignment3: Difference between revisions
Line 39: | Line 39: | ||
//ddd is a gnu compiler - download it first :) | //ddd is a gnu compiler - download it first :) | ||
</source> | |||
'''file:''' aymclapping.c | |||
'''description:''' printing a pattern similar to the clapping music pattern but with "x" instead of sound... | |||
#include "stdio.h" //include the library - very basic one | |||
<source lang="php"> | |||
char pat[] = "xxxoxxoxoxxo"; //array of characters | |||
int i = 0; | |||
int j = 0; | |||
int main() | |||
{ | |||
while (j < 13) //this loop will shift the pattern 12 times | |||
{ | |||
while (i < 12) //this loop will print the pattern | |||
{ | |||
printf("%c %c\n", pat[i], pat[(i+j)%12]); //prints side by side the pattern | |||
//%c for dispaying character - %s for displaying string - %12 is modulo 12 division reminder | |||
i++; // it is ading 1 to i --> like i = i + 1 | |||
} | |||
printf("SHIFT\n"); //prints shift | |||
i = 0; | |||
j++; | |||
} | |||
</source> | </source> |
Revision as of 16:58, 13 November 2010
C programming practice
The following baby programs were written using the Gedit text editor. The idea was to familiarize myself with C before using it with Arduino. So first attempt was to recreate the clapping music that we did with Micheal as the 2nd prototyping assigment but most of us didn't really complete it successfully.
file: loop.c
description: a simple while loop printing "hello world" 6 times.
#include "stdio.h"
int i=65;
int main () //it's an integer variable but is actually a function - it is standard for every C program
{
i=1;
while (i<63)
{
printf("hello world %d\n", i); //whatever i get here (i) paste it here (%d\n) and is a decimal (d)
//printf("hello world %c\n", i); //whatever i get here (i) paste it here (%c\n) and is a char (c)
i = i * 2;
}
}
//gcc loop.c -o loop
//$ ./loop
// the above 2 lines are for running the loop.c on the terminal...
//for ASCII check the tables eg on google. in our case: A=65
//printf function expects a string inside...eg: "hello world"
//debugging tip! Use printf (for C) or Serial.printf (for Arduino)- to test if things work
//
//to compile use this:
//gcc -g loop.c -o loop
//ddd loop
//ddd is a gnu compiler - download it first :)
file: aymclapping.c
description: printing a pattern similar to the clapping music pattern but with "x" instead of sound...
- include "stdio.h" //include the library - very basic one
char pat[] = "xxxoxxoxoxxo"; //array of characters
int i = 0;
int j = 0;
int main()
{
while (j < 13) //this loop will shift the pattern 12 times
{
while (i < 12) //this loop will print the pattern
{
printf("%c %c\n", pat[i], pat[(i+j)%12]); //prints side by side the pattern
//%c for dispaying character - %s for displaying string - %12 is modulo 12 division reminder
i++; // it is ading 1 to i --> like i = i + 1
}
printf("SHIFT\n"); //prints shift
i = 0;
j++;
}