Home > front end >  Is there a way to display "number not found" after the forloop ends?
Is there a way to display "number not found" after the forloop ends?

Time:10-05

#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);
        }
}

**>i added else printf("number not found"); here but its also looping and printing a lot **

CodePudding user response:

Keep it simple... just add the print after the loop and add a return when you have a match.

Like:

#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");
}

BTW:

Notice that it shall be int main(void)

Notice that pointers are to be printed using %p with a cast to (void*)

CodePudding user response:

  1. Always check the return value of the scanf!!
  2. Initialize pointer to NULL. If nothing found it will remain NULL
  3. Use the correct format for the address (%p), and cast pointer to (void *) before passing to printf
  4. main return type is 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

CodePudding user response:

Just put the call of printf outside the for loop. For example

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 );
}

Also do not use magic numbers like 5. Instead use named constants. And declare variables in minimal scopes where they are used. And using the conversion specifier %d with a pointers has undefined behavior. You need to use the conversion specifier %p.

Here is a demonstrative program.

#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 );
    }
}

The program output might look like

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
  • Related