#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> voidfixed(double&,double&,double&); intmain(void) { system("title Random v1.0.0(dev.milarodvia.macrohard[github[x]])"); srand(time(NULL)); int n; printf("Please enter a statistical sample size:"); scanf("%d",&n); int gn=rand()%(n/2)+1,bn=n-gn; int b[bn],g[gn],c[3]={0},bc[3]={0},gc[3]={0}; memset(b,0,bn); memset(g,0,gn); //!Warning!:If the array `int c[]`,'int bc[]` and 'int gc[]` is not set to 0, then an irreversible data error will occur! //c[0] stands for "statistics with a weight of 0", and the rest is the same. puts("Prepare for data generation(for boy group)... ..."); _sleep(100);//sleep for a while. for(int i=0;i<bn;++i)//range(i,gn) { printf("Generating Group %d data... ...\n",i+1); ++c[b[i]=rand()%3]; ++bc[b[i]]; printf("[%d|boy|(c[b[i]]=%d)]:%d.\n",i+1,c[b[i]],b[i]); } puts("Prepare for data generation(for boy group)... ..."); _sleep(100);//sleep for a while. for(int i=0;i<gn;++i)//range(i,gn) { printf("Generating Group %d data... ...\n",i+1); ++c[g[i]=rand()%3]; ++gc[g[i]]; printf("[%d|girl|(c[g[i]]=%d)]:%d.\n",i+1,c[g[i]],g[i]); } puts("done."); puts("Prepare to calculate parameters... ..."); _sleep(100);//sleep for a while. double d0,d1,d2; d0=c[0]*1.0/n*1.0; d1=c[1]*1.0/n*1.0; d2=c[2]*1.0/n*1.0; fixed(d0,d1,d2); puts("The overall mood state distribution of whole class:"); printf("%d|d0=%.4lf\t%d|d1=%.4lf\t%d|d2=%.4lf\n",c[0],d0,c[1],d1,c[2],d2); //printf("The mood value of whole class:%.4lf\n",(c[2]*2+c[1])/n*1.0); d0=bc[0]*1.0/bn; d1=bc[1]*1.0/bn; d2=bc[2]*1.0/bn; fixed(d0,d1,d2); printf("The overall mood state distribution of %d boys:\n",bn); printf("%d|d0=%.4lf\t%d|d1=%.4lf\t%d|d2=%.4lf\n",bc[0],d0,b[1],d1,bc[2],d2); //printf("The mood value of boys:%.4lf\n",(bc[2]*2+bc[1])/bn*1.0); d0=gc[0]*1.0/gn; d1=gc[1]*1.0/gn; d2=gc[2]*1.0/gn; fixed(d0,d1,d2); printf("The overall mood state distribution of %d girls:\n",gn); printf("%d|d0=%.4lf\t%d|d1=%.4lf\t%d|d2=%.4lf\n",gc[0],d0,gc[1],d1,gc[2],d2); //printf("The mood value of girls:%.4lf\n",(gc[2]*2+gc[1])/gn*1.0); return0; } voidfixed(double& d0,double& d1,double& d2) { constdouble s=1.0; //the standard. char buf[32]; //the buffer sprintf(buf,"%.4lf %.4lf %.4lf",d0,d1,d2); sscanf(buf,"%lf%lf%lf",&d0,&d1,&d2); double t=d0+d1+d2; if(t==s)return; if(t<s)d1+=(s-t); //try to fix the bad data else d1-=(t-s); //another way to fix the bad data return; }
Please enter a statistical sample size:44 Prepare for data generation(for boy group)... ... Generating Group 1 data... ... [1|boy|(c[b[i]]=1)]:0. Generating Group 2 data... ... [2|boy|(c[b[i]]=1)]:2. Generating Group 3 data... ... [3|boy|(c[b[i]]=1)]:1. Generating Group 4 data... ... [4|boy|(c[b[i]]=2)]:0. Generating Group 5 data... ... [5|boy|(c[b[i]]=2)]:2. Generating Group 6 data... ... [6|boy|(c[b[i]]=3)]:0. Generating Group 7 data... ... [7|boy|(c[b[i]]=3)]:2. Generating Group 8 data... ... [8|boy|(c[b[i]]=2)]:1. Generating Group 9 data... ... [9|boy|(c[b[i]]=4)]:0. Generating Group 10 data... ... [10|boy|(c[b[i]]=3)]:1. Generating Group 11 data... ... [11|boy|(c[b[i]]=4)]:1. Generating Group 12 data... ... [12|boy|(c[b[i]]=4)]:2. Generating Group 13 data... ... [13|boy|(c[b[i]]=5)]:2. Generating Group 14 data... ... [14|boy|(c[b[i]]=6)]:2. Generating Group 15 data... ... [15|boy|(c[b[i]]=7)]:2. Generating Group 16 data... ... [16|boy|(c[b[i]]=5)]:0. Generating Group 17 data... ... [17|boy|(c[b[i]]=5)]:1. Generating Group 18 data... ... [18|boy|(c[b[i]]=6)]:0. Generating Group 19 data... ... [19|boy|(c[b[i]]=7)]:0. Generating Group 20 data... ... [20|boy|(c[b[i]]=6)]:1. Generating Group 21 data... ... [21|boy|(c[b[i]]=8)]:0. Generating Group 22 data... ... [22|boy|(c[b[i]]=9)]:0. Generating Group 23 data... ... [23|boy|(c[b[i]]=8)]:2. Generating Group 24 data... ... [24|boy|(c[b[i]]=7)]:1. Generating Group 25 data... ... [25|boy|(c[b[i]]=9)]:2. Generating Group 26 data... ... [26|boy|(c[b[i]]=10)]:2. Generating Group 27 data... ... [27|boy|(c[b[i]]=11)]:2. Generating Group 28 data... ... [28|boy|(c[b[i]]=10)]:0. Generating Group 29 data... ... [29|boy|(c[b[i]]=11)]:0. Generating Group 30 data... ... [30|boy|(c[b[i]]=12)]:0. Generating Group 31 data... ... [31|boy|(c[b[i]]=13)]:0. Generating Group 32 data... ... [32|boy|(c[b[i]]=14)]:0. Generating Group 33 data... ... [33|boy|(c[b[i]]=8)]:1. Generating Group 34 data... ... [34|boy|(c[b[i]]=12)]:2. Generating Group 35 data... ... [35|boy|(c[b[i]]=13)]:2. Generating Group 36 data... ... [36|boy|(c[b[i]]=9)]:1. Generating Group 37 data... ... [37|boy|(c[b[i]]=15)]:0. Generating Group 38 data... ... [38|boy|(c[b[i]]=16)]:0. Prepare for data generation(for boy group)... ... Generating Group 1 data... ... [1|girl|(c[g[i]]=10)]:1. Generating Group 2 data... ... [2|girl|(c[g[i]]=14)]:2. Generating Group 3 data... ... [3|girl|(c[g[i]]=17)]:0. Generating Group 4 data... ... [4|girl|(c[g[i]]=15)]:2. Generating Group 5 data... ... [5|girl|(c[g[i]]=18)]:0. Generating Group 6 data... ... [6|girl|(c[g[i]]=19)]:0. done. Prepare to calculate parameters... ... The overall mood state distribution of whole class: 19|d0=0.4318 10|d1=0.2273 15|d2=0.3409 The overall mood state distribution of 38 boys: 16|d0=0.4211 2|d1=0.2368 13|d2=0.3421 The overall mood state distribution of 6 girls: 3|d0=0.5000 1|d1=0.1667 2|d2=0.3333