#include <stdio.h>
#include <wiringPi.h>
void red(void);
void yellow(void);
void green(void);
int main()
{
/* set up the wiring pi library functions */
wiringPiSetup();
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
/* pinMode 0 is the GPIO_0 (GPIO Zero) pin that is */
/* pin #7 on the header - green*/
/* set up GPIO_0 as an output port (as opposed to input port * /
/*pinMode 1 is GPIO_1 yellow and pinMode 2 is GPIO_2 red */
while(1)
{
green();
delay(300);
yellow();
delay(300);
red();
delay(300);
}
return (0);
/* Not necessay in C Code - main() returns 0 <--zero */
/*at the termination of program */
}
void green(void)
{
digitalWrite(0,HIGH);
delay(700);
digitalWrite(0,LOW);
}
void yellow(void)
{
digitalWrite(1,HIGH);
delay(700);
digitalWrite(1,LOW);
}
void red(void)
{
digitalWrite(2,HIGH);
delay(700);
digitalWrite(2,LOW);
}
Monday, October 19, 2015
Average and Standard Deviation Program
/* Average (mean) and standard deviation program */
/* avg_std3.c */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Require math.h to perform square-root and square calculations */
int
main(
){
int i,j;
int x[10];
double sum,avg,sigma,sigma_sq;
int *ptr;
sum = 0;
for(i=0;i<10;i++)
{
x[i]=i;
/* ptr is assigned the value of the x[i] element address */
ptr=&x[i];
/* notice for long printf() statements, close the " and open " on next line */
printf("\nx[%d] = %d, at address using pointer *ptr %X, "
"and using pointer ADDRESS operator &x[i] = %X\n",i,x[i],ptr,&x[i]);
/* taking a running average by using the variable, sum */
/* also using the pointer *ptr to 'point' to the value */
sum=sum+*ptr;
}
j=i;
printf("\nSum = %f\n", sum);
avg=sum/(i);
printf("\nAverage = %f\n", avg);
sum=0;
for (i=0;i<10;i++)
{
/* the pow(a,b) = a^b using the math.h library */
sum=sum+pow((x[i]-avg),2);
}
/*calculate the variance whichi is the square of the standard deviation, sigma_sq*/
sigma_sq=sum/(i);
/*take the square root of the variance to calculate the standard deviation, sigma*/
/* sqrt(a) is the square root function using the math.h library */
sigma=sqrt(sigma_sq);
printf("\nVariance sigma_sq = %f\n\n",sigma_sq);
printf("\nStandard Deviation sigma = %f\n\n",sigma);
/******************************************************************/
/* to compile, */
/* gcc avg_std3.c -lm */
/* */
/* the -lm links the math.h library to the program avg_std3.c */
/* during compile. */
/* */
/* the program can be compiled in the 'me' directory */
/* */
/* To execute, ./a.out */
/* */
/* To name an executable named avg_std3: */
/* gcc avg_std3.c -o avg_std3 -lm */
/******************************************************************/
return(0);
}
/* avg_std3.c */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Require math.h to perform square-root and square calculations */
int
main(
){
int i,j;
int x[10];
double sum,avg,sigma,sigma_sq;
int *ptr;
sum = 0;
for(i=0;i<10;i++)
{
x[i]=i;
/* ptr is assigned the value of the x[i] element address */
ptr=&x[i];
/* notice for long printf() statements, close the " and open " on next line */
printf("\nx[%d] = %d, at address using pointer *ptr %X, "
"and using pointer ADDRESS operator &x[i] = %X\n",i,x[i],ptr,&x[i]);
/* taking a running average by using the variable, sum */
/* also using the pointer *ptr to 'point' to the value */
sum=sum+*ptr;
}
j=i;
printf("\nSum = %f\n", sum);
avg=sum/(i);
printf("\nAverage = %f\n", avg);
sum=0;
for (i=0;i<10;i++)
{
/* the pow(a,b) = a^b using the math.h library */
sum=sum+pow((x[i]-avg),2);
}
/*calculate the variance whichi is the square of the standard deviation, sigma_sq*/
sigma_sq=sum/(i);
/*take the square root of the variance to calculate the standard deviation, sigma*/
/* sqrt(a) is the square root function using the math.h library */
sigma=sqrt(sigma_sq);
printf("\nVariance sigma_sq = %f\n\n",sigma_sq);
printf("\nStandard Deviation sigma = %f\n\n",sigma);
/******************************************************************/
/* to compile, */
/* gcc avg_std3.c -lm */
/* */
/* the -lm links the math.h library to the program avg_std3.c */
/* during compile. */
/* */
/* the program can be compiled in the 'me' directory */
/* */
/* To execute, ./a.out */
/* */
/* To name an executable named avg_std3: */
/* gcc avg_std3.c -o avg_std3 -lm */
/******************************************************************/
return(0);
}
Program with Function
*Example of a program with functions*/
/*temperature conversion program*/
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
double CelToFahr(double temp_cel);
double FahrToCel(double fahr_temp);
int main()
{
int answer_1, dummy;
double celsius, fahr;
answer_1=TRUE;
while(answer_1==TRUE) {
printf("\nInput degrees Fahrenheit for conversion to Celsius(^z to exit): \n");
scanf("%lf",&fahr);
celsius=FahrToCel(fahr);
printf("\n%lf Celsius.\n\n",celsius);
printf("\nAnother F to C conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\nDo you want to input degrees in Celsius for conversion? (yes=1)\n");
scanf("%d",&answer_1);
while(answer_1==TRUE) {
printf("\n\nInput degrees Celsius for conversion to Fahrenheit (^z to exit): \n");
scanf("\n%lf",&celsius);
fahr=CelToFahr(celsius);
printf("\n%lf Fahrenheit.\n\n", fahr);
}
printf("End of program - enter any number to exit.\n");
scanf("%d",&dummy);
}
double FahrToCel(double fahr_temp)
/* Function to convert Fahrenheit to Celsius */
{
double cel_temp;
cel_temp=(5.0/9.0)*(fahr_temp-32.0);
return(cel_temp);
}
double CelToFahr(double temp_cel)
/* Function to convert Celsius to Fahrenheit */
{
double temp_fahr;
temp_fahr=(9.0/5.0)*temp_cel+32.0;;
return(temp_fahr);
}
/*temperature conversion program*/
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
double CelToFahr(double temp_cel);
double FahrToCel(double fahr_temp);
int main()
{
int answer_1, dummy;
double celsius, fahr;
answer_1=TRUE;
while(answer_1==TRUE) {
printf("\nInput degrees Fahrenheit for conversion to Celsius(^z to exit): \n");
scanf("%lf",&fahr);
celsius=FahrToCel(fahr);
printf("\n%lf Celsius.\n\n",celsius);
printf("\nAnother F to C conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\nDo you want to input degrees in Celsius for conversion? (yes=1)\n");
scanf("%d",&answer_1);
while(answer_1==TRUE) {
printf("\n\nInput degrees Celsius for conversion to Fahrenheit (^z to exit): \n");
scanf("\n%lf",&celsius);
fahr=CelToFahr(celsius);
printf("\n%lf Fahrenheit.\n\n", fahr);
}
printf("End of program - enter any number to exit.\n");
scanf("%d",&dummy);
}
double FahrToCel(double fahr_temp)
/* Function to convert Fahrenheit to Celsius */
{
double cel_temp;
cel_temp=(5.0/9.0)*(fahr_temp-32.0);
return(cel_temp);
}
double CelToFahr(double temp_cel)
/* Function to convert Celsius to Fahrenheit */
{
double temp_fahr;
temp_fahr=(9.0/5.0)*temp_cel+32.0;;
return(temp_fahr);
}
Program without Function
/*Example of a program without functions*/
/*temperature conversion program*/
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
int main()
{
int answer_1;
double celsius;
double fahr;
answer_1=TRUE;
while(answer_1==TRUE) {
printf("\nInput degrees Fahrenheit for conversion to Celsius (^z to exit): \n");
scanf("%lf",&fahr);
celsius=((fahr-32.0)*5.)/9.;
printf("\n%lf Celcius\n\n", celsius);
printf("\nAnother F to C conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\nDo you want to input degrees in Celsius for conversion? (yes=1)\n");
scanf("%d", &answer_1);
while(answer_1==TRUE) {
printf("\n\nInput degrees Celsius for conversion to Fahrenheit (^z to exit): \n");
scanf("%lf",&celsius);
fahr=(9.0/5.0)*celsius+32.0;
printf("\n\n%lf Fahrenheit. \n\n",fahr);
printf("\nAnother C to F conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\n\nEnd of program \n");
return (0);
}
/*temperature conversion program*/
#include <stdio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
int main()
{
int answer_1;
double celsius;
double fahr;
answer_1=TRUE;
while(answer_1==TRUE) {
printf("\nInput degrees Fahrenheit for conversion to Celsius (^z to exit): \n");
scanf("%lf",&fahr);
celsius=((fahr-32.0)*5.)/9.;
printf("\n%lf Celcius\n\n", celsius);
printf("\nAnother F to C conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\nDo you want to input degrees in Celsius for conversion? (yes=1)\n");
scanf("%d", &answer_1);
while(answer_1==TRUE) {
printf("\n\nInput degrees Celsius for conversion to Fahrenheit (^z to exit): \n");
scanf("%lf",&celsius);
fahr=(9.0/5.0)*celsius+32.0;
printf("\n\n%lf Fahrenheit. \n\n",fahr);
printf("\nAnother C to F conversion? (0 to exit conversion): \n");
scanf("%d", &answer_1);
}
printf("\n\nEnd of program \n");
return (0);
}
Inputting integers, characters, and strings
#include <stdio.h>
#include <string.h>
void main()
{
int a, b;
float c, d;
double e, f;
char g,h;
char hello[10], cousin[10];
a=10; b=12; c=10; d=12; e=10; f=12;
g='H'; h='C';
strcpy(hello, "Howzit");printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
strcpy(cousin, "Cuz");
printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
printf("\ne=%f, f=%f\n",e,f);
printf("\ng=%c, h=%c\n\n", g,h);
printf("\nHello=%s, Cousin=%s\n\n",hello,cousin);
printf("a= ");
scanf("%d",&a);
printf("\nb= ");
scanf("%d", &b);
printf("\n\n");printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
printf("\na=%d \tb=%d \tc=%f \td=%f \n", a,b,c,d);
printf("Input String hello: ");
scanf("%s",&hello);
printf("Input String cousin: ");
scanf("%s", &cousin);
/* scanf("Input Hello String=%s and Input Cousin String=%s", hello, cousin); */
/* scanf("Input Hello String=%s and Input Cousin String=%s", hello, cousin); */
printf("\n\nHello = %s \n",hello);
printf("\nCousin = %s \n",cousin);
printf("\n\n%s %s\n\n",hello,cousin);
puts(hello);
puts(cousin);
}
#include <string.h>
void main()
{
int a, b;
float c, d;
double e, f;
char g,h;
char hello[10], cousin[10];
a=10; b=12; c=10; d=12; e=10; f=12;
g='H'; h='C';
strcpy(hello, "Howzit");printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
strcpy(cousin, "Cuz");
printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
printf("\ne=%f, f=%f\n",e,f);
printf("\ng=%c, h=%c\n\n", g,h);
printf("\nHello=%s, Cousin=%s\n\n",hello,cousin);
printf("a= ");
scanf("%d",&a);
printf("\nb= ");
scanf("%d", &b);
printf("\n\n");printf("\na=%d, b=%d, c=%f, d=%f \n", a,b,c,d);
printf("\na=%d \tb=%d \tc=%f \td=%f \n", a,b,c,d);
printf("Input String hello: ");
scanf("%s",&hello);
printf("Input String cousin: ");
scanf("%s", &cousin);
/* scanf("Input Hello String=%s and Input Cousin String=%s", hello, cousin); */
/* scanf("Input Hello String=%s and Input Cousin String=%s", hello, cousin); */
printf("\n\nHello = %s \n",hello);
printf("\nCousin = %s \n",cousin);
printf("\n\n%s %s\n\n",hello,cousin);
puts(hello);
puts(cousin);
}
Subscribe to:
Posts (Atom)