Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

5. Write a function called convert_temp. Here is the function\'s prototype: void

ID: 3814433 • Letter: 5

Question

5. Write a function called convert_temp. Here is the function's prototype:

void convert_temp(int, char, int *, char *);

In other words, convert_temp is passed 4 parameters: an integer, a character, a pointer to an integer, and a pointer to a character. The integers are temperatures, and the characters are scales. We will assume that a scale is either 'F' (Fahrenheit) or 'C' (Celsius). The function should convert a temperature from one scale to the the other. The "answer" is stored in the 3rd and 4th parameters. For example:

void convert_temp(int, char, int *, char *);

int main() {

int temp = 32;

char scale = 'F';

int new_temp;

char new_scale;

convert_temp(temp, scale, &new_temp, &new_scale);

printf("%d %c = %d %c ", temp, scale, new_temp, new_scale);

convert_temp(new_temp, new_scale, &temp, &scale);

printf("%d %c = %d %c ", new_temp, new_scale, temp, scale);

}

The above code (assuming proper completion of convert_temp) should produce the output 32 F = 0 C 0 C = 32 F

Explanation / Answer

// C code
#include <stdio.h>
#include <string.h>

void convert_temp(int, char, int *, char *);

int main()
{
int temp = 32;
char scale = 'F';
int new_temp;
char new_scale;
convert_temp(temp, scale, &new_temp, &new_scale);
printf("%d %c = %d %c ", temp, scale, new_temp, new_scale);
convert_temp(new_temp, new_scale, &temp, &scale);
printf("%d %c = %d %c ", new_temp, new_scale, temp, scale);
}

void convert_temp(int temp, char scale, int *new_temp, char *new_scale)
{
if(scale == 'F')
{
*new_scale = 'C';
*new_temp = 5*((temp-32)/9.0);
}
else if(scale == 'C')
{
*new_scale = 'F';
*new_scale = 9*(temp/5.0) + 32;
}
}


/*
output:

32 F = 0 C
0 C = 32

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote