Question

In: Computer Science

Program IN C Prompt the user to enter month number and year. Retain an appropriate response...

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

.

Solutions

Expert Solution

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;
}


Related Solutions

IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C program Your program will prompt the user to enter a value for the...
Write a C program Your program will prompt the user to enter a value for the amount of expenses on the credit card. Retrieve the user input using fgets()/sscanf() and save the input value in a variable. The value should be read as type double. The user may or may not enter cents as part of the input. In other words, expect the user to enter values such as 500, 500.00 and 500.10. The program will then prompt the user...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream> and switch and if-else statements only. Thank you. Ex. Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four Enter a number: 100000 One Hundred Thousand Enter a number: -2 Number should be from 0-1000000 only
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT