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[] =...
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...
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.
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must...
Create a C module convertTo_csv.c that will implement the function loadAndConvert(const char* file) The code must be split into 2 files (.h file and a .c file). The convertTo_csv.c will have the function implementation in ti and the The convertTo_csv.h file will have the declaration. One argument will be given to the function, that is the name of the input file that needs to be converted. A function will create a new csv file called output.csv Know that the loadAndConvert...
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...
Q7: (Currency Conversion) Implement the following double functions: a) Function toAUD takes an amount in US...
Q7: (Currency Conversion) Implement the following double functions: a) Function toAUD takes an amount in US dollars and returns the AUD equivalent. b) Function toYuan takes an amount in US dollars and return the Yuan equivalent c) Use these functions to write a program that prints charts showing the AUD and Yuan equivalents of a range of dollar amounts. Print the outputs in a neat tabular format. Use an exchange rate of 1 USD = 1.42 AUD and 1 USD...
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x =...
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x = 30; int y = 4; double z = x/y; System.out.println(z);
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT