idastan97

CSCI151 L26 P2

Nov 9th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /*
  2.  * prog.c
  3.  *
  4.  *  Created on: Nov 10, 2016
  5.  *      Author: dastan.iyembergen
  6.  */
  7.  
  8. #include <stdio.h>
  9.  
  10. int i;
  11.  
  12. typedef struct _weather{
  13.     int rain;
  14.     double temp;
  15.     double wind;
  16.     struct _weather *link;
  17. } weather;
  18.  
  19. int totalRain(weather *first){
  20.     int sum=0;
  21.     weather *p=first;
  22.     for (i=0; i<12; i++){
  23.         sum+=(*p).rain;
  24.         p=(*p).link;
  25.     }
  26.     return sum;
  27. }
  28.  
  29. double lowestTemp(weather *first){
  30.     double lowest=(*first).temp;
  31.     weather *p=(*first).link;
  32.     for (i=1; i<12; i++){
  33.         if ((*p).temp<lowest)
  34.             lowest=(*p).temp;
  35.         p=(*p).link;
  36.     }
  37.     return lowest;
  38. }
  39.  
  40. double aveWind(weather *first){
  41.     double sum=0;
  42.     weather *p=first;
  43.     for (i=0; i<12; i++){
  44.         sum+=(*p).wind;
  45.         p=(*p).link;
  46.     }
  47.     return sum/12.0;
  48. }
  49.  
  50. void printWeather(weather *first){
  51.     weather *p=first;
  52.     for (i=0; i<12; i++){
  53.         printf("%4i %7.1f %6.1f\n", (*p).rain, (*p).temp, (*p).wind);
  54.         p=(*p).link;
  55.     }
  56. }
  57.  
  58. int main(void) {
  59.     FILE *infile;
  60.     infile=fopen("astana.txt", "r");
  61.     weather *first=NULL;
  62.     weather *prev=NULL;
  63.     for (i=0; i<12; i++){
  64.         int r;
  65.         double te, wi;
  66.         fscanf(infile, "%i %lf %lf", &r, &te, &wi);
  67.         weather *p=(weather*)malloc(sizeof(weather));
  68.         (*p).rain=r;
  69.         (*p).temp=te;
  70.         (*p).wind=wi;
  71.         (*p).link=NULL;
  72.         if (first==NULL){
  73.             first=p;
  74.         } else{
  75.             (*prev).link=p;
  76.         }
  77.         prev=p;
  78.     }
  79.     fclose(infile);
  80.     printf("The total rainfall: %4i\n", totalRain(first));
  81.     printf("The lowest temperature: %7.1f\n", lowestTemp(first));
  82.     printf("average wind speed: %6.1f\n\n", aveWind(first));
  83.     printWeather(first);
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment