1. a program will calculate and print the fahrenheit or celsius temperature of a
ID: 3864698 • Letter: 1
Question
1. a program will calculate and print the fahrenheit or celsius temperature of a given temperature reading:
Input: Temperature entered from keyboard and choice to either calculate the equivalent fahrenheit or celsius temperature.
processing: two values returning functions: CalcFahrenheit and CalcCelcius which will calculate the appropriate temperature and return that value to main().
output: temperature value in F or C.
Hint: F=1.8C+32
C=5.0/9.0*(F-32)
2. A program will calculate and print the average of three test scores.
Input in main():
score1 = getTestScore();
score2 = getTestScore();
score3 = getTestScore();
processing: calculate the average of the three scores by calling a value returning function from main() named: CalcAverage.
output: value obtained from CalcAverage and printed in main().
Explanation / Answer
main1.cpp
#include <iostream>
using namespace std;
float CalcCelcius (float);
float CalcFahrenheit (float);
int main ()
{
float degree,Ctemp,Ftemp;
cout << "Enter your temperature reading in degrees : " << endl;
cin >> degree;
char scale;
cout << "Enter the letter of the temperature scale that will be used:" << endl;
cout << "(F = Fahrenheit; C = Celsius) : ";
cin >> scale;
cout<<endl;
if (scale == 'F')
{
Ftemp=degree;
Ctemp=CalcCelcius(degree);
}
else if (scale == 'C')
{
Ctemp=degree;
Ftemp=CalcFahrenheit(degree);
}
else
cout << "ERROR: Invalid temperature scale" << endl;
cout << "Your temperature reading converts as follows:" << endl;
cout << "Fahrenheit:" << Ftemp << endl;
cout << "Celsius:" << Ctemp << endl;
return 0;
}
//This function converts a Fahrenheit temperature to Celsius
float CalcCelcius (float Ftemp)
{
float Ctemp;
Ctemp = (Ftemp - 32) / 1.8;
return Ctemp;
}
//This function converts a Celsius temperature to Fahrenheit
float CalcFahrenheit (float Ctemp)
{
float Ftemp;
Ftemp = 1.8 * Ctemp + 32;
return Ftemp;
}
Output:-
main2.cpp
#include <iostream>
using namespace std;
float getTestScore();
float CalcAverage(float,float,float);
int main ()
{
float score1,score2,score3,avg;
score1 = getTestScore();
cout<<endl;
score2 = getTestScore();
cout<<endl;
score3 = getTestScore();
cout<<endl;
avg = CalcAverage(score1,score2,score3);
cout<<"Average score of given three score : "<<avg;
return 0;
}
float getTestScore ()
{
float C;
cout<<"Enter test score : ";
cin>>C;
return C;
}
float CalcAverage (float a, float b, float c)
{
float S,A;
S=a+b+c;
A=S/3;
return A;
}
Output:-
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.