/* - */
/* This program stores fingerprint information in a structure. */
/* It then references a function to compute the overall category.*/
#include <stdio.h>
#include <stdlib.h>
/* Define a structure for the fingerprint information. */
/* The order for fingertips is right hand, thumb to pinky, */
/* and left hand, thumb to pinky. The codes are L for loops, */
/* W for whorls, and A for arches. */
struct fingerprint
{
int ID_number;
double overall_category;
char fingertip[10];
};
int main(void)
{
/* Declare and initialize variables. */
struct fingerprint new_print;
double compute_category(struct fingerprint f);
/* Specify information for the new fingerprint. */
new_print.ID_number = 2491009;
new_print.overall_category = 0;
new_print.fingertip[0] = 'W';
new_print.fingertip[1] = 'L';
new_print.fingertip[2] = 'L';
new_print.fingertip[3] = 'W';
new_print.fingertip[4] = 'A';
new_print.fingertip[5] = 'L';
new_print.fingertip[6] = 'L';
new_print.fingertip[7] = 'W';
new_print.fingertip[8] = 'A';
new_print.fingertip[9] = 'L';
/* Reference function to compute overall category. */
new_print.overall_category = compute_category(new_print);
/* Print overall category computed by the function. */
printf("Fingerprint Analysis for ID: %i \n",
new_print.ID_number);
printf("Overall Category: %.2f \n",new_print.overall_category);
/* Exit program. */
return 0;
}
/*--------------------------------------------------------------*/
/* This function computes the overall category */
/* for a fingerprint. */
double compute_category(struct fingerprint f)
{
/* Declare and initialize variables. */
double Rt=0, Ri=0, Rm=0, Rr=0, Rp=0, Lt=0, Li=0, Lm=0, Lr=0,
Lp=0, num, den;
int count = 0;
/* Set values based on whorls. */
if (f.fingertip[0] == 'W')
{
Rt = 16;
}
if (f.fingertip[1] == 'W')
{
Ri = 16;
}
if (f.fingertip[2] == 'W')
{
Rm = 8;
}
if (f.fingertip[3] == 'W')
{
Rr = 8;
}
if (f.fingertip[4] == 'W')
{
Rp = 4;
}
if (f.fingertip[5] == 'W')
{
Lt = 4;
}
if (f.fingertip[6] == 'W')
{
Li = 2;
}
if (f.fingertip[7] == 'W')
{
Lm = 2;
}
if (f.fingertip[8] == 'W')
{
Lr = 0;
}
if (f.fingertip[9] == 'W')
{
Lp = 0;
}
while (f.fingertip != 0)
{
printf("Numbers of whorls: %d", count);
f.fingertip /= 10;
count++;
}
/* Compute the numerator and denominator for overall category. */
num = Ri + Rr + Lt + Lm + Lp + 1;
den = Rt + Rm + Rp + Li + Lr + 1;
return num/den;
}
ENGR6 W.Hwee
Thursday, November 8, 2018
Tuesday, November 6, 2018
day 21 lab
/*??????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define FILENAME "longstring.txt"
int main(void)
{
FILE *in_file = fopen(FILENAME, "r"); //read the textfile
/* Declare and initialize variables. */
int count=0;
char short_str[3],
c = fgetc(in_file);
/* Print Short String. */
printf("Enter the short_string\n");
scanf("%s", short_str);
if (in_file == NULL)//open file
{
printf("cannot open file \n");
}
printf("Your shortstring: %s\n", short_str);
/*Read data from file*/
c = fgetc(in_file);
while(c > 0)
{
printf ("%c", c);
c = fgetc(in_file);
}
fclose(in_file);
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
/* This program initializes a long character string and a short */
/* character string. It then prints the locations of the short */
/* string in the long string. It also prints the number of */
/* occurrences of the short string in the long string. */
#include <stdio.h>
#include <string.h>
#define FILENAME "longstring.txt"
int main(void)
{
FILE *in_file = fopen(FILENAME, "r"); //read the textfile
/* Declare and initialize variables. */
int count=0;
char short_str[3],
c = fgetc(in_file);
/* Print Short String. */
printf("Enter the short_string\n");
scanf("%s", short_str);
if (in_file == NULL)//open file
{
printf("cannot open file \n");
}
printf("Your shortstring: %s\n", short_str);
/*Read data from file*/
c = fgetc(in_file);
while(c > 0)
{
printf ("%c", c);
c = fgetc(in_file);
}
fclose(in_file);
/* Exit program. */
return 0;
}
/*????????????????????????????????????????????????????????????-*/
Thursday, November 1, 2018
Monday, October 15, 2018
day 14 lab
//LCD with I2C
#include <Wire.h>
//Include the library code:
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 72; //1001000 written as decimal number
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//Set up the LCDs number of columns and rows:
lcd.begin(16, 2);
//Create a Wire object
Wire.begin();
}
void loop() {
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
int buttonval = analogRead(A0);// let buttons read as variable
int f = round(c*9.0/5.0 +32.0);//convert to farenheit
// read the input on analog pin 0:
Serial.println(buttonval);
delay(1);
Serial.println(c);
delay(500); // delay in between reads
Serial.println(f);
delay(500); // delay in between reads
if ( buttonval == 481)
{
switch(c){
case 23:{
lcd.setCursor(0,1);
lcd.print("23C");
break;
}
case 24:{
lcd.setCursor(0,1);
lcd.print("24C");
break;
}
case 25:{
lcd.setCursor(0,1);
lcd.print("25C ");
break;
}
case 26:{
lcd.setCursor(0,1);
lcd.print("26C");
break;
}
}
}
else if ( buttonval == 0){
switch(f){
case 73:{
lcd.setCursor(0,1);
lcd.print("73F ");
break;
}
case 75:{
lcd.setCursor(0,1);
lcd.print("75F ");
break;
}
case 77:{
lcd.setCursor(0,1);
lcd.print("77F ");
break;
}
case 79:{
lcd.setCursor(0,1);
lcd.print("79F ");
break;
}
}
}
return;
}
#include <Wire.h>
//Include the library code:
#include <LiquidCrystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int temp_address = 72; //1001000 written as decimal number
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//Set up the LCDs number of columns and rows:
lcd.begin(16, 2);
//Create a Wire object
Wire.begin();
}
void loop() {
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
int buttonval = analogRead(A0);// let buttons read as variable
int f = round(c*9.0/5.0 +32.0);//convert to farenheit
// read the input on analog pin 0:
Serial.println(buttonval);
delay(1);
Serial.println(c);
delay(500); // delay in between reads
Serial.println(f);
delay(500); // delay in between reads
if ( buttonval == 481)
{
switch(c){
case 23:{
lcd.setCursor(0,1);
lcd.print("23C");
break;
}
case 24:{
lcd.setCursor(0,1);
lcd.print("24C");
break;
}
case 25:{
lcd.setCursor(0,1);
lcd.print("25C ");
break;
}
case 26:{
lcd.setCursor(0,1);
lcd.print("26C");
break;
}
}
}
else if ( buttonval == 0){
switch(f){
case 73:{
lcd.setCursor(0,1);
lcd.print("73F ");
break;
}
case 75:{
lcd.setCursor(0,1);
lcd.print("75F ");
break;
}
case 77:{
lcd.setCursor(0,1);
lcd.print("77F ");
break;
}
case 79:{
lcd.setCursor(0,1);
lcd.print("79F ");
break;
}
}
}
return;
}
day 14 assignment
/* This program demonstrates the relationship between /
/* variables and addresses. */
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
int a=1, b=2;
/*Print the contents and addresses of a and b. */
printf("a = %d; address of a = %u \n",a,&a);
printf("b = %d; address of b = %u \n",b,&b);
/* Exit program.*/
return 0;
}
/* variables and addresses. */
#include <stdio.h>
int main(void)
{
/* Declare and initialize variables. */
int a=1, b=2;
/*Print the contents and addresses of a and b. */
printf("a = %d; address of a = %u \n",a,&a);
printf("b = %d; address of b = %u \n",b,&b);
/* Exit program.*/
return 0;
}
Thursday, October 11, 2018
day 13 assignment
/*?????????????????????????????????????????????????????????????*/
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
int numpeaks=0;
int numvalleys=0;
double elevation[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
/* This program determines the locations of peaks in an */
/* grid of elevation data. */
#include <stdio.h>
#define N 25
#define FILENAME "grid1.txt"
int main(void)
{
/* Declare variables. */
int nrows, ncols, i, j;
int numpeaks=0;
int numvalleys=0;
double elevation[N][N];
FILE *grid;
/* Read information from a data file. */
grid = fopen(FILENAME,"r");
if (grid == NULL)
printf("Error opening input file\n");
else
{
fscanf(grid,"%d %d",&nrows,&ncols);
for (i=0; i<=nrows-1; i++)
for (j=0; j<=ncols-1; j++)
fscanf(grid,"%lf",&elevation[i][j]);
/* Determine and print peak locations. */
printf("Top left point defined as row 0, column 0 \n");
for (i=1; i<=nrows-2; i++)
for (j=1; j<=ncols-2; j++)
if ((elevation[i-1][j]<elevation[i][j]) &&
(elevation[i+1][j]<elevation[i][j]) &&
(elevation[i][j-1]<elevation[i][j]) &&
(elevation[i][j+1]<elevation[i][j])){
numpeaks = numpeaks +1;
numvalleys =
printf("Peak at row: %d column: %d \n",i,j);
}
printf("peak count is %i \n", numpeaks);
fclose(grid); /* Close file. */
}
return 0; /* Exit program. */
}
day 13 lab
//Reads Temp from I2C temperature sensor and prints it on the serialport
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
//Start serial communication at 9600 baud
Serial.begin(9600);
//Create a Wire object
Wire.begin();
}
void loop()
{
//Send a request
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
//Do some math to convert the Celsius to Fahrenheit
int f = round(c*9.0/5.0 +32.0);
//Send the temperature in degrees C and F to the serial monitor
Serial.println(c);
//Serial.print("C ");
//Serial.print(f);
//Serial.println("F");
delay(500);
switch ( c )
{
case 26:
{
tone(speaker, NOTE_DS1);
break;
}
case 25:
{
tone(speaker, NOTE_D1);
break;
}
case 24:
{
tone(speaker, NOTE_CS1);
break;
}
case 23:
{
tone(speaker, NOTE_C1);
break;
}
case 22:
{
tone(speaker, NOTE_B0);
break;
}
}
return;
}
//Include Wire I2C library
#include <Wire.h>
#include "pitches.h"
const int speaker=9;
int temp_address = 72; //1001000 written as decimal number
int notes[] = { NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1};
int value[] = { 22, 23, 24, 25, 26} ;
int d;
void setup()
{
//Start serial communication at 9600 baud
Serial.begin(9600);
//Create a Wire object
Wire.begin();
}
void loop()
{
//Send a request
//Start talking to the device at the specified address
Wire.beginTransmission(temp_address);
//Send a bit asking for register zero, the data register
Wire.write(0);
//Complete Transmission
Wire.endTransmission();
//Read the temperature from the device
//Request 1 Byte from the specified address
Wire.requestFrom(temp_address, 1);
//Wait for response
while(Wire.available() == 0);
//Get the temp and read it into a variable
int c = Wire.read();
c = map(c, 22, 32, 22, 32);
c = constrain(c, 22, 32);
//Do some math to convert the Celsius to Fahrenheit
int f = round(c*9.0/5.0 +32.0);
//Send the temperature in degrees C and F to the serial monitor
Serial.println(c);
//Serial.print("C ");
//Serial.print(f);
//Serial.println("F");
delay(500);
switch ( c )
{
case 26:
{
tone(speaker, NOTE_DS1);
break;
}
case 25:
{
tone(speaker, NOTE_D1);
break;
}
case 24:
{
tone(speaker, NOTE_CS1);
break;
}
case 23:
{
tone(speaker, NOTE_C1);
break;
}
case 22:
{
tone(speaker, NOTE_B0);
break;
}
}
return;
}
Subscribe to:
Comments (Atom)