5. Write a function called convert_temp. Here is the function\'s prototype: void
ID: 3814605 • 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
// 5. Complete the convert_temp function as described in the
// homework assignment write-up.
void convert_temp(int t1, char s1, int *t2, char *s2) {
// Write your code below
}
int main() {
int small_numbers[] = {3, 6, 2, 6, 1, 0, 6};
int big_numbers[ ] = {1717986912, 1717986912};
int x = 1, y = 2;
int the_sum;
int arr1[] = {3, 1, 2};
int arr2[3];
int t1 = 32;
char s1 = 'F';
int t2;
char s2;
int i;
the_sum = sum_array(small_numbers,7);
// should print 24
printf("This output should be 24 %d ", the_sum);
the_sum = sum_array(big_numbers, 2);
// should print a negative number
printf("This output should be negative %d ", the_sum);
// the next line should print 3
printf("This output should be 3 ");
call_add();
// this should print 3 1 2
copy_array(arr1, arr2, 3);
printf("This output should be 3 1 2 ");
for (i=0; i<3; i++)
printf("%d ", arr2[i]);
printf(" ");
// this should print 2 1 3
reverse_array(arr1, 3);
printf("This output should be 2 1 3 ");
for (i=0; i<3; i++)
printf("%d ", arr1[i]);
printf(" ");
// this should print 0 C
convert_temp(t1, s1, &t2, &s2);
printf("This output should be 0 C ");
printf("%d %c ", t2, s2);
}
Explanation / Answer
5.
#include <stdio.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 t1, char s1, int *t2, char *s2){
if(s1=='F')//input is in F
{
*t2=(5/9)*(t1-32);
*s2='C';
}else if(s1=='C'){
*t2 = (t1 * 9.0) / 5.0 + 32; // F = (C * 9.0) / 5.0 + 32;
*s2='F';
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.