Question

In: Physics

Implement the following function: a. float temp(float t, char ch) – this function takes temperature ‘t’...

Implement the following function:
a. float temp(float t, char ch) – this function takes temperature ‘t’ and unit of
temperature ‘ch’ as parameter. The variable ‘ch’ will be either f or c depending on
whether the temperature in variable ‘t’ is in Fahrenheit or Celsius. The function
returns the converted temperature to the main method.
Call the above function in the main method. Initialize temperature and unit of
temperature from the user and pass them as parameter to the above function. The
converted temperature should be displayed in the main method.

Hi teacher can you please give me solution in c++ & python

Solutions

Expert Solution

The C++ code is as under


########################


#include <iostream>

using namespace std;

float temp(float t, char ch) {
if (ch == 'C' || ch == 'c') {
return t*1.8;
}
else if (ch == 'F'|| ch == 'f') {
return t/1.8;
}
else {
float t;
char ch;
cout<<"Please input temperature, only numbers";
cin>>t;
cout<<"Please input C for celcius and F for Fahrenheit";
cin>>ch;
cout<<temp(t,ch);
  
}
}

int main()
{   
float t;
char ch;
cout<<"Please input temperature, only numbers";
cin>>t;
cout<<"Please input C for celcius and F for Fahrenheit";
cin>>ch;
if(ch == 'F'|| 'f'){
cout<<temp(t,ch);
cout<<"Converted to Celcius";
}
else if(ch == 'C'|| 'c'){
cout<<temp(t,ch);
cout<<"Converted to F";
}
  

return 0;
}


##########################


the python code is as under

##################

def temp(t,ch):
if(ch == 'C' || ch == 'c'):
return t*1.8
else if(ch == 'F' || ch == 'f'):
return t/1.8
else:
return temp(input('Please input temperature, only numbers'), input('Please input C for celcius and F for Fahrenheit')

if __name__=="__main__":
t = input('Please input temperature, only numbers');
x = input('Please input C for celcius and F for Fahrenheit');
if(x=="C"||x=="c"):
print(temp(t,x ))
print("Converted to F")
else:
print(temp(t,x ))
print("Converted to C")
  


Related Solutions

Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes...
Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes a string as parameter and creates a Doubly Linked List of characters and returns the pointer to the first node. (ii) Define a function int IsPalindrome(Nodeptr first)to check whether the string represented by the above doubly linked list pointed to by first, is a palindrome or not and return 1/0 accordingly. Do not use any additional data structure.Write a main function to read a...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char...
1.) Answer the following programs a.) Let f be the following function: int f(char *s, char *t) {     char *p1, *p2;     for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++){               if (*p1 == *p2) break;     }     return p1 -s; } What is the return value of f(“accd”, “dacd”)? b.) What will be the output of the below program? #include <stdio.h> int main()   {    char x[]="ProgramDesign", y[]="ProgramDesign";   if(x==y){   printf("Strings are...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
The density of an oil mixture (mix) as a function of the temperature (T) and the...
The density of an oil mixture (mix) as a function of the temperature (T) and the mass fraction of the three components (mi) was measured and results are shown : T (K) m1 m2 m3 Pmix (kg/m3 ) 300 0 1 0 879.6 320 0 0.5 0.5 870.6 340 0 0 1 863.6 360 0.5 0 0.5 846.4 380 0.5 0.25 0.25 830.8 400 0.5 0.5 0 819.1 420 1 0 0 796 440 1 0 0 778.2 Find the...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] );...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] ); Argument pt[2] defines a point on the plane: pt[0] is the x-coordinate, pt[1] is the y-coordinate. Argument rect[4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle. rect[2] and rect[3] define the opposite corner. Coordinates may be any valid floating point value, including negative values. The function returns int 0...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT