Thread: Find specific number in array

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    Find specific number in array

    I am trying to find the specific number in array. if user enter number ie 5 then program should be able to find that the number is present in array or not if it present in array then print number match and if its not in array then say it' invalid number

    Code:
    #include<stdio.h>int main ()
    {
    	int array[10], size, i, number;
    	
    	printf("Enter size : ");
    	scanf("%d", &size);
    	for(i = 0; i < size; i++)
    	{
    		printf(" \n Enter array element : ");
    		scanf("%d",&array[i]);
    	}
        
    	printf("Enter number : ");
    	scanf("%d", &number);
    	number = array[0];
    		
    	for(i = 0; i < size; i++)
    	{
    		if (number == array[i])
    		{
    			printf(" \n Number match");
    		}
    		else
    		{
    			printf("\n Invalid Number");
    		}
    	}
    	
    	return 0;
    }
    why this program doesn't execute ?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    It executes for me. It doesn't give the right answer, of course, but it executes.

    What is the purpose of line 15? You are overwriting number with a[0].

    Also, you presumably only want to print the message "Invalid Number" once (and only if the number is not in the array). The way you've written it, it prints "Invalid Number" over and over, even if the number is in the array.

    You should initialize a flag before your loop:
    Code:
    int found = 0;
    And then in the loop, if you find the number in the array, set found to true and break the loop:
    Code:
    if (a[i] == number) {
        found = 1;
        break;
    }
    Then use the state of the flag to print the correct message.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by john.c View Post
    It executes for me. It doesn't give the right answer, of course, but it executes..
    if i initialize variable i then it execute

    number = array[0];

    I am assuming first number of array is specif number and if it's not then compare with next index
    Code:
    #include<stdio.h>int main ()
    {
        int array[10], size, number;
    	int i = 0;
         
        printf("Enter size : ");
        scanf("%d", &size);
        for(i = 0; i < size; i++)
        {
            printf(" \n Enter array element : ");
            scanf("%d",&array[i]);
        }
         
        printf("Enter number : ");
        scanf("%d", &number);
        number = array[0];
             
        for(i = 0; i < size; i++)
        {
            if (array[i]== number)
            {
                printf(" \n Number match");
            }
            else
            {
                printf("\n Invalid Number");
            }
        }
         
        return 0;
    }
    Is there any simple method then the flag to display only true or false condition

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There are lots of ways to do this but the way you've specifically chosen is incorrect, because what happens is
    > number = array[0];
    you assign array[0] to the input variable, making it impossible to remember what number the user typed.

    I recommend something like the following.
    Code:
    int found = -1;
    for (int idx = 0; idx < size; idx++) 
    {
       if (array[idx] == number)
       {
          found = idx;
          break;
       }
    }
    found is either where the number is, or a -1 in case it isn't in the array at all.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    There are lots of ways to do this but the way you've specifically chosen is incorrect, because what happens is
    .
    If I do this, then both of statement's, if statement and else statement ​​will print because they will be under the for loop

    Code:
    #include<stdio.h>
    int main (){
        int array[10], size, number, found;
         int i = 0;
        printf("Enter size : ");
        scanf("%d", &size);
        for(i = 0; i < size; i++)
        {
            printf(" \n Enter array element : ");
            scanf("%d",&array[i]);
        }
         
        printf("Enter number : ");
        scanf("%d", &number);
       
         found = -1;
         for (int i = 0; i < size; i++) 
        {
          if (array[i] == number)
          {
            found = i;
            break;
           }
        }
        return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vead
    If I do this, then both of statement's, if statement and else statement ​​will print because they will be under the for loop
    That's impossible by definition: if control enters the if branch of an if statement, then it will not also enter the else branch, and vice versa. Hence, what you need to do is to make use the variable named found in order to write the if statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Thanks to all of you

    Code:
    #include<stdio.h>int main ()
    {
        int array[10], size, number, found;
         int i = 0;
        printf("Enter size : ");
        scanf("%d", &size);
        for(i = 0; i < size; i++)
        {
            printf(" \n Enter array element : ");
            scanf("%d",&array[i]);
        }
         
        printf("Enter number : ");
        scanf("%d", &number);
       
         found = -1;
         for (int i = 0; i < size; i++) 
        {
          if (array[i] == number)
          {
            found = i;
            break;
           }
        }
    	if (found ==1)
    	{
    	  printf("number is valid");
    	}
    	else
    	{
    		printf("number is invalid");
    	}
        return 0;
    }
    Enter size : 4


    Enter array element : 1


    Enter array element : 2


    Enter array element : 3


    Enter array element : 4
    Enter number : 5
    number is invalid

    Enter size : 4


    Enter array element : 1


    Enter array element : 2


    Enter array element : 3


    Enter array element : 4
    Enter number : 2
    number is valid

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Repeat your test and try and find element 4.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You've confused my idea and whiteflag's idea a little.

    My idea was to use a boolean, initializing found to 0 and setting it to 1 if the number is found.

    whiteflag's idea is better since it tells you the specific index if it's found. Since 0 is a valid index, he uses -1 to mean "not found".

    It would be possible to do without the found variable if you wanted:
    Code:
    int i;
    for (i = 0; i < size; i++)
        if (number == array[i])
            break;
    if (i == size)
        printf("Not found\n");
    else
        printf("Found at index %d\n", i);
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Feb 2018
    Posts
    1
    [QUOTE=vead;1277008] Try this!
    It finds the number with its index

    Code:
    #include<stdio.h>
    
    int main ()
    {
        int array[10], size, i, number;
        int index = -1;
         
        printf("Enter size : ");
        scanf("%d", &size);
        for(i = 0; i < size; i++)
        {
            printf(" \n Enter array element : ");
            scanf("%d",&array[i]);
        }
         
        printf("Enter number : ");
        scanf("%d", &number);
             
        for(i = 0; i < size; i++)
        {
            if (number == array[i])
                index = i;
        }
        
        if(index != -1)
            printf("Number found at index %d",index);
         else
             printf("\n Invalid Number");
             
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-11-2017, 01:26 AM
  2. Find missing number in an array given the sum
    By futilecheese28 in forum C Programming
    Replies: 3
    Last Post: 04-23-2017, 08:30 PM
  3. Need help!! Find number in an array..
    By Elias Zaguri in forum C Programming
    Replies: 12
    Last Post: 10-14-2015, 09:09 AM
  4. [HELP] find closes number from array??
    By Fromar123 in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2012, 01:33 PM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM

Tags for this Thread