简体   繁体   English

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

[英]C language- How to print out the largest number inputted into an array?

I want to be able to print out the largest age inputted by the user as well as the smallest age.我希望能够打印出用户输入的最大年龄以及最小年龄。

Also, I noticed my program doesn't include the numbers after the decimal point.另外,我注意到我的程序不包括小数点后的数字。 It would just say 25.00 for example, rather than 25.25.例如,它只会说 25.00,而不是 25.25。

Any help is greatly appreciated!任何帮助是极大的赞赏!

#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);
}

I think it will be helpful.我认为这会有所帮助。 For decimal point you have to declare your array as float .对于小数点,您必须将数组声明为float FLT_MAX These macros define maximum value of a float . FLT_MAX这些宏定义了float最大值。 Before useing FLT_MAX you should inclue float.h header file.期运用FLT_MAX之前,您应该inclue 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;
}

Pseudo-code.伪代码。 Keep two extra variables.保留两个额外的变量。 long largestInput, smalltestInput.长最大输入,小测试输入。

set largestInput = smalltestInput = age[0];设置最大输入 = 小测试输入 = 年龄 [0];

for (i = 0; i < 10; i++) {
  if age[i]> largestInput{
     largestInput = age[i];
  }
  if age[i] < smallestInput {
      smalltestInput = age[i];
  }
}

Print it as you like随心所欲打印

Your code works fine on my system.您的代码在我的系统上运行良好。 I've modified your code to not be Windows specific and shown you how to calculate the smallest and largest of the numbers entered.我已将您的代码修改为非 Windows 特定代码,并向您展示了如何计算输入的最小和最大数字。 The idea is to constantly compare the current entry to the current smallest and largest number and change smallest and largest as needed.这个想法是不断地将当前条目与当前最小和最大数字进行比较,并根据需要更改最小和最大。 The code below also shows how to start at array index 0.下面的代码还显示了如何从数组索引 0 开始。

#include <stdio.h>


int main()
{
    int age[10];
    float total = 0;
    int i = 0;
    float average;
    int large = -9999;
    int small = 9999;

    do
    {
        printf("# %d: ", i+1);
        scanf("%d", &age[i]);       
        total = (total + age[i]);

        if( age[i] < small )
        {
            small = age[i];
        }

        if( age[i] > large )
        {
            large = age[i];
        }

        i++;

    } while (i < 10);

    average = (total / 10.00);
    printf("Smallest = %d Largest = %d \n", small, large);
    printf("Average = %.2f\n", average);
}


# 1: 30
# 2: 40
# 3: 50
# 4: 2
# 5: 3
# 6: 4
# 7: 6
# 8: -1
# 9: 4
# 10: 5
Smallest = -1 Largest = 50 
Average = 14.30

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用数组打印出许多星号,取决于用户在C中输入的数字 - Using an Array to print out a number of asterisks, dependent on number inputted from user in C 如何使用 C 打印数组中的第二大数字? - How to print second largest number in an array using C? 我的 C 代码应该取正值并在输入 -1 时结束,并且打印最大数字不起作用 - My C code that is supposed to take positive values and end when -1 is inputted, and print the largest number is not working c - 如何在数组中找到最大和最小的数字 - How to find the largest and smallest number in an array in c 打印用户在 C 中输入的所有数字 - Print all inputted number by the user in C 如何在 C 语言中打印带有两个点的数字? - How to print a number with two dots in the C language? 查找用户输入的最大数字,并确定最大数字输入了多少次 - Find the largest number input by the user and identify how many times the largest number was inputted 用户在 C 语言中输入浮点数或字符时如何打印错误消息? - How do I print out an error message when the user enters a floating number or characters in C language? 使用strtok构建char **后释放C语言的内存 - C Language- freeing memory after using strtok to build char** 语言:C 如何让我的代码仅在用户输入的文本满足特定条件时打印消息? - Language: C How do I get my code to only print a message when the inputted text from the user meets a certain criteria?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM