Sample Solutions to first Assignment. EEN218 Thursday 20th September 2001

 

A reminder of the data-file format: each line contains exactly 28 characters. Here is a sample line with the key to the digits' meanings underneath:

199901011300+043055011017632
aaaabbccddeeffffggghhhiiijjj
a=year, b=month, c=day, d=hour, e=minute, f=temperature, g=humidity, h=wind speed, i=wind direction, j=pressure. I will show both the C (stdio) and C++ (iostream) ways of reading the file and interacting with the user:
 
This is the C/stdio way
 
#include <stdio.h>
#include <stdlib.h>
 
const int MAX = 9000;
 
int year[MAX], month[MAX], day[MAX], time[MAX], temp[MAX],
    hum[MAX], wspd[MAX], wdir[MAX], pres[MAX];
int nrecords=0;
 
void main(void)
{ FILE *infile=fopen("weather.dat", "r");
  if (infile==NULL)
  { printf("Could not read weather.dat\n");
    exit(1); }
  while (nrecords<MAX)
  { int n=fscanf(infile, "%4d%2d%2d%4d%4d%3d%3d%3d%3d", 
                 &year[nrecords], &month[nrecords], &day[nrecords],
                 &time[nrecords], &temp[nrecords], &hum[nrecords],                 
                 &wspd[nrecords], &wdir[nrecords], &pres[nrecords]);
    if (n!=9) break;
    nrecords+=1; }
  fclose(infile);
  printf("1 = average temperature throughout year\n");
  printf("2 = average temperature for a particular month\n");
  printf("3 = average temperature for a particular time of day\n");
  printf("4 = find windiest moment\n");
  printf("5 = full report on a particular moment\n");
  printf("6 = exit\n");
  while (1)
  { printf("option: ");
    int option;
    scanf("%d", &option);
    if (option==1) avg_temp_all_year();
    else if (option==2) avg_temp_all_month();
    else if (option==3) avg_temp_for_time();
    else if (option==4) find_windiest();
    else if (option==5) report_on_moment();
    else if (option==6) break;
    else printf("What are you talking about?\n"); } }

// These functions would of course be defined before main:
 
void avg_temp_all_year(void)
{ float total=0.0;
  for (int i=0; i<nrecords; i+=1)
    total+=temp[i];
  float avg=total/nrecords;
  printf("Average temperature over all records is %.1f degrees Fahrenheit\n", avg); }
 
void avg_temp_all_month(void)
{ printf("which month (number 1-12): ");
  int mth;
  scanf("%d", &mth);
  float total=0.0, count=0.0;
  for (int i=0; i<nrecords; i+=1)
    if (month[i]==mth)
    { total+=temp[i];
      count+=1.0; }
  float avg=total/count;
  if (count==0.0)
    printf("No records have month = %d\n", mth);
  else
    printf("Average temperature in month %d is %.1f degrees Fahrenheit\n", mth, avg); }
 
void avg_temp_for_time(void)
{ printf("what time (number 0000-2359): ");
  int tim;
  scanf("%d", &tim);
  float total=0.0, count=0.0;
  for (int i=0; i<nrecords; i+=1)
    if (time[i]==tim)
    { total+=temp[i];
      count+=1.0; }
  float avg=total/count;
  if (count==0.0)
    printf("No records have time = %d\n", tim);
  else
    printf("Average temperature at time %04d is %.1f degrees Fahrenheit\n", tim, avg); }
 
void find_windiest(void)
{ int windex=0, wmax=-999999;
  for (int i=0; i<nrecords; i+=1)
    if (wspd[i]>wmax)
    { wmax=wspd[i];
      windex=i; }
  printf("Windiest moment is at %04d on %04d/%02d/%02d, speed was %d mph\n",
          time[windex], year[windex], month[windex], day[windex], wmax); }
 
void report_on_moment(void)
{ int yr, mn, dy, tm;
  printf("enter year month date time: ");
  scanf("%d %d %d %d", &yr, &mn, &dy, &tm);
  int found=0;
  for (int i=0; i<nrecords; i+=1)
    if (year[i]==yr && month[i]==mn && day[i]==dy && time[i]==tm)
    { found=1;
      printf("on %04d/%02d/%02d at %04d:\n", yr, mn, dy, tm);
      printf("  temperature %+4d degrees fahrenheit\n", temp[i]);
      printf("     humidity %4d %%\n", hum[i]);
      printf("   wind speed %4d mph\n", wspd[i]);
      printf("  w direction %4d degrees East from North\n", wdir[i]); }
  if (found==0)
    printf("No matching record for %04d/%02d/%02d at %04d found\n", yr, mn, dy, tm); }