一个函数,它接收一个整数数组作为参数,如果它是有序的则返回true [英] A function that receives an integer array as parameter and returns true if it is in order

查看:141
本文介绍了一个函数,它接收一个整数数组作为参数,如果它是有序的则返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
int order(int ar[5]);


int main()
{

   int ar[5];
   order(ar[5]);
}

int order(int ar[5])
{
    int i;

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

    if( 0<= 1<= 2 || 0>=1>= 2);
    {
        printf("%s","true");
    }

        return 0;
}

推荐答案

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



这是一个空循环,没有。循环体仅包含尾部'; '。请注意,'; '是C / C ++中的有效(空)语句,因此可以构成代码块!


This is an empty loop and does nothing. The loop body only consists of the trailing ';'. Note that ';' is a valid (empty) statement in C/C++ and therefore can constitute a code block!

if( 0<= 1<= 2 || 0>=1>= 2);



这里有几个问题:

1.和上面的陈述一样,这个什么都不做,因为if块只包含尾随的'; '

2.你要比较常数值0,1,和2,而不是数组元素。有什么意义?

3.您定义比较的方式可能不会按照您期望的方式进行评估:e。 G。 0< = 1< = 2 将被评估为(0 <= 1)< = 2 这是(true)< = 2 这是(1)< = 2 这是真的。请注意,虽然这可能是您期望的最终结果,但如果您编写(1< = 0< = 2),您将得到完全相同的最终结果:它将被评估as (false)< = 2 (0)< = 2 这也是真的!如果你想确定一个数字在给定的时间间隔内,你应该做这样的比较:


Several problems here:
1. Like the above statement, this will do nothing, since the if block only consists of the trailing ';'
2. you're comparing the constant values 0, 1, and 2, rather than array elements. What is the point?
3. The way you defined the comparison will likely not be evaluated in the way you expect: e. g. 0<=1<=2 will be evaluated as (0<=1)<=2 which is (true)<=2 which is (1)<=2 which is true. Note that while this is probably the end result you expect, you would get the exact same end result if you wrote (1<=0<=2): it will be evaluated as (false)<=2 or (0)<=2 which is also true! If you want to make sure a number is within a given interval, you should make a comparison like this:

int interval_start = 3;
int interval_end = 7;
int my_number = 6;
if (interval_start <= my_number && my_number <= interval_end)
{
   puts("my number is inside the interval.");
}



4.虽然您的代码目前对您最初指定的任务没有任何作用,但您的意图似乎是写为truefor数组中的每个数字,至少在本地,看起来是正确的顺序。因此,在完全有序的数组的情况下,您的输出将类似于 truetruetrue ,而不完美排序的数组将导致类似 falsetruefalse 。您可能想重新考虑(a)何时发出输出和(b)可能在输出中添加换行符。


4. while your code currently does nothing in the way of the task you initially specified, your intention seems to have been to write "true" for every number in the array that, at least locally, appears to be in correct order. So, in case of a perfectly ordered array, your output would be something like "truetruetrue", while a less than perfectly ordered array would result in something like "falsetruefalse". You might want to reconsider (a) when to issue an output and (b) maybe add a linebreak to the output.


提示:如果 ar [k]< ; ar [k + 1] 对于 k = 0,1,.. N-2 然后序列处于(严格升序)顺序。





请注意,该功能不应提前知道阵列的大小。更好的原型可能是:

Hint: if ar[k] < ar[k+1] for k = 0,1,..N-2 then the sequence is in (strict ascending) order.


Please note, the function is not supposed to know in advance the size of the array. A better prototype could be:
int is_in_order( int ar[], int ar_size);


这篇关于一个函数,它接收一个整数数组作为参数,如果它是有序的则返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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