C语言-如何打印出输入到数组中的最大数字?

标签 c arrays printing

我希望能够打印出用户输入的最大年龄以及最小年龄。

此外,我注意到我的程序不包含小数点后的数字。例如,它只会显示 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);
}

最佳答案

我认为这会有所帮助。 对于小数点,您必须将数组声明为floatFLT_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/

相关文章:

c++ - 文件 IO 打印到屏幕

c - 如何使用 x86intrin.h

c# - C# 中的 JSON 到 ListView

c - 为什么没有输出,EOF 的值是多少?

java - 在for循环中设置数组内容

ios - UITableView 无法使用 Swift 3 中的 Index 正确显示数据

java - 如何在主方法中以流畅的句子打印我创建的方法的结果?

python - 打印列表外的元素

c - 求 k 素数的个数;

java - 使用 Android NDK 创建并显示弹出窗口?