输出从降序到升序变化 [英] Output change from descending to ascending order

查看:116
本文介绍了输出从降序到升序变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <conio.h>
#define arr_size 5
void main(void)
{
    float numbers[arr_size] ;
    int counter;
    printf("Input five numbers: \n");
    for (counter = 0; counter < arr_size; ++counter)
        {
            printf("%i?",counter + 1);
            scanf("%f", &numbers[counter]);
        }
    printf("Here they are:");
    for (counter = 4; counter >= 0; --counter)
    printf(" %g",numbers[counter]);
}


我想将输出从降序更改为升序.例如,输出将显示54321,但是我希望输出从12345开始显示,我可能知道它会将计数器更改为0吗?


I want to change the output from descending to ascending order. For example, the output will showing 54321, but I want the output showing start from 12345, may I know is it change the counter to 0?

推荐答案

首先,不要请勿使用幻数.您在第一个循环中使用了"arr_size",在第二个循环中也使用了它.否则,您将冒着在一个位置更改大小的风险,并且由于未在另一位置更改t而导致代码失败.
Firstly, don''t use Magic Numbers. You used "arr_size" in your first loop, use it in your second as well. Otherwise you run the risk of changing the size in one place, and your code fails because you didn''t change t in the other.
for (counter = arr_size - 1; counter >= 0; --counter)
   printf(" %g",numbers[counter]);

要反转打印顺序,您可以通过两种方式执行此操作:

To reverse the order of printing, you could do this two ways:

for (counter = arr_size; counter > 0; --counter)
   printf(" %g",numbers[arr_size - counter]);


for (counter = 0; counter < arr_size; ++counter)
   printf(" %g",numbers[counter]);

BTW:按照惯例,在for 循环中使用后缀表示法-不是强制性的,而是正常的:
代替

BTW: It is a convention to use postfix notation in for loops - not mandatory, but normal:
Instead of

for (counter = arr_size - 1; counter >= 0; --counter)
   printf(" %g",numbers[counter]);

使用

for (counter = arr_size - 1; counter >= 0; counter--)
   printf(" %g",numbers[counter]);



[edit] Typo:添加新条件时忘记从for循环中删除旧条件-太早了! -OriginalGriff [/edit]



[edit]Typo: Forgot to remove old condition from for loop when adding new condition - it''s early! - OriginalGriff[/edit]


这篇关于输出从降序到升序变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆