-1

Alright.. I have been trying to make this code work, but for some reason it doesn't and I cannot seem to find the issue. Can someone clear it out? It's supposed to run 5 numbers, and you should then enter a desired number you'd like to know whether exist or not in the array. It just keeps saying no.

void search_for_number(int *a, int search);

int main(void)
{
  int number[5];
  int i, search, a = 1;

  for(i = 0; i < 5; i++)
  {
    printf("Number %d of 5: \n", a++);
    scanf("%d", &number[i]);
  }

  printf("\nWhat number should we search for?: \n");
  scanf("%d", &search);

  search_for_number(&number[i], search);
}

void search_for_number(int *a, int search)
{
  int i;

  for(i = 0; i < 5; i++)
  {
    if(*a == search)
    {
      printf("%d is present!\n", search);
    }
  }

  if(*a != search)
  {
    printf("%d is not present.\n", search);
  }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
decipher
  • 137
  • 6
  • 14

2 Answers2

2

To send an array to a function, you need to send the base address of the array and receive it with a pointer.

search_for_number(&number[i], search);

In this line, i = 5 since you did not reset it after the for loop.

Either reset it before calling the function, or you can simply do

search_for_number(number, search);

Since the name of the array is the base address itself.


Another issue is the way you are accessing the array in the function.

In the function search_for_number() since a is now pointing to the first integer of the array, you can access them by doing a[i], which basically means *(a + i). So, after the changes it should look like,

for(i = 0; i < 5; i++)
{
    if(a[i] == search)     // or if(*(a + i) == search)
    {
        printf("%d is present!\n", search);
    }
}

Do the changes accordingly in other places too.


And, since you want to end the function execution after you have found an element, you need to make the function return after it has found,

for(i = 0; i < 5; i++)
{
    if(a[i] == search)      // or if(*(a + i) == search)
    {
        printf("%d is present!\n", search);
        return;
    }
}


Btw, since you added the return; statement in the first loop, you don't need the next check. So, your total function should look like
void search_for_number(int *a, int search)
{
    int i;

    for(i = 0; i < 5; i++)
    {
        if(a[i] == search)      // or if(*(a + i) == search)
        {
            printf("%d is present!\n", search);
            return;
        }
    }

    printf("%d is not present.\n", search);
}

Your main() has a return value, so do add

return 0;

in the end of main(). It nay be omitted, but it is a good coding practice. See this.

Community
  • 1
  • 1
Haris
  • 12,120
  • 6
  • 43
  • 70
1

The function search_for_number is called passing to it the address of the non-existent element with index 5 of the array

search_for_number(&number[i], search);
                  ^^^^^^^^^^

So this call does not make sense.

And this statement within the function search_for_number

if(*a == search)

or this statement

if(*a != search)

always compares the first element of the array with value search.

Also it is a bad idea that the function depends on magic number 5.:)

The function can be written the following way

void search_for_number( const int *a, int n, int search )
{
    int i = 0;

    while ( i < n && a[i] != search ) i++;

    if ( i != n )
    {
        printf( "%d is present!\n", search );
    }
    else
    {
        printf( "%d is not present.\n", search );
    }
}

and called like

search_for_number( number, 5, search );

Here is a demonstrative program

#include <stdio.h>

void search_for_number( const int *a, int n, int search )
{
    int i = 0;

    while ( i < n && a[i] != search ) i++;

    if ( i != n )
    {
        printf( "%d is present!\n", search );
    }
    else
    {
        printf( "%d is not present.\n", search );
    }
}

#define N   5

int main( void )
{
    int a[N] = { 1, 2, 3, 4, 5 };

    for ( int i = 0; i <= N + 1; i++ ) search_for_number( a, N, i );
}    

Its output is

0 is not present.
1 is present!
2 is present!
3 is present!
4 is present!
5 is present!
6 is not present.

Of course it would be better that the second parameter had type size_t instead of int.:) For example

void search_for_number( const int *a, size_t n, int search );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335