求一個c語言程序:輸入一組數,求其最大值,最小值,中位數,平均數,方差,標準差及其相關係數.
題目:
求一個c語言程序:輸入一組數,求其最大值,最小值,中位數,平均數,方差,標準差及其相關係數.
要求,程序中有數據排序體現且採用冒泡排序,其中有一個模塊採用宏編寫,但儘量多採用函數編寫,相關係數可選擇編寫.請高手指教,越快越好!
解答:
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
#include "math.h"
#define NUM_OF_INT 30
#define MAX(x,y) x>y?1:0
//排序
void sort(int * data){
\x09int i, j;
\x09int temp_val;
\x09for(i = 0; i < NUM_OF_INT; i++){
\x09\x09for(j = 0; j < NUM_OF_INT - i - 1; j++){
\x09\x09\x09if(MAX(data[j],data[j + 1])){
\x09\x09\x09\x09temp_val = data[j];
\x09\x09\x09\x09data[j] = data[j + 1];
\x09\x09\x09\x09data[j + 1] = temp_val;
\x09\x09\x09}
\x09\x09}
\x09}
}
//平均值
float average(int * data){
\x09int temp_val = 0;
\x09for(int i = 0; i < NUM_OF_INT; i++){
\x09\x09temp_val += data[i];
\x09}
\x09return (float)temp_val / NUM_OF_INT;
}
//方差
float variance(int * data, float average){
\x09double temp_val = 0;
\x09for(int i = 0; i < NUM_OF_INT; i++){
\x09\x09temp_val += pow((data[i] - average), 2);
\x09}
\x09return (float)temp_val / NUM_OF_INT;
}
void main(){
\x09int i;
\x09int temp_val;
\x09int data[NUM_OF_INT];
\x09time_t t;
\x09time(&t);
\x09srand(t);
\x09for(i = 0; i < NUM_OF_INT; i++){
\x09\x09data[i] = rand()%200 + 1;
\x09\x09printf("%d ", data[i]);
\x09}\x09
\x09printf("\n");
\x09sort(data);
\x09for(i = 0; i < NUM_OF_INT; i++){
\x09\x09printf("%d ", data[i]);
\x09}
\x09printf("\n");
\x09printf("min_val = %d,max_val = %d",data[0], data[NUM_OF_INT - 1]);
\x09printf("\n");
\x09
\x09if(NUM_OF_INT % 2){
\x09\x09printf("middle_val = %d\n", data[NUM_OF_INT / 2]);
\x09}else{
\x09\x09printf("middle_val = %d\n", (data[NUM_OF_INT / 2] + data[NUM_OF_INT / 2 - 1]) / 2);
\x09}
\x09float average_val = average(data);
\x09printf("average = %f\n", average_val);
\x09float variance_val = variance(data, average_val);
\x09printf("variance = %f\n", variance_val);
\x09float deviation_val = sqrt(variance_val);
\x09printf("deviation = %f\n", deviation_val);
\x09//不懂你說的相關係數是什麼,是自相關,還是啥呀
}
添加新評論