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

-Write a program in C++ that would ask the user for 10 (ten) different temperatu

ID: 3635146 • Letter: #

Question

-Write a program in C++ that would ask the user for 10 (ten) different temperatures in Fahrenheit and convert them to Celsius in a LOOP. Hint: C = 5/9 (F-32).
-Write a program in C++ that would function as a calculator with 4 main operations (+, -, *, /).
-Write a function that reverses the order of the values in an array. Assume that the function prototype statement is:
void v_rev(int x[], int n);
where x is a one-dimensional array with n elements.
-Write a program in C++ that would pick up the three coefficients of a second order polynomial from the input and output the roots of the equation

Explanation / Answer

Let's say that you would like to create a program that prints a Fahrenheit-to-Celsius conversion table. This is easily accomplished with a for loop or a while loop:

#include

int main()
{
int a;
a = 0;
while (a <= 100)
{
printf("%4d degrees F = %4d degrees C ",
a, (a - 32) * 5 / 9);
a = a + 10;
}
return 0;
}

If you run this program, it will produce a table of values starting at 0 degrees F and ending at 100 degrees F. The output will look like this:

0 degrees F = -17 degrees C
10 degrees F = -12 degrees C
20 degrees F = -6 degrees C
30 degrees F = -1 degrees C
40 degrees F = 4 degrees C
50 degrees F = 10 degrees C
60 degrees F = 15 degrees C
70 degrees F = 21 degrees C
80 degrees F = 26 degrees C
90 degrees F = 32 degrees C
100 degrees F = 37 degrees C


The table's values are in increments of 10 degrees. You can see that you can easily change the starting, ending or increment values of the table that the program produces.

If you wanted your values to be more accurate, you could use floating point values instead:

#include

int main()
{
float a;
a = 0;
while (a <= 100)
{
printf("%6.2f degrees F = %6.2f degrees C ",
a, (a - 32.0) * 5.0 / 9.0);
a = a + 10;
}
return 0;
}



You can see that the declaration for a has been changed to a float, and the %f symbol replaces the %d symbol in the printf statement. In addition, the %f symbol has some formatting applied to it: The value will be printed with six digits preceding the decimal point and two digits following the decimal point.

Now let's say that we wanted to modify the program so that the temperature 98.6 is inserted in the table at the proper position. That is, we want the table to increment every 10 degrees, but we also want the table to include an extra line for 98.6 degrees F because that is the normal body temperature for a human being. The following program accomplishes the goal:

#include

int main()
{
float a;
a = 0;
while (a <= 100)
{
if (a > 98.6)
{
printf("%6.2f degrees F = %6.2f degrees C ",
98.6, (98.6 - 32.0) * 5.0 / 9.0);
}
printf("%6.2f degrees F = %6.2f degrees C ",
a, (a - 32.0) * 5.0 / 9.0);
a = a + 10;
}
return 0;
}


This program works if the ending value is 100, but if you change the ending value to 200 you will find that the program has a bug. It prints the line for 98.6 degrees too many times. We can fix that problem in several different ways. Here is one way:

#include

int main()
{
float a, b;
a = 0;
b = -1;
while (a <= 100)
{
if ((a > 98.6) && (b < 98.6))
{
printf("%6.2f degrees F = %6.2f degrees C ",
98.6, (98.6 - 32.0) * 5.0 / 9.0);
}
printf("%6.2f degrees F = %6.2f degrees C ",
a, (a - 32.0) * 5.0 / 9.0);
b = a;
a = a + 10;
}
return 0;
}