Max Min Problem

This is a discussion on Max Min Problem within the C Programming forums, part of the General Programming Boards category; So here I am writing this code and I am having problems with something basic, but I think I am ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    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. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    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 ...
    Last edited by whiteflags; 10-22-2008 at 10:28 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Then you just write what's in the loop body in the main function instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    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. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    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. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by citizen View Post
    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?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    >> 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 subscribe to a feed

Similar Threads

  1. Array problem
    By jalex39 in forum C Programming
    Replies: 5
    Last Post: 03-29-2008, 07:27 PM
  2. Min, max, sort in vector STL
    By codeguy in forum C++ Programming
    Replies: 7
    Last Post: 02-29-2008, 08:06 AM
  3. Max Min
    By jhwebster1 in forum C Programming
    Replies: 6
    Last Post: 02-16-2006, 01:17 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21