===================Example1===================
// sawtooth_x.c

// This program draws a "sawtooth wave" on the screen

// Compile command for this code, c version:
// gcc -Wall -W -Werror sawtooth_x.c -o sawtooth_x
// Compile command for this code, c++ version:
// g++ -Wall -W -Werror sawtooth_x.c -o sawtooth_x
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int numbers[10];
int i;
int j;
int k;

for(k = 0; k<10; k++){
for(j = 0; j<10; j++){
/* populate the array */
for(i = 0; i<10; i++){numbers[i] = 0;}
numbers[j] = 1;
for(i = 0; i<10; i++){
if (numbers[i]==0) {printf(" ");}
else {printf("x");}
}
printf("\n");
usleep(100000);
}
}
return(0);
}
===================Example2===================
// sinewave_x.c

// This program draws a sine wave on the screen

// Compile command for this code, c version. Note the explicit link to the math library:
// gcc -Wall -W -Werror sinewave_x.c -o sinewave_x -lm
// Compile command for this code, c++ version:
// g++ -Wall -W -Werror sinewave_x.c -o sinewave_x

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <math.h>
int main() {
int numbers[80];
int i;
int j;
int x;
for(j = 0; j<500; j++){
/* populate the array */
for(i = 0; i<80; i++){numbers[i] = 0;}
x=floor((sin(j/10.0)+1.1)*35.0);
if (x<0) x=0; //Safety: make sure x is never negative
if (x>79) x=79; //Safety: make sure x is never bigger than 79
numbers[x] = 1;
for(i = 0; i<80; i++){
if (numbers[i]==0) {printf(" ");}
else {printf("x");}
}
printf("\n");
usleep(10000);
}
return(0);
}
===================Example3===================

// damped_oscillating_wave.c

// This program draws the motion of a spring with a damper.
// Its the same motion as a car after it hits a bump.

// Compile command for this code, c version. Note the explicit link to the math library:
// gcc -Wall -W -Werror damped_oscillating_wave.c -o damped_oscillating_wave -lm
// Compile command for this code, c++ version:
// g++ -Wall -W -Werror damped_oscillating_wave.c -o damped_oscillating_wave

// To run the program type
// ./damped_oscillating_wave

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

double damped_sine(double x) // x is the input
{
double a; // a is the output
a = sin(x/2)*exp(-x*0.1);
return(a);
}

int main() {
int numbers[80];
int i;
int j;
int x;
for(j = 0; j<75; j++){
/* populate the array */
for(i = 0; i<80; i++){numbers[i] = 0;}
x=floor((damped_sine(j)+1.1)*35.0);
if (x<0) x=0; //Safety: make sure x is never negative
if (x>79) x=79; //Safety: make sure x is never bigger than 79
numbers[x] = 1;
for(i = 0; i<80; i++){
if (numbers[i]==0) {printf(" ");}
else {printf("x");}
}
printf("\n");
usleep(10000);
}
return(0);
}




===================Example 4 ===================
// moving_x.c

// This program shows how you can make a single line print over and over.
// Using this trick you get an "animated" x moving across the screen

// Compile command for this code c version:
// gcc -Wall -W -Werror moving_x.c -o moving_x
// Compile command for this code c++ version:
// g++ -Wall -W -Werror moving_x.c -o moving_x

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
int main() {
int numbers[40];
int i;
int j;
int k;

for(k = 0; k<5; k++){
for(j = 0; j<40; j++){
/* populate the array */
for(i = 0; i<40; i++){numbers[i] = 0;}
numbers[j] = 1;
for(i = 0; i<40; i++){
if (numbers[i]==0) {printf(" ");}
else {printf("x");}
}
printf("\r");
fflush(stdout);
usleep(50000);
}
}
return(0);
}