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.