我希望能够打印出用户输入的最大年龄以及最小年龄。
此外,我注意到我的程序不包含小数点后的数字。例如,它只会显示 25.00,而不是 25.25。
非常感谢任何帮助!
#include "stdafx.h"
#include "stdio.h"
void main()
{
int age[11];
float total = 0;
int i = 1;
float average;
do
{
printf("# %d: ", i);
scanf_s("%d", &age[i]);
total = (total + age[i]);
i = i + 1;
} while (i <= 10);
average = (total / 10);
printf("Average = %.2f", average);
}
最佳答案
我认为这会有所帮助。
对于小数点,您必须将数组声明为float
。
FLT_MAX 这些宏定义浮点
的最大值。在使用FLT_MAX之前,您应该包含
float.h头文件。
#include <stdio.h>
#include <float.h>
int main(void)
{
float age[11];
float total = 0;
int i = 1;
float average;
float largestInput = 0.0;
float smallestInput = FLT_MAX;
do
{
printf("# %d: ", i);
scanf("%f", &age[i]);
total = total + age[i];
//here i am checking the largest input
if (age[i]> largestInput){
largestInput = age[i];
}
//here i am checking the smallest input
if (age[i] < smallestInput) {
smallestInput = age[i];
}
i = i + 1;
} while (i <= 10);
average = (total / 10);
printf("Average = %.2f\n", average);
printf("Largest Input Is = %.2f\n", largestInput);
printf("Smallest Input IS = %.2f", smallestInput);
return 0;
}
关于C语言-如何打印出输入到数组中的最大数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39877979/