Solution 12 to 25
Solution 13

Searching for a Number in an Array

In this program, we demonstrate how to search for a specific number within an array of integers. The user is prompted to enter the number of elements in the array and then input each element. After populating the array, the user provides a number to search for. The program iterates through the array to determine if the number exists and, if so, reports its position. This basic search algorithm helps in understanding array traversal and condition checking in C programming.

Code Breakdown

Algorithm

  1. Start
  2. Initialize Array and Variables: Create an array a of size 10, and variables i, n, x, and found.
  3. Input the Number of Elements: Prompt the user to enter the limit n (the number of elements in the array).
  4. Input Array Elements: Use a loop to input n elements into the array a.
  5. Input the Number to Search: Prompt the user to enter the number x to search for in the array.
  6. Search for the Number: Initialize found to 0. Iterate through the array:
    • Compare each element with x.
    • If an element equals x, set found to 1 and break the loop.
  7. Output the Result:
    • If found is 1, print the position of x in the array.
    • If found is 0, print that x is not found in the array.
  8. End

Code Explanation

Example Flowchart

                                   Start
                                     |
                                     V
                    +-----------------------------------+
                    |  Input the number of elements (n) |
                    +-----------------------------------+
                                     |
                                     V
                    +-----------------------------------+
                    | Input n elements into array (a[]) |
                    +-----------------------------------+
                                     |
                                     V
                    +----------------------------------+
                    |  Input the number to search (x)  |
                    +----------------------------------+
                                     |
                                     V
                    +----------------------------------+
                    |      Initialize found = 0        |
                    +----------------------------------+
                                      |
                                      V
                    +----------------------------------+
                    |       For i = 0 to n-1           |
                    |     (Iterate through array)      |
                    +----------------------------------+
                         |                          |
                         |                          |
                         V                          V
           +-------------------------+    +-------------------------+
           |    Is a[i] == x?        |    |    Is a[i] != x?        |
           +-------------------------+    +-------------------------+
                          |                          |
                          |                          |
                          V                          V
           +-------------------------+    +-------------------------+
           |     Set found = 1       |    |     Continue loop       |
           +-------------------------+    +-------------------------+
                          |                          |
                          V                          | 
           +-------------------------+               V
           |      Break loop         |              End
           +-------------------------+               
                          |                          
                          V                          
           +-------------------------+               
           |     Check found value   |               
           +-------------------------+               
                          |                          
            +-------------+-------------------+           
            |                                 |           
            V                                 V           
+-------------------------+       +-------------------------+
|   If found = 1          |       |      If found = 0       |
|   Print "x is found     |       |     Print "x is not     |
|   at location i+1"      |       |   found in the array"   |
+-------------------------+       +-------------------------+
            |                                  |
            V                                  V
           End                                End