Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C program : Find Median of Two Sorted Arrays | C Programs
Latest :

C program : Find Median of Two Sorted Arrays | C Programs

Write a C program to find the median of the given two sorted arrays. Median is the middle number when the numbers are sorted in either ascending or descending order.

To implement this, our required inputs are the sizes of two arrays along with the elements or data values of the two arrays. Our desired output is the double type number representing the median of the given two input arrays when combined into one sorted array.

Find Median of Two Sorted Arrays Using Function

The first step here is, to read our necessary inputs and to do so, we can make use of the predefined function scanf() which reads the input of any primitive datatype like int, char, float, double, long, etc., at runtime.

Based on the format specifier given, the datatype of the input to be read is determined. In our given problem statement, all the necessary inputs are of integer type and therefore, we use the ‘%d’ format specifier to read them.

Firstly, we’ll read the sizes of the two arrays (n1, n2) following which we’ll create the two arrays (a1, a2) of the respective size. Then, we’ll read the elements or data values of the two arrays.

printf(“\nEnter the size of Array 1 : “);

scanf(“%d”,&n1);

printf(“\nEnter the size of Array 2 : “);

scanf(“%d”,&n2);

int a1[n1],a2[n2];

printf(“\nEnter the %d sorted elements in Array1:\n”,n1);

for(i=0;i<n1;i++) {

scanf(“%d”,&a1[i]);

}

printf(“\nEnter the %d sorted elements in Array2:\n”,n2);

for(i=0;i<n2;i++) {

scanf(“%d”,&a2[i]);

}

Output:

Standard Method

In the above method, we have seen the logic to determine the median of the given two sorted arrays using functions. The benefits of using functions is that, it makes the parts of code within functions reusable and also enhances the readability of the code.

If some logic is needed elsewhere in the code and is within a function block of its own then, instead of rewriting the same set of statements we could just make a function call and pass the necessary arguments thereby, making it reusable.

Also, since the code is placed within different function blocks based on its logic and functionality, the code is lot cleaner and is placed systematically.

This way, if some part is to be found we can directly go to the corresponding function block and look for it. Hence, it increases the readability of the code.

As we can observe the same above, the main method is handling all the input/output related operations whereas the user-defined function (median) is handling the logic behind merging the two sorted array and thereby, finding the median.

But, we can also write the entire code within main method as the time and space complexity is the same for both because of the same logic being used.

So, if we are sure that, we do not need to reuse this logic then, there is no harm in writing it within main itself else, we might have to rewrite same set of statements again. We can see below the illustration of the same.

Output:

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...