Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * prog.c
- *
- * Created on: Nov 10, 2016
- * Author: dastan.iyembergen
- */
- #include <stdio.h>
- int i;
- typedef struct _weather{
- int rain;
- double temp;
- double wind;
- struct _weather *link;
- } weather;
- int totalRain(weather *first){
- int sum=0;
- weather *p=first;
- for (i=0; i<12; i++){
- sum+=(*p).rain;
- p=(*p).link;
- }
- return sum;
- }
- double lowestTemp(weather *first){
- double lowest=(*first).temp;
- weather *p=(*first).link;
- for (i=1; i<12; i++){
- if ((*p).temp<lowest)
- lowest=(*p).temp;
- p=(*p).link;
- }
- return lowest;
- }
- double aveWind(weather *first){
- double sum=0;
- weather *p=first;
- for (i=0; i<12; i++){
- sum+=(*p).wind;
- p=(*p).link;
- }
- return sum/12.0;
- }
- void printWeather(weather *first){
- weather *p=first;
- for (i=0; i<12; i++){
- printf("%4i %7.1f %6.1f\n", (*p).rain, (*p).temp, (*p).wind);
- p=(*p).link;
- }
- }
- int main(void) {
- FILE *infile;
- infile=fopen("astana.txt", "r");
- weather *first=NULL;
- weather *prev=NULL;
- for (i=0; i<12; i++){
- int r;
- double te, wi;
- fscanf(infile, "%i %lf %lf", &r, &te, &wi);
- weather *p=(weather*)malloc(sizeof(weather));
- (*p).rain=r;
- (*p).temp=te;
- (*p).wind=wi;
- (*p).link=NULL;
- if (first==NULL){
- first=p;
- } else{
- (*prev).link=p;
- }
- prev=p;
- }
- fclose(infile);
- printf("The total rainfall: %4i\n", totalRain(first));
- printf("The lowest temperature: %7.1f\n", lowestTemp(first));
- printf("average wind speed: %6.1f\n\n", aveWind(first));
- printWeather(first);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment