In: Computer Science
Objectives:
Problem Description: Linear Interpolation
There are two credit levels for this assignment. Completing the “B” level correctly is worth 16/20 points, completing the “A” level is worth 20/20. You are encouraged to get the code for the “B” level working first and then complete the “A” level if you have time/interest. You should include a comment at the top of your source code that indicates what level you are attempting.
Section 3.5 & 3.6 discuss the application of linear interpolation to determining the freezing point of seawater (based on salinity). In this assignment you will solve a more generalized version of the same problem.
Step 1: Ask the user for a temperature value. If the temperature is within the input temperature range, use linear interpolation to determine and display the corresponding salinity. Otherwise, print a suitable error message.
B-Level
Modification 1: Require the user to enter data values for 3 data samples (two interval ranges). A single sample is a salinity measure and its corresponding temperature. Your program should verify that the salinity values of the three samples are in increasing order and that the temperature values of the three
samples are in decreasing order; if not your program should simply print a message that the data are
invalid and then exit.
linear interpolation to determine the corresponding temperature.
Modification 2: Your program should ask the user what temperature scale to use for your input data (Celsius or Fahrenheit); your program should print computed temperatures in both Celsius and Fahrenheit (your program will need to convert between scales).
Two intervals
Two intervals
input
input
Program
Note: Screenshot is provided for indentation of code with input and output.
Modification 1:-
#include<iostream>
using namespace std;
int main()
{
float sal1,sal2,sal3;//salinity values
float temp1,temp2,temp3;//temperature value
cin>>sal1>>temp1;// user first value for
salinity measure and corresponding temperature
cin>>sal2>>temp2;// user second value for
salinity measure and corresponding temperature
cin>>sal3>>temp3;// user third value for
salinity measure and corresponding temperature
/* checking if salinity values are in increasing
order or not
and if corresponding values of temperature are in
decreasing order or not*/
if(((sal1<sal2)&&(sal2<sal3))&&((temp1>temp2)&&(temp2>temp3)))
{
float sal,temp;//saline value to
check
cin>>sal;
//checking if saline value is
present in first interval range or second interval range
if(sal>sal1&&sal<sal2)
{
temp = temp1 +
((temp2-temp1)/(sal2-sal1)) * (sal - sal1);
cout<<temp<<"\n";
}
else
if(sal>sal2&&sal<sal3)
{
temp = temp2 +
((temp3-temp2)/(sal3-sal2)) * (sal - sal2);
cout<<temp<<"\n";
}
else
{
cout<<"wrong saline value -- does not lies in the
intervals\n";
}
}
else
{
cout<<"Data are
invalid";
return;
}
}
code:-
Input:
Output:-
Modification 2:-
#include<iostream>
using namespace std;
int main()
{
float sal1, sal2, sal3; //salinity values
float temp1, temp2, temp3; //temperature value
cout << "What scale to use for temperature\n For
fahrenheit type 1 and For celsius type 2";
int x;
cin >> x; //variable for fahrenheit scale or
celsius scale
cin >> sal1 >> temp1; // user first value
for salinity measure and corresponding temperature
cin >> sal2 >> temp2; // user second value
for salinity measure and corresponding temperature
cin >> sal3 >> temp3; // user third value
for salinity measure and corresponding temperature
/* checking if salinity values are in increasing
order or not
and if corresponding values of temperature are in
decreasing order or not*/
if (((sal1 < sal2) && (sal2 < sal3))
&& ((temp1 > temp2) && (temp2 >
temp3)))
{
float sal, temp; //saline value to
check
cin >> sal;
//checking if saline value is
present in first interval range or second interval range
if (sal > sal1 && sal
< sal2)
{
temp = temp1 +
((temp2 - temp1) / (sal2 - sal1)) * (sal - sal1);
if (x ==
1)
{
cout << "Fahrenheit temperature is "
<< temp << "\n";
//converting fahreinheit to celsius
float c;
c = (5.0 / 9.0) * (temp - 32);
cout << "celsius temperature is " <<
c << "\n";
}
else if (x ==
2)
{
cout << "celsius temperature is " <<
temp << "\n";
float f;
f = (9.0 * temp / 5.0) + 32;
cout << "Fahrenheit temperature is "
<< f << "\n";
}
}
else if (sal > sal2 &&
sal < sal3)
{
temp = temp2 +
((temp3 - temp2) / (sal3 - sal2)) * (sal - sal2);
if (x ==
1)
{
cout << "Fahrenheit temperature is "
<< temp << "\n";
//converting fahreinheit to celsius
float c;
c = (5.0 / 9.0) * (temp - 32);
cout << "celsius temperature is " <<
c << "\n";
}
else if (x ==
2)
{
cout << "celsius temperature is " <<
temp << "\n";
float f;
f = (9.0 * temp / 5.0) + 32;
cout << "Fahrenheit temperature is "
<< f << "\n";
}
}
else
{
cout <<
"wrong saline value -- does not lies in the intervals\n";
}
}
else
{
cout << "Data are
invalid";
return 0;
}
}
Code screenshot
INPUT:-
OUTPUT;-