Friday, November 3, 2017

Day 16 - More Pointers

HOMEWORK:
El Nino Conditions:
/*------------------------*/
/* This program reads a data file of ENSO index values and */
/* determines the maximum El Nino condition in the file. */

#include <stdio.h>
#define FILENAME "ENSO.txt"
#define MAX_SIZE 10

int main(void)
{
/* Declare variables and function prototypes. */
int k = 0, year[MAX_SIZE], qtr[MAX_SIZE], max_k = 0, min_k = 0;
double index[MAX_SIZE];
FILE *enso;
/* Read sensor data file. */
enso = fopen(FILENAME, "r");
if (enso == NULL)
printf("Error opening input file. \n");
else
{
while (fscanf(enso, "%d %d %lf",year+k,qtr+k,index+k)==3)
{
//printf("%d %d %f \n", *(year+k),*(qtr+k),*(index+k));
if (*(index+k) > *(index+max_k))
{
max_k = k;
}
if (*(index+k) < *(index+max_k))
{
min_k = k;
}
k++;
}
/* Print data for maximum El Nino condition. */
printf("Maximum El Nino Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",*(year+max_k),*(qtr+max_k));
printf("Max Index Value: %f \n", *(index+max_k));

/* Print data for minimum El Nino condition. */
printf("\nMinimum El Nino Conditions in Data File \n");
printf("Year: %d, Quarter: %d \n",*(year+min_k),*(qtr+min_k));
printf("Min Index Value: %f \n", *(index+min_k));
/* Close file. */
fclose(enso);
}
/* Exit program */
return 0;
}

/*---------------------------------------------------*/

No comments:

Post a Comment