+ 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.
13 Respostas
+ 2
#include<stdio.h>
float Median3(float a, float b, float c);
int main()
{
int N,i;
float values[10],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]);
}
+ 1
Try yourself first and post your attempt if unsolved...
+ 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
+ 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
+ 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.
+ 1
Thank you for your answer
+ 1
Yeah
+ 1
👍Great!
I also tried this way:
https://code.sololearn.com/coU1Qykq8A6s/?ref=app
+ 1
Very much thankful for your support ☺️
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
0
This is my whole question.
0
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];
}
}
From this code, you are comparing not assigned values and storing in 3 arrays. I don't understand your logic here..
What you need is :
* after taking input values, call the function with array values at indices i-1, i, i+1.
* the function should work like arrange 3 passed values in ascending order and find middle value , that is Midian. Just return that value and store in new array at i th index.
* if i-1, or I+1 are out of valid indexes like < 0 or >N ( less than 0 or greater than N-1), just take value at those indexes as 0.
Ex: when i=0,
value[i-1] , value[I], value[I+1] are 0,-3.5, 4.7 arrange in ascending order is -3.5, 0, 4.7 and median =0 return this value and strore in new array at I th index like
for( int I=0;I<N;I++)
PrArray[I] = Median(value[i-1], value[i],value[i+1] ) ;
Now print, input array and processed array PrArray..
Hope you can implement this. Try once.
0
You're welcome..
Hope you got correct code..!