In: Computer Science
Program IN C
Prompt the user to enter month number and year. Retain an appropriate response for invalid month numbers, but don’t worry about validity of year.
2.Determine the number of days in the month entered (ignore leap years).
3.Use the answer to step 2 in one FOR-LOOP to ask the user to enter a FAHRENHEIT temperature (integer or floating) for each day of the month utilizing a prompt that includes the DATE.
a. Prompt the user to enter the Fahrenheit temperature for 2/1/2018... etc b.Continue to prompt the user for the Fahrenheit temperature for EACH day of the month entered until the loop reaches the last day of that month, e.g. 2/28/2018, last day of Feb.
4.When all daily temps have been entered, display month number, and average temperature in Fahrenheit and Celsius. (You know or can find the conversion equation.)
Display averages to nearest tenth of degree.
5.When TESTING your program and demonstrating it to your instruct or, let each month have 21 fewer days, 7, 9 or 10 instead of 28, 30 or 31.Appropriate comments and messages to user are required
.
The snapshot the required C program is as below:-
//Output
Attaching the text of the C code, in snapshot of the code above the last printf statement didn't came clearly, the ending part were cut, you can refer the below code for clarity:-
#include<stdio.h>
int main()
{
//variable declaration
int month_number=0;
int year=0;
int no_of_days=0;
int i=0;
float tempF[31]; //temperature array for storing
fahrenheit values
float sumTempF=0;//sum of fahrenheit temperature
float avg_Fahrenheit_temp=0;//average fahrenheit
temparature
float avg_Celsius_temp=0;//average celsius temparature
->calculated using conversion formula
//prompt user to enter month
while(1) //infinite loop to prompt user again in case
invalid month is entered
{
printf("Enter Month Number\n");
scanf("%d",&month_number);
if(month_number<1 || month_number>12) //month is
invalid
{
printf("Invalid Month!\n");
continue;
}
else
{
break;
}
}
//prompt user to enter year
printf("Enter Year\n");
scanf("%d",&year);
//Determine number of days in the month
switch(month_number)
{
case 1:
no_of_days=31;
//Jan
break;
case 2:
no_of_days=28;
//Feb
break;
case 3:
no_of_days=31;
//Mar
break;
case 4:
no_of_days=30;
//Apr
break;
case 5:
no_of_days=31;
//May
break;
case 6:
no_of_days=30;
//Jun
break;
case 7:
no_of_days=31;
//Jul
break;
case 8:
no_of_days=31;
//Aug
break;
case 9:
no_of_days=30;
//Sep
break;
case 10:
no_of_days=31;
//Oct
break;
case 11:
no_of_days=30;
//Nov
break;
case 12:
no_of_days=31;
//Dec
break;
}
//for loop for taking input of temperatures of days in
the given moth from user
//reducing days by 21 as asked in question
no_of_days=no_of_days-21; //you can comment this line
if you want to enter actual days of 28,30,31
for(i=1;i<=no_of_days;i++)
{
printf("Enter FAHRENHEIT
temperature for %d/%d/%d\n",month_number,i,year);
//as array starts from 0 we take
index as i-1 since i=1 initially, i-1=0, you can take separate
variable also
//but for reusability i have used i
itself
scanf("%f",&tempF[i-1]);
//calculating sum of fahrenheit
temperature
sumTempF=sumTempF+tempF[i-1];
}//for close
avg_Fahrenheit_temp=sumTempF/no_of_days;
//conversion formula fahrenheit to celsius
// c/5=(f-32)/9
avg_Celsius_temp=(5.0)*((avg_Fahrenheit_temp-32)/9.0);
//"%.1f" is used to round degrees upto tenth of a
degree i.e upto 1 decimal place
printf("Month Number = %d \t Average Temperature in
Fahrenheit = %.1f \t Average Temperature in Celsius =
%.1f",month_number,avg_Fahrenheit_temp,avg_Celsius_temp);
return 0;
}