Okay, I've got another thing here with my program that I need a third opinion on. It's a program that requires me to read in values using an array and then after all the values have been entered, display the minimum value, maximum value, median, and average.
Although the program works, I'm not too sure about the minimum value procedure. I had a bug with my program that involved displaying "0.00" for minimum value every time the program finishes. For instance, I would type in values from 1.00 to 20.00 and then the program would display the values in descending order. The maximum value is 20.00 while the minimum value is 1.00. The program always displays 0.00 as minimum value.
I corrected the problem by initializing minimum_income to 1.00. While it corrected the problem, I'm wondering if this kind of procedure is acceptable? Take a look at the code and tell me what you think:
/* Prob 11-5
* Created by Yuriy Mokriy
*/
#include <stdio.h>
#define HOMES 20 /* The maximum number of homes surveyed */
main()
{
float household_incomes[HOMES], /* The array to store the incomes */
maximum_income, /* The maximum income amount in the survey */
minimum_income, /* The minimum income amount in the survey */
average_income, /* The average income amount in the survey */
total_income, /* The total income amount from the survey */
median_income; /* The median income amount in the survey */
int income_count, /* Counts the incomes in the survey */
limit, /* Keeps track of how far to go on a pass */
pass, /* The number of the pass */
temp; /* Temporarily stores income amounts for sorting */
/* Initialization */
total_income = 0.00;
average_income = 0.00;
median_income = 0.00;
maximum_income = 0.00;
minimum_income = 1.00;
/* Prompt for incomes */
for (income_count = 0; income_count < HOMES; income_count++)
{
printf("\nEnter the income amount for home #%d: $", income_count + 1);
scanf("%f", &household_incomes[income_count]);
total_income += income_count + 1;
/* Determine maximum income */
if (household_incomes[income_count] > maximum_income)
maximum_income = household_incomes[income_count];
/* Determine minimum income */
if (household_incomes[income_count] < minimum_income)
minimum_income = household_incomes[income_count];
}
/* Calculate median and average income */
average_income = total_income / HOMES;
median_income = ((household_incomes[10] - 1) + (household_incomes[11] - 1)) / 2;
/* Sort the array in descending order */
limit = HOMES - 2;
for (pass = 1; pass <= HOMES - 1; pass++)
{
for (income_count = 0; income_count <= limit; income_count++)
if (household_incomes[income_count] < household_incomes[income_count + 1])
{
temp = household_incomes[income_count];
household_incomes[income_count] = household_incomes[income_count + 1];
household_incomes[income_count + 1] = temp;
}
--limit;
}
/* Display the sorted incomes */
printf("\nThe incomes in decreasing order are as follows:\n");
for (income_count = 0; income_count < HOMES; income_count++)
printf("$%.2f ", household_incomes[income_count]);
printf("\n");
/* Display the results */
printf("\nMAXIMUM INCOME: $%.2f", maximum_income);
printf("\nMINIMUM INCOME: $%.2f", minimum_income);
printf("\nAVERAGE INCOME: $%.2f", average_income);
printf("\nMEDIAN INCOME: $%.2f", median_income);
printf("\n");
}
Is it okay to use 1.00 as minimum income or do I need to have it set to 0.00 and find another way to fix the problem?