I have created a program to read from an external file. The program must locate the first maximum and the second. I am unsure about how to find the second maximum. Possibly by resetting the first max, but I am unsure how to do it. Here is my code for the program to locate the first max. What should i do from here?
I am a big time beginner. Thanks!
Code:
/* Homework 14 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define FILENAME "exam2.dat"
int main(void)
{
/* Declare and initalize variables. */
int j=0, locmax, flag0=0, x, max, count=0;
double sum=0.0, ave;
FILE*data;
/* Open file. */
data=fopen(FILENAME, "r");
/*Read file. */
while (fscanf(data, "%i", &x) == 1)
{
j++;
sum+=x;
count++;
if(flag0==0)
{
flag0=1;
max=x;
locmax=j;
}
if(x>max)
{
max=x;
locmax=j;
}
}
/* Compute average. */
{
ave=sum/count;
/* Print summary. */
printf("Average: %7.2f \n", ave);
printf("Maximum: %3i \n", max);
printf("Location of Max: %3i \n", locmax);
}
fclose(data);
return 0;
}