ulvis.paste.net

Paste Search Dynamic
Recent pastes
meteorological data array
  1. // C code
  2. // This program will input and store meteorological data into an array.
  3. // Developer: Troy Scites CMIS102
  4. // Date: Feb 25, 2018
  5. #define NUMMONTHS 12
  6. #define NUMYEARS 5
  7. #include <stdio.h>
  8. // function prototypes
  9. void inputdata();
  10. void printdata();
  11. void printannual();
  12. // Global variables
  13. // These are available to all functions
  14. float Raindata[NUMYEARS][NUMMONTHS];
  15. float Winddata[NUMYEARS][NUMMONTHS];
  16. char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
  17. char months[NUMMONTHS][12] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  18. int main () {
  19.   char enterData = 'y';
  20.   printf("Do you want to input Precipatation and Wind data? (y for yes)\n");
  21.   scanf("%c",&enterData);
  22.   if (enterData == 'y') {
  23.     // Call Function to Input data
  24.     inputdata();
  25.  
  26.     // Call Function to display data
  27.     //printdata();
  28.     // Call Function to sum and print yearly rainfall
  29.     printannual();
  30.   }
  31.   else {
  32.     printf("No data was input at this time\n");
  33.   }
  34.   printf("Please try the Precipitation program again. \n");
  35.   return 0;
  36. }
  37.  
  38. // function to inputdata
  39. void inputdata() {
  40.   /* variable definition: */
  41.   float Rain=1.0;
  42.   float Wind=1.0;
  43.   // Input Data
  44.   for (int year=0;year < NUMYEARS; year++) {
  45.     for (int month=0; month< NUMMONTHS; month++) {
  46.       printf("Enter rain for %d, %d:\n", year+1, month+1);
  47.       scanf("%f",&Rain);
  48.       Raindata[year][month]=Rain;
  49.       printf("Enter wind for %d, %d:\n", year+1, month+1);
  50.       scanf("%f",&Wind);
  51.       Winddata[year][month]=Wind;
  52.     }
  53.   }
  54. }
  55. // Function to printdata
  56. void printdata(){
  57.   // Print data
  58.   printf ("year\t month\t rain\n");
  59.   for (int year=0;year < NUMYEARS; year++) {
  60.     for (int month=0; month< NUMMONTHS; month++) {
  61.       printf("%s\t %s\t %5.2f\n",
  62.       years[year],months[month],Raindata[year][month]);
  63.     }
  64.   }
  65. }
  66. // Function to print sum of annual rainfall
  67. void printannual() {
  68.     // Declare and initialize variables
  69.     int year, month;
  70.     float total_rain, total_wind;
  71.     // Loop over years and sum monthly data
  72.     for (year = 0; year < NUMYEARS; year++ ) {
  73.         total_rain = 0;  // RESET VARIABLE TO ZERO FOR EACH YEAR
  74.         total_wind = 0;  // RESET VARIABLE TO ZERO FOR EACH YEAR
  75.         for (month = 0; month < NUMMONTHS; month++) {
  76.             total_rain += Raindata[year][month];
  77.             total_wind += Winddata[year][month];
  78.         }
  79.         printf("For year %s, the total rainfall was %5.2f and windspeed was %5.2f\n", years[year], total_rain, total_wind);
  80.     }
  81. }
Parsed in 0.016 seconds