0

I would like to know if the number the user enters is number in the array.

Here is my code:

#define ARR_SIZE 10

int main()
{
    int my_arr[10];
    int secnum = 0;
    int i = 0;

    for (i = 0; i < ARR_SIZE ; i++)
    {
        printf("Enter a number: ");
        scanf("%d",&my_arr[i]);
    }

    printf("Enter the another number");
    scanf("%d",&secnum);

    if(my_arr[i] == secnum)
    {
        printf("an ex");
    }

}

But it doesn't work.

How can I compare a number with another number in array?

Note: I don't know pointers so I need to do it without pointers.

6
  • Please descrip, doesn't work. Never ignore the return value of scanf() always check. Commented Jan 9, 2016 at 14:44
  • where is if check in your code? Commented Jan 9, 2016 at 14:45
  • error:subscripted value is neither array nor pointer. I dont need to use in pointer and I dont undersand the error. How to fix it? Commented Jan 9, 2016 at 14:46
  • @Pawan in the seond code if(my_arr[i]==secnum) Commented Jan 9, 2016 at 14:47
  • in your main() function, where and how are you using if check? Commented Jan 9, 2016 at 14:48

5 Answers 5

1

Why it doesn't work and what is wrong with the code?

  • You are comparing against just one value rather than all the array elements.
  • The value of i after the scanf loop is 10 so arr[i] would exceed the array and could cause Illegal memory access.

Check the comments in the program.

#define ARR_SIZE 10
int main()
{
    int my_arr[ARR_SIZE];  //Use ARR_SIZE because if ARR_SIZE changes, 10 won't causing unforseen errors.
    int secnum = 0;
    int i = 0;

    for (i = 0; i < ARR_SIZE ; i++)
    {
        printf("Enter a number: ");
        scanf("%d",&my_arr[i]);
    }

    printf("Enter the another number");
    scanf("%d",&secnum);

    for (i = 0; i < ARR_SIZE ; i++) // Ensures you are comparing secnum with each array element.
    {
        if(my_arr[i] == secnum)
        {
            printf("an ex"); //Do you wish to break here because one you find a match, the goal is attained :)
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

After the loop, i is equal to ARR_SIZE (10). So you compare my_arr[10] with secnum (0). But my_arr[10], while syntactically correct, points to an undefined value because the size of the array is 10.

Comments

0
#define ARR_SIZE 10

 int main()
{
int my_arr[10];
int secnum = 0;
int i = 0;

for (i=0;i<ARR_SIZE ; i++)
{
    printf("Enter a number: ");
    scanf("%d",&my_arr[i]);
}
printf("Enter the another number");
scanf("%d",&secnum);

for (i=0;i<ARR_SIZE ; i++)
{
    if(my_arr[i]==secnum)
    {
        printf("Given number is in array\n");
        break;
    }
}

}

2 Comments

It doesnt work for me. For this ex I need to check this in another function
@NoNameP then, please add it as a constraint to your question, together with other restrictions and specifications.
0

As pointed by OP in comments to one of the answers, OP apparently need a routine to check for the key in an array.

So once we have stored an array and have accepted a key to search, we need to pass this array and key to a search function which will return true or false depending upon whether the key is present in the array or not.

#include <stdbool.h>    // We need this for `true` and `false` bool values

bool search(int [], int);    // Function declaration

/**** Function Definition *****/
bool search(int numbers[], int key)
{
    int i;

    for(i = 0; i < ARR_SIZE; i++)
        if(numbers[i] == key)
            return true;

    return false;
}  

/** Calling search function from main **/  
...
if(search(my_arr, secnum))
    printf("Number found in array!\n");
else
    printf("Number could NOT be found in array!\n");

Comments

0

To find a value in an array you should iterate through it and compare each value with the wanted number. You should also check the return value of scanf() to control how many item did it actually read. See if this reviewed code is helpfull:

#include <stdio.h>

#define ARR_SIZE 10

int read_numbers(int a[], int size) {
    int i = 0;
    int r = 0;
    while ( i < size ) {
        printf("Please, enter a number (%d of %d): ",i+1,size);
        r = scanf(" %d",&a[i]);         // the space before will ignore any trailing newline
        if ( r == EOF ) break;          // if scanf fails return
        if ( r != 1 ) {                 // if user don't enter a number, repeat
            printf("Wrong input!\n");
            scanf("%[^\n]*");            // will read and ignore everything lweft on stdin till newline
            continue;
        }
        ++i;
    }
    return i;       // return size, unless scanf fails
}

int find_number(int a[], int size, int x) {
    int i = 0;
    while ( i < size  &&  a[i] != x ) ++i;
    return i;   // if x isn't in the array returns size
}

int main()
{
    int my_arr[ARR_SIZE];
    int secnum = 0;
    int i = 0;
    int n = 0;

    n = read_numbers(my_arr, ARR_SIZE);
    if ( n < ARR_SIZE ) {
        printf("Warning, only %d numebers read out of %d!\n",n,ARR_SIZE);
    }  // if happens you have uninitialized elements in array

    printf("Now, enter the value you want to find.\n");
    if ( read_numbers(&secnum, 1) != 1 ) {      // reuse the function
        printf("Sorry, an error has occurred.\n");
        return -1;
    }

    i = find_number(my_arr, n, secnum);     // If all went right n==ARR_SIZE
    if ( i < n ) {              // a match has been found
        printf("Found!\n");
    } else {
        printf("Not found.\n");
    }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.