求一个c语言程序:输入一组数,求其最大值,最小值,中位数,平均数,方差,标准差及其相关系数。
要求,程序中有数据排序体现且采用冒泡排序,其中有一个模块采用宏编写,但尽量多采用函数编写,相关系数可选择编写。请高手指教,越快越好!...
要求,程序中有数据排序体现且采用冒泡排序,其中有一个模块采用宏编写,但尽量多采用函数编写,相关系数可选择编写。请高手指教,越快越好!
展开
1个回答
展开全部
#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){
int i, j;
int temp_val;
for(i = 0; i < NUM_OF_INT; i++){
for(j = 0; j < NUM_OF_INT - i - 1; j++){
if(MAX(data[j],data[j + 1])){
temp_val = data[j];
data[j] = data[j + 1];
data[j + 1] = temp_val;
}
}
}
}
//平均值
float average(int * data){
int temp_val = 0;
for(int i = 0; i < NUM_OF_INT; i++){
temp_val += data[i];
}
return (float)temp_val / NUM_OF_INT;
}
//方差
float variance(int * data, float average){
double temp_val = 0;
for(int i = 0; i < NUM_OF_INT; i++){
temp_val += pow((data[i] - average), 2);
}
return (float)temp_val / NUM_OF_INT;
}
void main(){
int i;
int temp_val;
int data[NUM_OF_INT];
time_t t;
time(&t);
srand(t);
for(i = 0; i < NUM_OF_INT; i++){
data[i] = rand()%200 + 1;
printf("%d ", data[i]);
}
printf("\n");
sort(data);
for(i = 0; i < NUM_OF_INT; i++){
printf("%d ", data[i]);
}
printf("\n");
printf("min_val = %d,max_val = %d",data[0], data[NUM_OF_INT - 1]);
printf("\n");
if(NUM_OF_INT % 2){
printf("middle_val = %d\n", data[NUM_OF_INT / 2]);
}else{
printf("middle_val = %d\n", (data[NUM_OF_INT / 2] + data[NUM_OF_INT / 2 - 1]) / 2);
}
float average_val = average(data);
printf("average = %f\n", average_val);
float variance_val = variance(data, average_val);
printf("variance = %f\n", variance_val);
float deviation_val = sqrt(variance_val);
printf("deviation = %f\n", deviation_val);
//不懂你说的相关系数是什么,是自相关,还是啥呀
}
#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){
int i, j;
int temp_val;
for(i = 0; i < NUM_OF_INT; i++){
for(j = 0; j < NUM_OF_INT - i - 1; j++){
if(MAX(data[j],data[j + 1])){
temp_val = data[j];
data[j] = data[j + 1];
data[j + 1] = temp_val;
}
}
}
}
//平均值
float average(int * data){
int temp_val = 0;
for(int i = 0; i < NUM_OF_INT; i++){
temp_val += data[i];
}
return (float)temp_val / NUM_OF_INT;
}
//方差
float variance(int * data, float average){
double temp_val = 0;
for(int i = 0; i < NUM_OF_INT; i++){
temp_val += pow((data[i] - average), 2);
}
return (float)temp_val / NUM_OF_INT;
}
void main(){
int i;
int temp_val;
int data[NUM_OF_INT];
time_t t;
time(&t);
srand(t);
for(i = 0; i < NUM_OF_INT; i++){
data[i] = rand()%200 + 1;
printf("%d ", data[i]);
}
printf("\n");
sort(data);
for(i = 0; i < NUM_OF_INT; i++){
printf("%d ", data[i]);
}
printf("\n");
printf("min_val = %d,max_val = %d",data[0], data[NUM_OF_INT - 1]);
printf("\n");
if(NUM_OF_INT % 2){
printf("middle_val = %d\n", data[NUM_OF_INT / 2]);
}else{
printf("middle_val = %d\n", (data[NUM_OF_INT / 2] + data[NUM_OF_INT / 2 - 1]) / 2);
}
float average_val = average(data);
printf("average = %f\n", average_val);
float variance_val = variance(data, average_val);
printf("variance = %f\n", variance_val);
float deviation_val = sqrt(variance_val);
printf("deviation = %f\n", deviation_val);
//不懂你说的相关系数是什么,是自相关,还是啥呀
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询