Related questions
Task- Median elements (C Language)
Example #4 expected output is 5, but from the
Given an array of integer elements, the median is the value that separates the higher half from the lower half of the values. In other words, the median is the central element of a sorted array.
Since multiple elements of an input array can be equal to the median, in this task you are asked to compute the number of elements equal to the median in an input array of size N, with N being an odd number.
Requirements
- Name your program project4_median.c.
- Follow the format of the examples below.
- The program will read the value of N, then read in the values that compose the array. These values are not necessarily sorted.
- The program should include the following function. Do not modify the function prototype.
int compute_median(int *a, int n);
-
- a represents the input array, n is the length of the array. The function returns the median of the values in a.
- This function should use pointer arithmetic– not subscripting – to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
- In the main function, call the compute_median function, then compute and display the result.
- Pointer arithmetic is NOT required in the main function.
Examples (your program must follow this format precisely)
Example #1
Enter array size: 5
Enter array elements: 31 17 32 31 88
Output: 2
Example #2
Enter array size: 5
Enter array elements: 1 1 1 1 1
Output: 5
Example #3
Enter array size: 7
Enter array elements: 44 29 22 23 14 15 20
Output: 1
Example #4
Enter array size: 7
Enter array elements: 0 0 0 7 0 0 7
Output: 5
Program-
#include <stdio.h>
int compute_median(int *a, int n);
int main() {
int n, i;
//Getting the inputs from user
printf("Enter array size: ");
scanf("%d", &n);
int a[n];
printf("Enter array elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
//printing the correct output
int median_count = compute_median(a, n);
printf("Output: %d\n", median_count);
return 0;
}
//median for the given inputs
int compute_median(int *a, int n) {
int temp, median;
for (int i = 1 ; i <= n-1 ; i++)
{
for (int j = 1 ; j <= n-i ; j++)
{
if (a[j] <= a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
median = a[n/2 + 1];
int count = 0;
for (int i = 0; i < n; i++)
{
if (*(a + i) == median) {
count++;
}
}
return count;
}
Step by stepSolved in 4 steps
- Redo Programming using dynamic arrays. You must ask the user for the number of candidates and then create the appropriate arrays to hold the data.arrow_forwardProblem Statement Average function: Please complete the following function. The function has two parameters which are an integer array and an integer as a size of the array. The function computes the average value by adding all values from each element of an array and dividing them by the size of the array. The function returns an integer of the average value. Example 1 average({1, 2, 3, 4, 5}, 5) → 3 Example 2 average({2, 4, 6, 8, 10, 12, 14, 16, 18}, 9) → 10 Example 3 average({5, 5, 5, 5, 5}, 5) → 5 Partial Solution Please try to implement the full source code in your IDE first. The solution is partially provided below. Fill in the blanks to complete the missing parts and make sure to not add an empty space before and after the answer. } int sum = for(int i = } sum 0; 0; i return sum/ average(int array[], int size) { array size; i++){arrow_forwardJava:arrow_forward
- C++arrow_forwardTask - Median elements (C Langugage) Given an array of integer elements, the median is the value that separates the higher half from the lower half of the values. In other words, the median is the central element of a sorted array. Since multiple elements of an input array can be equal to the median, in this task you are asked to compute the number of elements equal to the median in an input array of size N, with N being an odd number. Requirements Name your program project4_median.c. Follow the format of the examples below. The program will read the value of N, then read in the values that compose the array. These values are not necessarily sorted. The program should include the following function. Do not modify the function prototype. int compute_median(int *a, int n); a represents the input array, n is the length of the array. The function returns the median of the values in a. This function should use pointer arithmetic– not subscripting – to visit array elements. In other...arrow_forwardASSEMBLY It is preferable to pass Arrays by reference when calling subroutines. True Falsearrow_forward
- MIPS Assembly The program: Write a function in MIPS assembly that takes an array of integers and finds local minimum points. i.e., points that if the input entry is smaller than both adjacent entries. The output is an array of the same size of the input array. The output point is 1 if the corresponding input entry is a relative minimum, otherwise 0. (You should ignore the output array's boundary items, set to 0.) My code: # (Note: The first/last entry of the output array is always 0# since it's ignored, never be a local minimum.)# $a0: The base address of the input array# $a1: The base address of the output array with local minimum points# $a2: Size of arrayfind_local_minima:############################ Part 2: your code begins here ###la $t1, ($t2)la $t1, ($t2)move $a1, $s0 li $a2, 4jal find_local_minima print:ble $a2, 0, exitlw $a0, ($s0)li $v0, 1syscall addi $s0, $s0, 4addi $a2, $a2, -1 ############################ Part 2: your code ends here ###jr $ra I am not getting the correct...arrow_forwardC# Question Assume that you have an array of integers called numbers that has been declared and filled with values. Write the heading of a for loop that can be used to iterate through the entire array. Use i as the loop variable.arrow_forwardOne dimension array in C:Create an array of 100 integer elements and initialize the array elements to zero.Populate the array (using a for loop) with the values 10,20,30,..., 990,1000.Write code (using for loops) to sum all the elements and output the sum to the screen. without using #definearrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education