Ask Question
1 February, 06:09

Write the following C function and main function;

void find_three_smallest (int a[], int n, int*smallest, int * second_smallest, int * third_smallest);

When passed an array "a" of the length "n", the function will search for its smallest, second_smallest and third_smallest repectively. In case of a tie, it can put one to be smallest and another one to be smallest, For example. a[5]={1,3,4,5,1,2} the smallest, second_smallest and third_smallest will be 1, 1, and 2 respectively.

+4
Answers (1)
  1. 1 February, 07:46
    0
    The program to this question can be given as:

    Program:

    #include / /include header files.

    #include

    //declaration of function

    void find_three_smallest (int a[], int n, int * small, int * second_smallest, int * third_smallest);

    //definitation of function

    void find_three_smallest (int a[], int n, int * smallest, int * second_smallest, int * third_smallest)

    {//function body.

    int i, first_number, second_number, third_number; / /define variable.

    / / There should atleast be three values

    if (n < 3) / /check condition.

    {

    printf (" Invalid Input "); / /print message.

    return;

    }

    first_number = second_number = third_number = INT_MAX;

    for (i = 0; i < n; i + +) / /Using loop

    {

    //conditional statements.

    / / If current element is smaller than first then update all

    if (a[i] < first_number)

    {

    third_number = second_number;

    second_number = first_number;

    first_number = a[i];

    }

    else if (a[i] < second_number)

    {

    third_number = second_number;

    second_number = a[i];

    }

    else if (a[i] < third_number)

    {

    third_number = a[i];

    }

    }

    //print values.

    printf ("The smallest value = %i/n", first_number);

    printf ("Second_smallest value = %i/n", second_number);

    printf ("Third_smallest value = %i/n", third_number);

    }

    //main method

    int main ()

    {

    int num, small, second_small, third_small; / /define variable.

    int x[6] = {1,3,4,5,1,2}; / /define 1-D array.

    num = sizeof (x) / sizeof (x[0]);

    find_three_smallest (x, num,&small, &second_small, &third_small); / /calling function.

    return 0;

    }

    Output:

    The smallest value = 1

    Second_smallest value = 1

    Third_smallest value = 2

    Explanation:

    In the above C language program firstly we declare a function that's name is already given in the question.

    In this function we define the variable that takes all value from the main function and checks that the given values (numbers) are greater then 3. if the value is greater then 3 functions arranged values and finds the smallest, second smallest and third smallest value and print it. In the main function, we define a 1_D array. In that array, we assign the value and pass the value to the function. In the next line, we calling the function.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write the following C function and main function; void find_three_smallest (int a[], int n, int*smallest, int * second_smallest, int * ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers