# help finding max and min values from list

This is a discussion on help finding max and min values from list within the C Programming forums, part of the General Programming Boards category; i have made an external list.dat file that my program reads from. The problem is that i have to print ...

1. ## help finding max and min values from list

i have made an external list.dat file that my program reads from. The problem is that i have to print out the highest and lowest numbers from that list. how do i do that?

2. There are several ways to do this. For instance, as you read in the integers,

1. Set the first integer to min && max.
2. compare the second to both, if it is lower than the min value, replace the min value...if it is larger than the max value, replace the max value.

When all of the data has been read in, you will then have your min and max values.

You could also do a sort on the data after it has been read in, however, I think it would be easier to do the compares as the data is read in.

If you want help with code, post what you have.

3. ok, i dont know how to initialize the first integer to min && max...
here is my code

Code:
```
#include<math.h>
#include<stdio.h>

int main(void)

{
FILE *inp;
double abs; //absolute value for each number
double squart; //square of number
double amv; // arithmetic mean value
double rmsa; //root-mean square average
double num; //number inputted
double sum = 0;
double max;
double min;
int input_status;
int count = 0; //running count of numbers inputted.

printf("***Roughness Indicators for file a:surface.dat***\n");

inp = fopen("a:surface.dat", "r");

for (input_status = fscanf(inp, "%lf", &num);
input_status != EOF;
input_status = fscanf(inp, "%lf", &num)) {

abs = fabs(num);
sum += abs;
squart = pow(num, 2);

count += 1;
}

rmsa = sqrt((squart / count));
amv = sum / count;

printf("Arithmetic Mean Value is %f\n", amv);
printf("Root-mean square Average is %f\n", rmsa);
fclose(inp);

return(0);
}```

4. This should work, however I haven't tested it.
Code:
```int main(void)

{
FILE *inp;
double abs; //absolute value for each number
double squart; //square of number
double amv; // arithmetic mean value
double rmsa; //root-mean square average
double num; //number inputted
double sum = 0;
double max;
double min;
int input_status;
int run=1;   /*THIS ALLOWS for the first read of min and max */
int count = 0; //running count of numbers inputted.

printf("***Roughness Indicators for file a:surface.dat***\n");

inp = fopen("a:surface.dat", "r");

for (input_status = fscanf(inp, "%lf", &num);
input_status != EOF;
input_status = fscanf(inp, "%lf", &num)) {

if (run==1)  {
max=num;   /* Step one Sets max and min to first num read in*/
min=num;
/* Step two; determines min and max.
run=0;   /* Keeps it from setting min and max to next variables.   */
}

if (num<min)
min=num;
if (num>max)
max=num;

abs = fabs(num);
sum += abs;
squart = pow(num, 2);

count += 1;
}

/* PRINTS RESULTS */
printf("Max is %lf\n Min is %lf\n",max,min);

rmsa = sqrt((squart / count));
amv = sum / count;

printf("Arithmetic Mean Value is %f\n", amv);
printf("Root-mean square Average is %f\n", rmsa);
fclose(inp);

return(0);
}```

5. Originally posted by ericp023
ok, i dont know how to initialize the first integer to min && max...
You can include the header limits.h and use INT_MIN and INT_MAX for initialization.

Code:
```#include<stdio.h>
#include<limits.h>

int finmin(int *a,int n)
{
int t=INT_MAX,j;
for(j=0;j<n;j++) if( a[j] < t ) t=a[j];
return t;
}

int finmax(int *a,int n)
{
int t=INT_MIN,j;
for(j=0;j<n;j++) if( a[j] > t ) t=a[j];
return t;
}

int main(void)
{
int a[]= {10,4,5,1,12,100,2,-3};
printf("Min:%d\tMax:%d\n",finmin(a,sizeof(a)/sizeof(*a)),finmax(a,sizeof(a)/sizeof(*a)));
return 0;
}```

6. You could use the first element of the list for initialisation. In a single element list the element is both minimum and maximum.

Code:
```ch = min = max = read_character()
while ch != end of file
if ch < min then min = ch
if ch > max then max = ch

7. the way to find min, max values from a list
================================

main()
{
FILE *fp;
int x;
char filename[12];

printf("Enter file name: ");
gets(filename);
fp = fopen(filename,"r");
if (fp == NULL)
{
printf("NO SUCH FILE");
exit();
}
min = max = getw(fp);
while(!feof(fp))
{
x = getw(fp);
if(x > max)
max = x;
if(x < min)
min = x;
}
fclose(fp);
}

>>gets(filename);
gets() is a function that should be avoided.

>>min = max = getw(fp);
You didn't declare min and max.
getw() is non standard, and therefore won't work on some compilers.

>>while (!feof(fp))
This is not the way to control the loop, you'll run into trouble at the end of the file.

main() should be declared:
>>int main(void)
and should return an int.

A more standard and safer way to do things would be to use fgets() to get a line at a time from the file, convert that to a number using strtol() and finally perform the min/max comparison.

Popular pages Recent additions