# Thread: Max Min Problem

1. ## Max Min Problem

So here I am writing this code and I am having problems with something basic, but I think I am overlooking something simple. I am trying to read from a text file and compute mean, root mean, and Max height (which is the highest number - lowest number) It's coming up with some huge number so yea, I need a second opinion please! Here is the data file

Code:
```-4.1 -2.2 -0.5  1.2  3.3  4.6  5.1  2.1  0.2 -3.6 -4.1  0.2
0.5  2.2  4.1 -0.2 -1.2 -3.3 -4.6 -5.0 -2.2 -1.1  0.8	3.2
-0.1 -4.8```
And here is the Code

Code:
```/* Christopher R Smith
COMP1200 - Fall 2008
Assignment 6
Program: Average Value Program

-------------------------------------------------------------------------------------------------------------
Taking surface measurements from a text file, compute and display the Arithmetic Mean Value, the
Root Mean Square Average, and the Max Roughness Height

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

#include <stdio.h>
#include <math.h>

int main()
{
FILE 		*inp;
double	value,
mean,
root_mean_square,
sum=0,
max_height;
int 		numValue=0,
count;

inp = fopen("surface.txt", "r");

while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
{
sum += fabs(value);
numValue++;

}

mean = sum / numValue;

while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
{
sum += pow( fabs(value), 2 );
numValue++;

}

root_mean_square = sqrt( sum / numValue);

while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
{
double max=-10000, min=10000;

if (value >= max)
max = value;
else
min = value;

;
max_height = max - min
}

printf( "Arithmetic mean value %.2lf\n", mean);
printf( "Root-mean-square average %.2lf\n", root_mean_square);
printf( "Maximum roughness height %.2lf\n", max_height);

return (0);
}```
This problem is probably coming from this segment of code

Code:
```while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
{
double max=-10000, min=10000;

if (value >= max)
max = value;
else
min = value;

;
max_height = max - min
}```
I am receiving this output

Code:
```Arithmetic mean value 2.48
Root-mean-square average 1.58
Maximum roughness height 1889828851749420549890302903237191694835027000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00```
Is that huge number some other value somewhere else in the computer

2. First, calculate max_height after you run through the loop. And, remove the initialization from the loop body. Lastly, max and min need to be updated separately. I find this to be a eloquent solution:

Code:
```double MAX ( double a, double b )
{
return a > b ? a : b;
}

double MIN ( double a, double b )
{
return a < b ? a : b;
}

/** call MAX and MIN like so **/

double value;
double max = -1000.0;
double min = 1000.0;
double max_height = 0.0;

while ( 1 == fscanf( inp, "&#37;lf", &value ) ) {
min = MIN( value, min );
max = MAX( value, max );
}

max_height = max - min;```
Not yet compiled, but good enough ...

3. what if we aren't supposed to "know" how to make a user defined function like you did in

Code:
`double MAX ( double a, double b )`
and

Code:
`double MIN ( double a, double b )`

4. Then you just write what's in the loop body in the main function instead.

5. I am not sure if understand this part of the code

Code:
```double MAX ( double a, double b )
{
return a > b ? a : b;
}

double MIN ( double a, double b )
{
return a < b ? a : b;
}```

the

Code:
`? a : b;`

6. That's the ternary operator: It's shorthand for an if-else. Feel free to ignore that and use an actual if-else in your code.

7. Originally Posted by citizen
First, calculate max_height after you run through the loop. And, remove the initialization from the loop body. Lastly, max and min need to be updated separately. I find this to be a eloquent solution:

Code:
```double MAX ( double a, double b )
{
return a > b ? a : b;
}

double MIN ( double a, double b )
{
return a < b ? a : b;
}

/** call MAX and MIN like so **/

double value;
double max = -1000.0;
double min = 1000.0;
double max_height = 0.0;

while ( 1 == fscanf( inp, "%lf", &value ) ) {
min = MIN( value, min );
max = MAX( value, min );
}

max_height = max - min;```
Not yet compiled, but good enough ...
What happens if you pass NaN to one of those functions?

8. >> What happens if you pass NaN to one of those functions?
At a stab in the dark, I'm guessing it breaks, otherwise you wouldn't have mentioned it. But correct me wherever possible. And admittedly I did not consider NaNs while helping this lad with his assignment.

Popular pages Recent additions