简体   繁体   English

即使我的条件得到验证,为什么不返回 true

[英]Why not returning true even if my condition is verified

I'm new to programming and I work on this program.我是编程新手,我在这个程序上工作。

The requirement is: Write a C program to check a given array (length will be at least 2) of integers and return true if there are two values 15, 15 next to each other.要求是:编写一个 C 程序来检查给定的整数数组(长度至少为 2),如果有两个值 15、15 相邻,则返回 true。

Although my condition is verified, the function is not returning true unless I exclude the "else".虽然我的情况得到验证,但 function 不会返回 true,除非我排除“else”。 But if I exclude else, it is not returning 0 when false, but any other random number.但是如果我排除其他,它不会在 false 时返回 0,而是返回任何其他随机数。 I don't understand why and how to fix it.我不明白为什么以及如何解决它。

#include <stdio.h>
#include <stdbool.h>

int check_fifteen(int a[], int size);

int main(){
   int a[]={1, 6, 15, 15, 3, 5, 15};
   int size= sizeof(a) / sizeof(a[0]);

   printf( "%d", check_fifteen(a, size));

}
int check_fifteen(int a[], int size){
   int i=0;

   if(size < 2){
       return 0;
   }
   for(i=0; i<size-1; i++){
       if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true;
       }
       else
           return false;
   }
}

Even if one pair doesn't satisfy if condition you are returning false .即使一对不满足if condition ,您也将返回false That shouldn't happen, you should check for all pairs and then return false if none satisfies.这不应该发生,您应该检查所有对,如果没有满足则返回 false。

if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true;
       }
       else
           return false;
}

To

for(i=0; i<size-1; i++){
       if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true; // if any pair satisfies you return true there itself
       }
}

// you will reach here only if every pair doesn't satisfy the requirement. 
return false;

The loop is interrupted as soon as there will be encountered two consecutive elements of the array that either equal each other and to 15 or are not equal.一旦遇到数组的两个连续元素,要么彼此相等且等于15 ,要么不相等,循环就被中断。

   for(i=0; i<size-1; i++){
       if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true;
       }
       else
           return false;
   }  

At least place the statement return false outside the loop.至少将语句return false放在循环之外。

   for(i=0; i<size-1; i++){
       if( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
           return true;
       }
   }
   return false;

It would be better to declare and define the function the following way最好通过以下方式声明和定义 function

int check_fifteen( const int a[], size_t size )
{
    const int Target = 15;
    int success = 0;

    for ( size_t i = 1; !success && i < size; i++ )
    {
        if ( a[i] == Target ) success = a[i-1] == Target;
    }

    return success;
}

and the variable size declare like并且变量size声明为

size_t size = sizeof(a) / sizeof(a[0]);

Also there is no great sense to write a function that checks only that two adjacent elements are equal to 15. You could write a more general function.此外,编写仅检查两个相邻元素是否等于 15 的 function 也没有什么意义。您可以编写更通用的 function。

Here you are.给你。

#include <stdio.h>

int check_equal_adjacent( const int a[], size_t size, int value )
{
    int success = 0;

    for ( size_t i = 1; !success && i < size; i++ )
    {
        if ( a[i] == value ) success = a[i-1] == value;
    }

    return success;
}

int main(void) 
{
    int a[] = { 1, 6, 15, 15, 3, 5, 15 };
    size_t size = sizeof( a ) / sizeof( *a );

    printf( "%s\n", check_equal_adjacent( a, size, 15 ) ? "true" : "false" );
   
    return 0;
}

The program output is程序 output 是

true

The problem is that the function check_fifteen already returns false at the first iteration because since the if condition isn't satisfied (the first two elements of the pointed array doesn't contain the value 15 ), inside of the else statement you immediately return false .问题是 function check_fifteen在第一次迭代时已经返回false ,因为由于不满足if条件(指向数组的前两个元素不包含值15 ),在else语句中你立即返回false .

for (i = 0; i < size-1; i++){
   if ( ( a[i] == 15 ) && ( a[i+1] == 15  ) ){
       return true;
   }
   else
       return false;    // You tell: If the 'if' condition is not matched, return `false`.
}

Place the return false;放置return false; after the loop, so the else statement won't make the function return already at the first iteration when if ( a[i] == 15 && a[i+1] == 15 ) is not true.在循环之后,因此当if ( a[i] == 15 && a[i+1] == 15 )不正确时, else语句不会使 function 在第一次迭代时返回。

for (i = 0; i < size-1; i++){
   if ( a[i] == 15  && a[i+1] == 15 ) {
       return true;
   }
}

return false;

This type of bug is easily avoided if you follow the rule (guideline, stylistic preference) that each function should have only one return point.如果您遵循每个 function 应该只有一个返回点的规则(指南、风格偏好),则可以轻松避免此类错误。 Just maintain a flag which is set when you see the validating condition.只需维护一个在您看到验证条件时设置的标志。 Something like:就像是:

int
check_fifteen(const int *a, int size)
{
        int status = 0;
        const int *b = a + size;
        while( a + 1 < b && !status ) {
                status = a[0] == 15 && a[1] == 15;
                a += 1;
        }
        return status;
}

Note that you can reduce the work a bit with a simple "optimization" (bearing in mind that premature optimization is the root of all evil):请注意,您可以通过简单的“优化”来减少工作量(请记住,过早的优化是万恶之源):

int
check_fifteen(const int *a, int size)
{
        int status = 0;
        const int *b = a + size;
        while( a + 1 < b && !status ) {
                if( a[1] == 15 ) {
                        status = *a++ == 15;
                } else {
                        a += 2;
                }
        }
        return status;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM