C编程 - 这些函数我做错了什么?

时间:2014-02-03 21:15:49

标签: c function max min median

我编写的这个程序应该要求工资,计算最小值,最大值,平均值,中位数,然后按降序对它们进行排序并显示它。该程序运行,但最大,最小,平均和工资量完全关闭。我一直在寻找,重读,只是无法弄清楚我哪里出错了。请帮忙!!!

#include <stdio.h>

/* Function Prototypes */

int get_number_of_salaries (void);

void enter_salaries (int [], int);

void sort (int [], int);

void ending_sort (int [], int); 

float get_median (int [], int);

float average (float []);

int minimum_value (int [], int);

int maximum_value (int[], int);



/* Functions */

int get_number_of_salaries (void)
{
    int number_of_salaries;
do
{
    printf ("Enter the number of salaries to process (2-100): ");
    scanf ("%i", &number_of_salaries);
    fflush(stdin);

    if (number_of_salaries <= 1)
        printf (" I'm sorry, you need to enter more than one salary.\n\n");
    if (number_of_salaries > 100)
        printf (" I'm sorry, you can not enter more than 100 salaries.\n\n");

} while (number_of_salaries <= 1 || number_of_salaries > 100);

return number_of_salaries;

}

void enter_salaries ( int salaries [], int number_of_salaries)

{
int i;


for ( i = 1; i <= number_of_salaries; ++i)
{
    do 
    {
        printf (" Enter salary #%i: ", i);
        scanf ("%i", &salaries[i]);
        fflush(stdin);

        if (salaries[i] <= 0)
            printf (" *** Invalid salary entered. Please re-enter. *** \n");

    } while (salaries[i] <= 0);



} 

printf ("\n");

}

int minimum_value (int salaries [], int number_of_salaries)
{
int minimum, i;

minimum = salaries [0];

for (i = 1; i < number_of_salaries; ++i)
    if (salaries [i] < minimum)
        minimum = salaries [i]; 

return minimum;

}


int maximum_value (int salaries [], int number_of_salaries)
{
int maximum, i;

maximum = salaries [0];

for (i = 1; i < number_of_salaries; ++i)
    if (salaries [i] > maximum)
        maximum = salaries [i];

return maximum;

}

float average ( float a[])
{
int i, number_of_salaries;
float avg, sum = 0.0;
for (i = 0; i < number_of_salaries; ++i)
{
    sum += a[i];
}

avg = (sum/number_of_salaries);

return avg;
 }

 void sort (int salaries [], int number_of_salaries)

 {

int i, j, temp;


    for (i = 0; i < number_of_salaries - 1; ++i)
        for (j = i + 1; j < number_of_salaries; ++j)
        if (salaries [i] > salaries [j])
        {

            temp = salaries[i];
            salaries[i] = salaries[j];
            salaries[j] = temp;
        }



} /* end swap */

float get_median (int salaries[], int number_of_salaries)
{
float temp;
int x, i;

for (x = 0; x < number_of_salaries; ++x)
for (i = x + 1; i < number_of_salaries; ++i)
{
    if (salaries[x] > salaries [i])
    {
        salaries[i] = temp;
        salaries[i] = salaries[x];
        salaries[x] = temp;
    }
}

if (number_of_salaries % 2 == 0)
return (salaries[number_of_salaries / 2] + salaries[number_of_salaries / 2 - 1]) / 2;
else
return salaries[number_of_salaries / 2];
printf (" The median salary is %.1f \n", get_median);

}

void ending_sort (int salaries[], int number_of_salaries)
{
int i, j, temp;



    for (i = 0; i < number_of_salaries - 1; ++i)
        for (j = i + 1; j < number_of_salaries; ++j)
        if (salaries [i] < salaries [j])
        {
            temp = salaries[i];
            salaries[i] = salaries[j];
            salaries[j] = temp;
        }
}


int main (void)
{
/* Variable Declarations */
/*-----------------------*/
int salaries [100];
int number_of_salaries, maximum, minimum, x, i; 
float avg;
int total_salary;
float median;

/* Output Greeting */
/*---------------------------------------------------*/
printf (" Welcome to the Salary Calculator.\n");




number_of_salaries = get_number_of_salaries ();

enter_salaries (salaries, number_of_salaries);

minimum_value (salaries, number_of_salaries);

maximum_value (salaries, number_of_salaries);

printf (" The minimum salary is %i \n", minimum);

printf (" The maximum salary is %i \n", maximum);

printf ("\n"); /* Spacing. */

average (salaries);

printf (" The average salary rounded is %i \n", avg);

sort (salaries, number_of_salaries);

get_median (salaries, number_of_salaries);

median = get_median (salaries, number_of_salaries);

printf (" The median salary is %.1f \n", median);

ending_sort (salaries, number_of_salaries);

printf (" The salaries entered were: \n");
for (x = 1; x < number_of_salaries; ++x)
    printf ("%i", salaries[x]);



printf ("\n");


getchar ();

} /* End Main */

2 个答案:

答案 0 :(得分:1)

在enter_salaries中,你的for循环看起来像这样

for ( i = 1; i <= number_of_salaries; ++i)

数组中的第一个值将位于索引0,最后一个值位于索引number_of_salaries - 1,因此将循环更改为

for ( i = 0; i < number_of_salaries; ++i)

检查其他功能,它们都有同样的问题。在average函数中,您在函数体中声明number_of_salaries,并在未初始化的情况下使用它。我想你想以与其他函数相同的方式传递number_of_salaries。此外,salaries成为平均原型中的浮点数组,而最初它是int

答案 1 :(得分:0)

一个错误是变量maximum,minimum和avg未初始化。另一个是你从工资[1]开始将工资存储在数组工资中,但是在minimum_value方法中,你将最小工资分配为工资[0]。所以在minimum_value中你可以改变:

minimum =薪水[1];

for(i = 2; i&lt; number_of_salaries; ++ i)

和其他maximum_value方法类似......

还要将您的代码更改为main:

minimum = minimum_value(工资,number_of_salaries);

maximum = maximum_value(薪水,number_of_salaries);