Thread: Displaying Minimum and Maximum values from input

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Displaying Minimum and Maximum values from input

    I am working on a program for a class. Background is that there is a sample of salmon taken, and each is weighed. I am supposed to as the user to input the number of salmon in the sample, then enter each of their weights. I then need to display the total, average, minimum, and maximum weights.

    I have figured out the total and average, but can't quite get the minimum and maximum. I understand that I need to use an if statement to see if the value entered is less than what is currently the minimum, and if it is, change minimum to that new value. Also the same thing for maximum. I just can't quite figure out how to do that within my While loop.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main( void )
    {
             float weight;  /*Define Variables*/
             int counter;
             int num_salmon;
             float tot_weight;
             float avg_weight;
             float min_weight=50000;
             float max_weight=-1;
             
    {       printf( "How many salmon are in the sample?: " );  /*Get total from user*/
            scanf( "%d", &num_salmon); /*Read the number*/
            printf ("Number of salmon entered is: %d\n", num_salmon);
    {
    /*Get the weight of each fish and add them to the total.*/
            tot_weight = 0;
            counter = 0;
            while (counter < num_salmon)
            {    printf("Enter the weight of a fish:\n");
                    scanf("%f", &weight);
                    counter = counter + 1;
                    tot_weight= tot_weight + weight;
    
                    if      (weight<min_weight);
                    {       min_weight=weight;
                    }
                    else if (weight>min_weight);
                    {       min_weight=min_weight;
                    }
                    if      (weight>max_weight);
                    {       max_weight=weight;
                    }
                    else if (weight>max_weight);
                    {       max_weight=max_weight;
                    }
                    }
            }       
            printf ("All %d fish entered.\n", counter );
            printf ("Total weight of all salmon is %f pounds.\n", tot_weight);
    }               
            avg_weight=tot_weight/counter;
            printf ("The average weight of the sampled salmon is %f pounds.\n", avg_weight);
             return EXIT_SUCCESS;
                    
                     
    }               
    }

    Can anyone help me straighten out the issue here? I would greatly appreciate it.

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mgardnertech View Post
    I am working on a program for a class. Background is that there is a sample of salmon taken, and each is weighed. I am supposed to as the user to input the number of salmon in the sample, then enter each of their weights. I then need to display the total, average, minimum, and maximum weights.

    I have figured out the total and average, but can't quite get the minimum and maximum. I understand that I need to use an if statement to see if the value entered is less than what is currently the minimum, and if it is, change minimum to that new value. Also the same thing for maximum. I just can't quite figure out how to do that within my While loop.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main( void )
    {
             float weight;  /*Define Variables*/
             int counter;
             int num_salmon;
             float tot_weight;
             float avg_weight;
             float min_weight=50000;
             float max_weight=-1;
             
    {  //WTH is this doing here??  Lose it!
            printf( "How many salmon are in the sample?: " );  /*Get total from user*/
            scanf( "%d", &num_salmon); /*Read the number*/
            printf ("Number of salmon entered is: %d\n", num_salmon);
    {  //Another curly brace to lose! We don't just stick in a curly brace, pell mell!
    
    /*Get the weight of each fish and add them to the total.*/
            tot_weight = 0;
            counter = 0;
            while (counter < num_salmon)
            {    printf("Enter the weight of a fish:\n");
                    scanf("%f", &weight);
                    counter = counter + 1;
                    tot_weight= tot_weight + weight;
    
                    if      (weight<min_weight);     //this is right, keep it
                    {       min_weight=weight;
                    }
                    else if (weight>min_weight);  //this is a frankenstein of logic - lose it
                    {       min_weight=min_weight;   //C'mon!! You had to know this was crap
                    }
                    if      (weight>max_weight);
                    {       max_weight=weight;
                    }
                    else if (weight>max_weight);    //OMG, another Dr. Frankenstein! Lose this.
                    {       max_weight=max_weight;
                    }
                    }
            }       
            printf ("All %d fish entered.\n", counter );
            printf ("Total weight of all salmon is %f pounds.\n", tot_weight);
    }     
            avg_weight=tot_weight/counter;
            printf ("The average weight of the sampled salmon is %f pounds.\n", avg_weight);
             return EXIT_SUCCESS;
                    
                     
    
    }
                   
    
    }
    

    Can anyone help me straighten out the issue here? I would greatly appreciate it.

    Thanks.
    Your indentation style is not as helpful as it should be. Consider the three french braces I've highlighted in blue, above.

    What does each of them signal to a reader of your code? What block of code is being ended, by each of them?

    You might remember because you just posted this up, but a year from now, you surely won't. Neither will any reader (like me), who you want to have look at your code.

    Contrast that with the clarity of the following:
    Code:
    
    //this is K&R style, which I prefer
    while( i < num_fishes)   {
       if(fish_weighs < min_weight)   {
          min_weight = fish_weight;
       }  //see how this brace lines up perfect with the "if" above it? It 
          //literally guides your eye right to the block of code it ends.
    }  //ditto. You can't help but see the relationship between the start of
        //of the while loop, and this closing french brace.
    
    
    //this is Student or Uni style, which is also excellent. All the notes from above,
    //apply here, as well.
    
    for(i = 0; i < num_fishes; i++)
    {
       if(fish_weighs < min_weight)
       {
          min_weight = fish_weight;
       }
    }
    The example above is a simplified one, but I believe it shows the point. I like your if statement style, in your code, but it is non-standard. Quite nice though.

    I don't like the style you used with the closing braces in blue, however. It's a style that will come around and bite you on the butt, before long. What I mean is, you won't see an error in logic or syntax, that you would have seen, had you used a better style of indentation in your code. In fact, I believe it bit you right in this program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 09:26 PM
  2. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 03:54 PM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  5. Replies: 2
    Last Post: 10-31-2002, 07:27 PM