【问题标题】:Is there a way to display "number not found" after the forloop ends?有没有办法在 forloop 结束后显示“找不到数字”?
【发布时间】:2021-11-25 03:12:23
【问题描述】:
#include <stdio.h>
void main()
{
    int n,i;
    int arr[5]={5,4,3,2,1};
    int *ptr;
    printf("input the number you want to find:\n");
    scanf("%d",&n);
    for(i=0;i<5;i++)
        if(arr[i]==n)
        {
            ptr=&arr[i];
        printf("number '%d' is present in the array and it is the %d st/nd/rd/th term in the array.\n its address is: %d",n,i+1,ptr);
        }
}

**>我添加了 else printf("number not found");在这里,但它也循环和打印了很多 **

【问题讨论】:

  • 您需要一个单独的变量来跟踪您是否找到了号码。
  • 或者,一旦找到号码,return。然后在循环之后添加您的printf,只有在找不到号码的情况下才会到达。如果您想报告多个匹配项,则无法使用。
  • @dbush 谢谢!我太傻了,我搜索了一下,发现和你建议的完全一样。无论如何,我感谢您的帮助。
  • @EugeneSh。有趣的。我会试试看。如您所见,我是初学者,但我真诚地感谢您抽出宝贵的时间为我提供有用的意见。再次感谢好心的先生。
  • 也许还将main()类型更改为int main(void)?

标签: arrays c function for-loop if-statement


【解决方案1】:

只需将 printf 的调用放在 for 循环之外。例如

int *ptr = NULL;

printf("input the number you want to find:\n");
scanf("%d",&n);

for( i = 0; ptr == NULL && i < 5; i++ )
{
    if ( arr[i] == n )
    {
        ptr = arr + i;
    }
}

if ( ptr != NULL )
{
    printf("number '%d' is present in the array and it is the %td st/nd/rd/th term in the array.\n its address is: %p\n", 
           n, ptr - arr + 1, ( void * )ptr );
}
else
{
    printf( "number %d is not present in the array.\n", n );
}

也不要使用像5 这样的幻数。而是使用命名常量。并在使用它们的最小范围内声明变量。 并且使用带有指针的转换说明符 %d 具有未定义的行为。您需要使用转换说明符%p。

这是一个演示程序。

#include <stdio.h>

int main(void) 
{
    int arr[] = { 5, 4, 3, 2, 1 };
    const size_t N = sizeof( arr ) / sizeof( *arr );
    
    int n = 0;
    
    printf( "input the number you want to find (default is %d): ", n );
    scanf( "%d", &n );
    
    int *ptr = NULL;
    
    for ( size_t i = 0; ptr == NULL && i < N; i++ )
    {
        if ( arr[i] == n )
        {
            ptr = arr + i;
        }
    }

    if ( ptr != NULL )
    {
        printf( "number '%d' is present in the array and it is the %td st/nd/rd/th term in the array.\n its address is: %p\n", 
                n, ptr - arr + 1, ( void * )ptr);
    }
    else
    {
        printf( "number %d is not present in the array.\n", n );
    }
}

程序输出可能看起来像

input the number you want to find (default is 0): 3
number '3' is present in the array and it is the 3 st/nd/rd/th term in the array.
 its address is: 0x7ffefecb1778

【讨论】:

  • 也许他想打印所有的出现。
  • @0___________ 他明确写了他想找一个号码。
  • @VladfromMoscow 哇,我以为这只是一个简单的程序,但这么简单的程序可以被这么多有趣的人以这么多有趣的方式完成
【解决方案2】:

保持简单...只需在循环之后添加打印并在匹配时添加返回。

喜欢:

#include <stdio.h>
int main(void)
{
    int n,i;
    int arr[5]={5,4,3,2,1};
    int *ptr;
    printf("input the number you want to find:\n");
    if (scanf("%d",&n) != 1)
    {
        // Input error
        exit(1);
    }
    for(i=0;i<5;i++)
    {
        if(arr[i]==n)
        {
            ptr=&arr[i];
            printf("number '%d' is present in the array and it is the %d st/nd/rd/th term in the array.\n its address is: %p",n,i+1,(void*)ptr);
            return 0;  // Done... just end the program
        }
    }
    puts("not found");
}

顺便说一句:

注意应该是int main(void)

请注意,将使用%p 打印指针并转换为(void*)

【讨论】:

  • 如果他想做更多的事情怎么办?它只适用于这个简单的例子
  • @0___________ 没错,如果 OP 想要一个多线程解决方案来发现人类已知的最大素数,那么这不是解决方案。尽管如此,这是解决 OP 要求的最简单方法
  • 它不必是多线程的——你的解决方案并不简单——它很糟糕。
  • @0___________ 不...不是。它以一种简单的合规方式解决了 OP 想要的问题 - 这从来都不是坏事
  • @0___________ 如果还有更多工作要做,那么很快“搜索”功能将转移到自己的功能中,如果需要,可以再次安全地提前返回。
【解决方案3】:
  1. 始终检查scanf的返回值!!
  2. 将指针初始化为 NULL。如果没有找到,它将保持为 NULL
  3. 使用正确的地址格式(%p),并在传递给printf之前将指针转换为(void *)
  4. main 返回类型为int。
int main()
{
    int n;
    int arr[]={5,4,3,5,2,5,1};
    int *ptr = NULL;
    printf("input the number you want to find:\n");
    if(scanf("%d",&n) == 1)
    {
        for(size_t i=0;i<sizeof(arr)/ sizeof(arr[0]);i++)
            if(arr[i]==n)
            {
                ptr=&arr[i];
                printf("number '%d' is present in the array and it is the %zu st/nd/rd/th term in the array.\n its address is: %p",n,i+1, (void *)ptr);
            }
        if(!ptr) printf("NUMBER NOT FOUND!!!!!\n");
    }
    else
    {
        printf("SCANF ERROR\n");
    }
}

https://godbolt.org/z/6zcE4nnz8

【讨论】:

  • @yano 没有。 for 循环中只有一条语句 - if
  • 啊,你说得对。为了清楚起见,我的偏好是括号,在这里感到困惑
  • @0___________ 有很多东西要学,对我来说还有很长的路要走。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
相关资源
最近更新 更多