Thinking Like an Engineer 3rd edition ICA 2017 You are writi
Thinking Like an Engineer 3rd edition
ICA 20-17
You are writing the code to control a chemical reaction. Sensors are used to place values into the variables Temp (temperature in degrees Celsius ), Pres (pressure in atmospheres), pH (pH of solution), and Time (elapsed time in minutes since start of reaction). A function called Update reads the sensors and modifies the contents of the variables to indicate the current conditions.
To use the Update function, you simply include the line
[Temp,Pres,pH,Time]= Update;
in your code as needed.
Write a program that will use the Update function once every minute to read new val- ues from the sensors, and continue doing so as long as the pH has decreased by at least 0.02 since the last reading. (Hint: Remember the pause function – pause(n) pauses execution for n seconds.)
If at any time the pressure exceeds 5 atmospheres or the temperature exceeds 200 degrees Celsius, a message should be generated indicating which parameter is beyond the safe limits and the parameter value. In this case, the program should call the function terminate which will shut down the reaction and the program stops after printing a message to the screen such as
Reaction terminated after X minutes. Final pH is x.xxx.
where X and x.xxx are replaced with the elapsed time and the final pH.
If at any point, the pressure exceeds 4 atmospheres or the temperature exceeds 175 degrees Celsius, an appropriate warning should be generated stating which parameter is too high and what its value is, but the reaction should continue and the program keep running.
When the pH has decreased by less than 0.02 from the previous reading, the reaction should be terminated, a message printed giving elapsed time and final pH as above, and the program should end.
You may assume that the initial pH is 8.0, the initial temperature is 30 degrees Celsius the initial pressure is 1 atmospheres, and the initial time is 0.
Solution
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
float ph=1000.0;
void Update(int temp_sensor,int pres_sensor,float ph_sensor,int time_sensor)
{
if(ph-ph_sensor < 0.02)
{
printf(\"Reaction terminated after %d minutes. Final pH is %f.\",time_sensor/60,ph_sensor);
exit(0);
}
if(pres_sensor > 5)
{
printf(\"Reaction terminated after %d minutes. Final pressure is %d.\",time_sensor/60,pres_sensor);
exit(0);
}
if(temp_sensor > 200)
{
printf(\"Reaction terminated after %d minutes. Final Temperature is %d.\",time_sensor/60,temp_sensor);
exit(0);
}
if(pres_sensor > 4)
{
printf(\"Warning: Pressure is Very high %d Atmosphere.\",pres_sensor);
}
if(temp_sensor > 175)
{
printf(\"Warning: Temperature is Very high %d Celcius.\",temp_sensor);
}
ph=ph_sensor;
}
int main() {
int pres_sensor=1,temp_sensor=30,time_sensor=0;
float ph_sensor=8.0;
while(1)
{
Update(temp_sensor,pres_sensor,ph_sensor,time_sensor);
sleep(60);
temp_sensor = rand() % 1000;
pres_sensor = rand() % 100;
ph_sensor = ((float)rand()/(float)(RAND_MAX))* 8.0;
time_sensor = time_sensor + 60;
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

