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. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    Question 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. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    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.
    Last edited by justin69enoch; 02-21-2003 at 07:24 PM.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    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. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    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);
    }
    Last edited by justin69enoch; 02-21-2003 at 08:11 PM.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    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;
    }
    Last edited by pinko_liberal; 02-22-2003 at 03:17 AM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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
        ch = read_character()

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    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);
    }

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First up, vijayabhaskar read this.

    >>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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Finding Max and Min Value
    By will15 in forum C++ Programming
    Replies: 3
    Last Post: 11-28-2006, 10:27 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Calculating min and max values of an arbitrary mesh.
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 04-21-2006, 11:33 AM
  5. HELP! Min Max values using array.
    By steverushby in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2005, 09:13 AM

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