HW 3
Modify:
1. Use the program to determine the freezing temperatures to go with the following salinity
measurements in ppt: 3 8.5 19 23.5 26.8 30.5
2. Modify the program so that it converts and prints the new temperature in degrees Centigrade. (Recall that TF = 9/5 TC + 32
3. Suppose that the data used with the program contained values with the degrees in Centigrade. Would the program need to be changed? Explain.
4. Modify the program so that it interpolates for a salinity, instead of a new freezing temperature. (You may want to refer to the graph below, which contains a plot of this data with the freezing temperature on the x-axis and the salinity on the y-axis.)
Temperatures for salinity:
3 ppt = 31.8
8.5 ppt = 31.3
19 ppt = 30.2
26.8 ppt = 29.4
Output in Celsius:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = ((f_a + (b-a)/(c-a)*(f_c - f_a))-32)*(0.5555);
/* Print new freezing temperature. */
printf("New freezing temperature in degrees C: %4.2f \n",f_b);
return 0; /* Exit program. */
}
The program would require that all Centigrade data be converted to Fahrenheit prior to calculation.
Interpolate salinity:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new freezing point: \n");
scanf("%lf",&f_b);
// Use linear interpolation to compute new freezing temperature.
b = (a + (f_b-f_a)/(f_c-f_a)*(c - a));
/* Print new freezing temperature. */
printf("New salinity in ppt: %4.2f \n",b);
return 0; /* Exit program. */
}
Jet engine program:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, v, a;
printf("Input time in seconds: ");
scanf("%lf", &t);
v = 0.00001*pow(t,3)-0.00488*pow(t,2)+0.75795*t+181.3566;
a = 3-0.000062*pow(v,2);
printf("At %0.3lf seconds, velocity is %0.3lf m/s and acceleration is %0.5lf m/s^2", t, v, a);
return 0;
}
Jet engine minute input:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, t_s, v, a;
printf("Input time in minutes: ");
scanf("%lf", &t);
t_s = t*60;
v = 0.00001*pow(t_s,3)-0.00488*pow(t_s,2)+0.75795*t_s+181.3566;
a = 3-0.000062*pow(v,2);
printf("At %0.3lf minutes, velocity is %0.3lf m/s and acceleration is %0.5lf m/s^2", t, v, a);
return 0;
}
Jet engine feet output:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, v, v_ft, a;
printf("Input time in seconds: ");
scanf("%lf", &t);
v = 0.00001*pow(t,3)-0.00488*pow(t,2)+0.75795*t+181.3566;
v_ft= v * 3.281;
a = (3-0.000062*pow(v,2))*3.281;
printf("At %0.3lf seconds, velocity is %0.3lf ft/s and acceleration is %0.5lf ft/s^2", t, v_ft, a);
return 0;
}
Logarithm function:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, base, log_x_b;
again:
printf("Input 'x' value: ");
scanf("%lf", &x);
printf("Input base value: ");
scanf("%lf", &base);
if (x <= 0 || base <= 0)
{
printf("Invalid x or base value.");
goto again;
}
else
{
log_x_b = log(x)/log(base);
printf("The log of %.3lf to base %.3lf is %.3lf", x, base, log_x_b);
}
return 0;
}
Arduino:
const int LED1=9; //The LED is connected to pin 9
const int LED2=6;
const int BUTTON1=2; //The Button is connected to pin 2
const int BUTTON2=4;
boolean lastButton1 = LOW; //Variable containing the previous button state
boolean currentButton1 = LOW; //Variable containing the current button state
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean ledOn1 = false; //The present state of the LED (on/off)
boolean ledOn2 = false; //The present state of the LED (on/off)
void setup()
{
pinMode (LED1, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON1, INPUT); //Set button as input (not required)
pinMode(LED2, OUTPUT);
pinMode(BUTTON2, INPUT);
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce1(boolean last)
{
boolean current = digitalRead(BUTTON1); //Read the button state
if (last != current) //if it's different…
{
delay(5); //wait 5ms
current = digitalRead(BUTTON1); //read it again
}
return current; //return the current value
}
boolean debounce2(boolean last)
{
boolean current = digitalRead(BUTTON2); //Read the button state
if (last != current) //if it's different…
{
delay(5); //wait 5ms
current = digitalRead(BUTTON2); //read it again
}
return current; //return the current value
}
void loop()
{
currentButton1 = debounce1(lastButton1); //read debounced state
if (lastButton1 == LOW && currentButton1 == HIGH) //if it was pressed...
{
ledOn1 = !ledOn1; //toggle the LED value
}
lastButton1 = currentButton1; //reset button value
digitalWrite(LED1, ledOn1); //change the LED state
currentButton2 = debounce2(lastButton2); //read debounced state
if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
{
ledOn2 = !ledOn2; //toggle the LED value
}
lastButton2 = currentButton2; //reset button value
digitalWrite(LED2, ledOn2); //change the LED state
}
2. Modify the program so that it converts and prints the new temperature in degrees Centigrade. (Recall that TF = 9/5 TC + 32
3. Suppose that the data used with the program contained values with the degrees in Centigrade. Would the program need to be changed? Explain.
4. Modify the program so that it interpolates for a salinity, instead of a new freezing temperature. (You may want to refer to the graph below, which contains a plot of this data with the freezing temperature on the x-axis and the salinity on the y-axis.)
Temperatures for salinity:
3 ppt = 31.8
8.5 ppt = 31.3
19 ppt = 30.2
26.8 ppt = 29.4
Output in Celsius:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new salinity: \n");
scanf("%lf",&b);
// Use linear interpolation to compute new freezing temperature.
f_b = ((f_a + (b-a)/(c-a)*(f_c - f_a))-32)*(0.5555);
/* Print new freezing temperature. */
printf("New freezing temperature in degrees C: %4.2f \n",f_b);
return 0; /* Exit program. */
}
The program would require that all Centigrade data be converted to Fahrenheit prior to calculation.
Interpolate salinity:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* Declare variables. */
double a, f_a, b, f_b, c, f_c;
/* Get user input from the keyboard. */
printf("Use ppt for salinity values. \n");
printf("Use degrees F for temperatures. \n");
printf("Enter first salinity and freezing temperature: \n");
scanf("%lf %lf",&a,&f_a);
printf("Enter second salinity and freezing temperature: \n");
scanf("%lf %lf",&c,&f_c);
printf("Enter new freezing point: \n");
scanf("%lf",&f_b);
// Use linear interpolation to compute new freezing temperature.
b = (a + (f_b-f_a)/(f_c-f_a)*(c - a));
/* Print new freezing temperature. */
printf("New salinity in ppt: %4.2f \n",b);
return 0; /* Exit program. */
}
Jet engine program:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, v, a;
printf("Input time in seconds: ");
scanf("%lf", &t);
v = 0.00001*pow(t,3)-0.00488*pow(t,2)+0.75795*t+181.3566;
a = 3-0.000062*pow(v,2);
printf("At %0.3lf seconds, velocity is %0.3lf m/s and acceleration is %0.5lf m/s^2", t, v, a);
return 0;
}
Jet engine minute input:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, t_s, v, a;
printf("Input time in minutes: ");
scanf("%lf", &t);
t_s = t*60;
v = 0.00001*pow(t_s,3)-0.00488*pow(t_s,2)+0.75795*t_s+181.3566;
a = 3-0.000062*pow(v,2);
printf("At %0.3lf minutes, velocity is %0.3lf m/s and acceleration is %0.5lf m/s^2", t, v, a);
return 0;
}
Jet engine feet output:
#include <stdio.h>
#include <math.h>
int main(void)
{
double t, v, v_ft, a;
printf("Input time in seconds: ");
scanf("%lf", &t);
v = 0.00001*pow(t,3)-0.00488*pow(t,2)+0.75795*t+181.3566;
v_ft= v * 3.281;
a = (3-0.000062*pow(v,2))*3.281;
printf("At %0.3lf seconds, velocity is %0.3lf ft/s and acceleration is %0.5lf ft/s^2", t, v_ft, a);
return 0;
}
Logarithm function:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, base, log_x_b;
again:
printf("Input 'x' value: ");
scanf("%lf", &x);
printf("Input base value: ");
scanf("%lf", &base);
if (x <= 0 || base <= 0)
{
printf("Invalid x or base value.");
goto again;
}
else
{
log_x_b = log(x)/log(base);
printf("The log of %.3lf to base %.3lf is %.3lf", x, base, log_x_b);
}
return 0;
}
Arduino:
const int LED1=9; //The LED is connected to pin 9
const int LED2=6;
const int BUTTON1=2; //The Button is connected to pin 2
const int BUTTON2=4;
boolean lastButton1 = LOW; //Variable containing the previous button state
boolean currentButton1 = LOW; //Variable containing the current button state
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;
boolean ledOn1 = false; //The present state of the LED (on/off)
boolean ledOn2 = false; //The present state of the LED (on/off)
void setup()
{
pinMode (LED1, OUTPUT); //Set the LED pin as an output
pinMode (BUTTON1, INPUT); //Set button as input (not required)
pinMode(LED2, OUTPUT);
pinMode(BUTTON2, INPUT);
}
/*
* Debouncing Function Pass it the previous button state,
* and get back the current debounced button state.
*/
boolean debounce1(boolean last)
{
boolean current = digitalRead(BUTTON1); //Read the button state
if (last != current) //if it's different…
{
delay(5); //wait 5ms
current = digitalRead(BUTTON1); //read it again
}
return current; //return the current value
}
boolean debounce2(boolean last)
{
boolean current = digitalRead(BUTTON2); //Read the button state
if (last != current) //if it's different…
{
delay(5); //wait 5ms
current = digitalRead(BUTTON2); //read it again
}
return current; //return the current value
}
void loop()
{
currentButton1 = debounce1(lastButton1); //read debounced state
if (lastButton1 == LOW && currentButton1 == HIGH) //if it was pressed...
{
ledOn1 = !ledOn1; //toggle the LED value
}
lastButton1 = currentButton1; //reset button value
digitalWrite(LED1, ledOn1); //change the LED state
currentButton2 = debounce2(lastButton2); //read debounced state
if (lastButton2 == LOW && currentButton2 == HIGH) //if it was pressed...
{
ledOn2 = !ledOn2; //toggle the LED value
}
lastButton2 = currentButton2; //reset button value
digitalWrite(LED2, ledOn2); //change the LED state
}
Comments
Post a Comment