Ask Question

Write a program to define a variable size array, ask the user to enter the size of array and its elements. Then search whether an element (x) exists in the array or not and print the indexes where it found (if it exists in more the one index).

+3
Answers (1)
  1. 18 April, 09:17
    0
    The program to this question can be given as:

    Program:

    //header file

    #include

    void search (int n) / /define function.

    {

    int a[n], find, i; / /variable declaration

    printf ("Enter elements of array : /n"); / /message

    for (i = 0; i
    {

    scanf ("%d", &a[i]); / /input array elements

    }

    printf ("Enter a number to search: ");

    scanf ("%d", &find); / /input number for search

    for (i = 0; i< n; i++) / /search number.

    {

    if (a[i] = = find) / /if element is found in array

    {

    printf ("%d is present at location %d./n", find, i+1); / /print location.

    }

    }

    }

    int main () / /main method

    {

    int n; / /variable declaration

    printf ("Enter size of array: "); / /message

    scanf ("%d", &n); / /input sizeof array.

    search (n); / /calling function.

    return 0;

    }

    Output:

    Enter size of array: 6

    Enter elements of array:

    4

    2

    1

    4

    2

    4

    Enter a number to search: 4

    4 is present at location 1.

    4 is present at location 4.

    4 is present at location 6.

    Explanation:

    In the above C language program firstly we define the header file that is stdio. h this header file is used to take stander input output from the user.

    Then we define the main function. In this function, we declare a variable that is search, n, and i. In this, we also declare an array variable that is a[].

    After inserting all array elements we take a number from the user to search number that is present in the array or not.

    For this process, we define a loop. In this loop, we define a conditional statement in this if block we check elements of the array equal to search variable if this condition is true so it will print the position of number that is found in the array.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program to define a variable size array, ask the user to enter the size of array and its elements. Then search whether an element ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers