Write a program that uses the main program to print the class heading, a tempera
ID: 3694421 • Letter: W
Question
Write a program that uses the main program to print the class heading, a temperature chart heading and call user defined functions. The program should use a symbolic constant of 7 to represent the 7 days of the week and use the symbolic constant throughout the program. Temperature will be show with 1 decimal place throughout the program. The first user defined function (UDF1) will use random number with a seed to generate randomly random numbers “Fahrenheit reading for the particular day # NN”. Use the floating point equation: ( (double) rand() / RAND_MAX ) * (end1 - start1) + start1; // found in (stdlib.h)to fill an array with decimal data. The program must increment to fill in the “NN” for the day#. The second user-defined function (UDF2) will convert the array of temperatures from Fahrenheit to Celsius. The third user-defined function (UDF3) will print the heading “…Equivalent Celsius Readings…” and print the array which is the output value of Celsius that was calculated by the 2nd user defined function: Again, the program must increment to fill in the “NN” for the day#. The forth user-defined function (UDF4) will sort the numbers from highest to lowest and print a heading and the sorted temperature values. UDF3 and UDF4 information will be printed to an output data / text file.
Explanation / Answer
Solution: See the code below
--------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#define DAYS_IN_WEEK 7
double farenheit_temperatures_in_week[DAYS_IN_WEEK];
double celsius_temperatures_in_week[DAYS_IN_WEEK];
//function to generate random temperature readings for days in a week
void UDF1()
{
int start1=1,end1=DAYS_IN_WEEK;
printf("…Fahrenheit Readings… ");
for(;start1<=end1;start1++)
{
double random_temperature=((double)rand()/RAND_MAX )*(end1 - start1) + start1;
printf("Fahrenheit reading for the day %d:%.1lf ",start1,random_temperature);
farenheit_temperatures_in_week[start1-1]=random_temperature;
}
}
//function to convert farenheit readings to celsius
void UDF2()
{
int start1=1,end1=DAYS_IN_WEEK;
double farenheit,celsius;
for(;start1<=end1;start1++)
{
farenheit=farenheit_temperatures_in_week[start1-1];
celsius=((farenheit-32.0)*5.0)/9.0;
//printf("Celsius reading for the day %d:%.1lf ",start1,celsius);
celsius_temperatures_in_week[start1-1]=celsius;
}
}
//function to print temperature readings to celsius
void UDF3()
{
int start1=1,end1=DAYS_IN_WEEK;
double celsius;
printf("…Equivalent Celsius Readings… ");
for(;start1<=end1;start1++)
{
celsius=celsius_temperatures_in_week[start1-1];
printf("Celsius reading for the day %d:%.1lf ",start1,celsius);
}
}
//function to sort the temperatures
void UDF4()
{
int i,j;
double temp;
//sort the temperatures from highest to lowest
for (i=0;i<(DAYS_IN_WEEK-1);i++)
{
for(j=0;j<DAYS_IN_WEEK-i-1;j++)
{
if(celsius_temperatures_in_week[j]>celsius_temperatures_in_week[j+1])
{
temp = celsius_temperatures_in_week[j];
celsius_temperatures_in_week[j]=celsius_temperatures_in_week[j+1];
celsius_temperatures_in_week[j+1] = temp;
}
}
}
printf("…Sorted temperatures in celsius… ");
for(i=0;i<DAYS_IN_WEEK;i++)
{
printf("Celsius reading for the day %d:%.1lf ",i+1,celsius_temperatures_in_week[i]);
}
}
//main function
int main(void) {
UDF1();
UDF2();
UDF3();
UDF4();
return EXIT_SUCCESS;
}
------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.