【问题标题】:Decrementing an array and print a value if condition met如果条件满足,则递减数组并打印一个值
【发布时间】:2014-12-01 12:30:38
【问题描述】:

我想减少一个指向数组最后一个元素的指针,并检查以下条件:如果指针的值小于 5,则不执行任何操作并进入下一轮循环,如果不是,即如果该值等于或大于 5,打印该值。因此,对于示例中的数组,我只希望打印 6,即第一次遇到等于或大于 5 的值。我尝试了下面的代码,但是在编译时没有错误,它不打印任何价值。我会很感激你的cmets。

#include<stdio.h>

//The is a C program to decrement an array from the last element to the first. 
    int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2}; 
    int *lastElem, count;
int main (void) {
    lastElem = &x[10];
    for (count = 0; count < 11; count++)
        if (abs(*lastElem) < 5)
        continue;
        else 
        printf("%d\n", *lastElem--); 
    return 0;
}

【问题讨论】:

  • 比较错误,因为元素小于5
  • 对不起,我的意思是绝对值。
  • 对不起,我现在没有得到你想要的东西?

标签: c arrays loops absolute-value


【解决方案1】:

你的递减逻辑有问题。如果该值小于 5,则您错过了减量。

检查下面的代码。

#include<stdio.h>

//The is a C programme to decrement an array from the last element to the first. 
    int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2};
    int *lastElem, count;
int main (void) {
    lastElem = &x[10];
    for (count = 0; count < 11; count++)
    {
        if (*lastElem >= 5)
            {
            printf("%d\n", *lastElem);
             break ;
             }
        lastElem--;
    }
return 0;
}

编辑:

当您修改问题以包含 absolute value 比较时,更改如下所示。

注意:当对原始问题进行一些重大更改[这将改变行为和输出]时,请记下这一点,如果可能,请正确提及编辑。

#include<stdio.h>
#include <stdlib.h>

//The is a C programme to decrement an array from the last element to the first. 
int x[11] = {5, -6, -4, -3, -2, -1, 4, 3, 2, 1, -2};
int *lastElem, count;
int main (void) {
        lastElem = &x[10];
        for (count = 0; count < 11; count++)
        {
                if ( abs(*lastElem) >= 5)
                {
                        printf("%d\n", *lastElem);
                        break;
                }
                lastElem--;
        }
        return 0;
}

【讨论】:

  • OP 希望第一次出现大于 4 的数字(在 OP 的情况下为 6)被打印出来,而不是所有大于 4 的数字
  • @coolguy 我试图指出你的错误部分。但是,将更新答案以适应这一点。
  • 对不起,一个问题,如果这些数字是浮点数而不是整数,唯一的变化是在 printf 语句中,即printf(%.2f\n", *lastElem);
  • @homa 没错,但会有其他副作用。 abs () 只对整数起作用,如果我没记错的话。
  • fabs() 用于获取 c 中浮点数的绝对值
【解决方案2】:

您永远不会为lastElem 分配另一个地址,而不是初始值(即第 10 个元素)。此外,如果您想倒退,请将count 设置为 10 并使其计数为 0。在每个循环中,您必须将 &amp;x[count] 分配给 lastElem(或递减它,因为它是一个指针,地址将根据它指向的对象递减)。

【讨论】:

    【解决方案3】:

    printf("%d\n", *lastElem--); 行并不总是执行,它仅在abs(*lastElem) &gt;= 5 时执行。你应该这样做

    #include<stdio.h>
    
    //The is a C programme to decrement an array from the last element to the first. 
    int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2}; 
    int *lastElem, count, value;
    int main (void) 
    {
        lastElem = &x[10];
        for (count = 0; count < 11; count++)
        {
            value = *(lastElem--);
            if (value < 5)
                continue;
            printf("%d\n", value);
            break;
        }
        return 0;
    }
    

    这将通过continue 完成。

    【讨论】:

    • 我正是想要这个,但这并没有打印任何值,我不明白!
    • 是的,但这会打印 6 和 5,但我希望它打印第一次遇到等于或大于 5 的值,然后停止循环。所以,类似于前面的代码,当找到 6 时,只需打破循环。
    • 你说,你只想打印6,在这种情况下你应该把if (*lastElem &gt;= 5)改成if (*lastElem &gt; 5)
    • 是的,但我真的很好奇为什么前面的代码不起作用,因为我想到了使用“继续”的循环
    • @iharob ,如果你这样做,如果数组有 5 而不是 6,则不会打印 5
    【解决方案4】:

    这是执行任务的其他答案中唯一正确的代码。:)

    实际上,您需要一个程序来向后搜索满足给定条件的数组元素。如果有这样的元素然后输出它。

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void ) 
    {
        int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
        const size_t N = sizeof( a ) / sizeof( *a );
        int *first = a, *last = a + N;
    
        while ( first != last && ( unsigned int )abs( *( last - 1 ) ) < 5 ) --last;
    
        if ( first != last ) printf( "%d\n", *--last );
    
        return 0;
    }
    

    输出是

    6
    

    下面展示了此类任务的一般方法

    #include <stdio.h>
    #include <stdlib.h>
    
    int * find_backward( const int a[], size_t n, int ( *predicate )( int ) )
    {
        const int *first = a, *last = a + n;
    
        while ( first != last && !predicate( *( last -1 ) ) ) --last;
    
        return ( int * )last;
    }
    
    int is_greater_or_equal_to_5( int x )
    {
        return 5 <= ( unsigned int )abs( x );
    }
    
    int main( void ) 
    {
        int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
        const size_t N = sizeof( a ) / sizeof( *a );
    
    
        int *target = find_backward( a, N, is_greater_or_equal_to_5 );  
    
        if ( target != a ) printf( "%d\n", *--target );
    
        return 0;
    }
    

    使用这种方法,您可以使用任何整数数组(即使大小为零)以及通过谓词设置的任何条件。

    例如,如果你想找到可以被 3 整除的数组的最后一个元素,你可以这样写

    #include <stdio.h>
    #include <stdlib.h>
    
    int * find_backward( const int a[], size_t n, int ( *predicate )( int ) )
    {
        const int *first = a, *last = a + n;
    
        while ( first != last && !predicate( *( last -1 ) ) ) --last;
    
        return ( int * )last;
    }
    
    int divisible_by_3( int x )
    {
        return x % 3 == 0;
    }
    
    int main( void ) 
    {
        int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
        const size_t N = sizeof( a ) / sizeof( *a );
    
    
        int *target = find_backward( a, N, divisible_by_3 );    
    
        if ( target != a ) printf( "%d\n", *--target );
    
        return 0;
    }
    

    输出是

    3
    

    考虑到这个函数也允许处理常量数组。:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多