+ 2

# How can I compute median values of every consecutive three input array elements?

I have to write a program in which an arrays of n values are to be read and median values of every consecutive three input array elements are to be computed and their median values are stored in another array in same order .It assumes all the values beyond the limits of valid array indexes in the input array are zeroes.

11th Jan 2022, 3:42 PM
Nitu Rao
+ 2
#include<stdio.h> float Median3(float a, float b, float c); int main() { int N,i; float values,a[i],b[i],c[i],Median; printf("Enter N\n",N); scanf("%d",&N); printf("N=%d\n",N); for (i=0;i<N;i++) scanf("%f",&values[i]); printf("Input array:\n"); for (i=0;i<N;i++) printf("%.2f\n",values[i]); for (i=0;i<N; i++) { if (a[i]>b[i] && b[i]>c[i]) { a[i]=values[i-1]; b[i]=values[i]; c[i]=values[i+1]; Median=b[i]; } } printf("Median_values are:%.2f\n",b[i]); }
12th Jan 2022, 3:33 AM
Nitu Rao
+ 1
Try yourself first and post your attempt if unsolved...
11th Jan 2022, 7:51 PM
Jayakrishna 🇮🇳
+ 1
Nitu Rao can you say what is your expected output for a sample..? What you think of wrong in your code..? like For ex: sample: 4 3 2 5 3 expected output: 1.00
12th Jan 2022, 12:20 PM
Jayakrishna 🇮🇳
+ 1
float Median3(float a, float b, float c); The function takes three real numbers a, b, and c as input and returns their median value. Write a program which implements Median3(.).The program reads an array of N (to be read) values, and computes median values of every consecutive three input array elements (e.g. for array indexes i-1, i, and i+1), and store their median values computed by using Median3(.) in another array in the same order (i.e. for numbers at indexes i-1, i, and i+1 the median value is stored at the i th index of the output array). It assumes all the values beyond the limits of valid array indexes in the input array are zeroes. Finally the program prints both the original array and the processed array (array of median values). For example given the following input: N=5 Input array: -3.5 4.7 2.0 -1.0 10 The processed array (output) is: 0 2.0 2.0 2.0 0
12th Jan 2022, 12:46 PM
Nitu Rao
+ 1
Input array: -3.5 4.7 2.0 -1.0 10 i=0 values at i-1,i,i+1 are 0, -3.5, 4.7 sorted values are : -3.5, 0, 4.7 =>then Median M=0 i=1 values at i-1,i,i+1 are -3.5, 4.7, 2.0 sorted : -3.5, 2.0, 4.7 => M=2.0 i=2 values at i-1,i,i+1 are 4.7, 2.0, -1.0 sorted : -1.0,2.0,4.7 =>M=2.0 i=3 values at i-1,i,i+1 are 2.0, -1.0, 10 sorted : -1.0, 2.0,10 => M=2.0 i=4 values at i-1,i,i+1 are -1.0,10, 0 sorted : -1.0, 0, 10 => M=0 The processed array (output) is: 0, 2.0, 2.0, 2.0, 0 when if ( i-1<0 or i+1>=N ) pass 0 as value at those indexes. ** Also you can try to find median of 3 values by if -else block without sorting, for simplicity.. hope you can find solution now.. else you can reply for any thing if not clear.. hope it helps.
12th Jan 2022, 6:04 PM
Jayakrishna 🇮🇳
+ 1
14th Jan 2022, 10:15 AM
Nitu Rao
+ 1
Yeah
14th Jan 2022, 4:42 PM
Nitu Rao
+ 1
👍Great! I also tried this way: https://code.sololearn.com/coU1Qykq8A6s/?ref=app
14th Jan 2022, 5:55 PM
Jayakrishna 🇮🇳
+ 1
Very much thankful for your support ☺️
14th Jan 2022, 6:02 PM
Nitu Rao
0
The median is the value that separates the upper half of a sorted set of data from the lower half. It is the middle value. This is not to be confused with the mean or average. hint: loops
11th Jan 2022, 6:29 PM 