Question

In: Computer Science

Objectives: Practice using selection structures within a complete C++ program to solve a problem. Be able...

Objectives:

  • Practice using selection structures within a complete C++ program to solve a problem.
  • Be able to understand and use linear interpolation in a program.
  • Modify/Extend an existing program.

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.

  • If the samples are correctly ordered, your program should then ask the user to enter a salinity value.
  • If the salinity value lies within one of the two input intervals, the program should use

linear interpolation to determine the corresponding temperature.

  • If the input value is not within either of the input intervals, then the program should print a message so indicating and exit.

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

Solutions

Expert Solution

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;-


Related Solutions

Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
C++ Plan and code a program utilizing one or more repetition structures to solve the following...
C++ Plan and code a program utilizing one or more repetition structures to solve the following problem: Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. A number is a multiple of 9 if the sum of its digits is evenly divisible by 9. Determine if a number is a multiple of 9 by adding together the individual digits of the number and determining if the...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
Write a complete and syntactically correct Python program to solve the following problem: You are the...
Write a complete and syntactically correct Python program to solve the following problem: You are the payroll manager for SoftwarePirates Inc. You have been charged with writing a package that calculates the monthly paycheck for the salespeople. Salespeople at SoftwarePirates get paid a base salary of $2000 per month. Beyond the base salary, each salesperson earns commission on the following scale: Sales Commission Rate Bonus <$10000 0% 0 $10000 – $100,000 2% 0 $100,001 - $500,000 15% $1000 $500,001 -...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
c# only please Objectives Use the more advanced data structures introduced to accomplish a difficult problem....
c# only please Objectives Use the more advanced data structures introduced to accomplish a difficult problem. Tasks This assignment has two parts: Read from a file. Write to a file. Task 1 – File Input The first piece of information we need is a text file’s name and location. Ask the user through the console what the file’s name and location are. After that, please ensure that the file does exist and at the given location. If the file does...
(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
Write a complete Java Program to solve the following problem. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT