Ryota Nakamura
/
Function_Median
配列になったfloat型のデータに対して,中央値(メジアン)を戻り値として返す関数を作成.ただし,引数は配列ポインタ(float型)と配列個数(int型)の2種類を用いる.
Revision 0:ac4681bf247f, committed 2017-02-26
- Comitter:
- RyotaNakamura
- Date:
- Sun Feb 26 04:09:47 2017 +0000
- Commit message:
- ??????float????????????????????????????????????????????????float????????int?)?2???????; ;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r ac4681bf247f main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Feb 26 04:09:47 2017 +0000 @@ -0,0 +1,58 @@ +/*配列になったfloat型のデータに対して,中央値(メジアン)を戻り値として返す関数を作成せよ.*/ +/*ただし,引数は配列ポインタ(float型)と配列個数(int型)の2種類を用いよ.*/ + +#include "mbed.h" + +Serial pc(USBTX, USBRX); + +void Median(float *date,int n); + +int n; //配列要素数 +float *array; //float型のポインタ + +int main(int argc, char* argv[]){ + pc.printf("The element of array : n = "); + pc.scanf("%d\n",&n); //配列要素数を入力 + + array=new float[n]; //実数n個分の配列を確保 + + for(int i=0;i<n;i++){ + pc.scanf("%f\n",&array[i]); //配列要素を入力 + } + + pc.printf("_______________\n"); + Median(array,n); //関数へ飛ぶ + + delete[] array; //配列を削除 + + return 0; +} + +void Median(float *date,int n){ + float median; + for(int i=0;i<n;i++){ //昇順に並び替え + for(int j=i+1;j<n;j++){ + if(array[i]>array[j]){ + float *x; + float *y; + x=&array[i]; + y=&array[j]; + float tmp; + tmp=*x; + *x=*y; + *y=tmp; + } + } + } + + for(int i=0;i<n;i++) //並び替えた結果の出力 + pc.printf("%f\n",array[i]); + + if(n % 2==0){ //配列要素数が偶数のとき + median=(array[n/2]+array[n/2-1])/2; + pc.printf("Median= %f\n",median); + }else{ //配列要素数が奇数のとき + median=array[(n-1)/2]; + pc.printf("Median= %f\n",median); + } +} \ No newline at end of file
diff -r 000000000000 -r ac4681bf247f mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Feb 26 04:09:47 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d \ No newline at end of file