User:Yiorgos Bagakis/prototyping/assignment3: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with " == 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 f...")
 
 
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:


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.
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.
<source lang="php">
#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 :)
</source>
'''file:''' aymclapping.c
'''description:''' printing a pattern similar to the clapping music pattern but with "x" instead of sound...
<source lang="php">
#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++;
    }
 
</source>

Latest revision as of 00:50, 15 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++;

    }