如題:
1.mode_num() has a bug when the number is not only one
2.希望能
input 4.5 3.4 5.7 4.5 8.5 10.3 3.4 4.5 5.7 8.3 0.1 n(enter)
output 0.1 3.4 3.4 4.5 4.5 4.5 5.7 5.7 8.3 8.5 10.3(換行)
Please help me debug....
#include <math.h>
#include <stdio.h>
#include <iostream.h>
long double functions(long double [],int);
long double mode_num(long double [],int);
long double scroes[255]={0.0},sum=0.0,sum_of_sqr_scroes=0.0,mean=0.0,mode=0.0
,median=0.0,variance=0.0,st_dv=0.0;
int i=0,j=0;
/*
宣告 : functions & mode_num to find Sum,Mean,Mode,Median,Variance
,Standard Deviation;
一 255個數之陣列空間scores[](並且歸零);
整數 i,j來跑迴圈;
*/
void main(void)
/*
主程式 main()執行概要 :
宣告 : 數量num,緩衝區temp;
輸入 : 數量num,數列scroes[];
螢幕輸出原數列;
重新排序(由小到大);
螢幕輸出新數列;
執行 : functions & mode_num 計算;
螢幕輸出的編排;
*/
{
int num=0;
long double temp=0.0;
cout <<"\n\n How many numbers do you input ? : ";
cin >>num;
cout <<"\n Number input : ";
for(i=0;i<255;i++)
//數列輸入迴圈;當 num圈或 num>=255時跳出;
{
if(i==num||num>=255)
{
break;
}
cin >>scroes[i];
}
printf("\n\n the inputed numbers:\n");
for(i=0;i<num;i++)
//螢幕輸出原數列;
{
printf("%8g",scroes[i]);
}
for(i=0;i<(num-1);i++)
for(j=0;j<(num-1);j++)
//重新排序(由小到大);
{
(scroes[j]<scroes[j+1])?(temp=scroes[j])
:(temp=scroes[j+1],scroes[j+1]=scroes[j],scroes[j]=temp);
}
printf("\n the new ones :\n");
for(i=0;i<num;i++)
//螢幕輸出新數列;
{
printf("%8g",scroes[i]);
}
functions(scroes,num);
mode_num(scroes,num);
//執行 : functions & mode_num 計算;
printf("\n\n\n%9s %7s\n%5s %10s\n","Number","Sum","of","of");
printf("%9s %10s %10s","Scroes","Scroes","Mean ");
printf(" %10s %10s %10s %10s","Median","Mode ","Var. ","St.Dv.");
printf("\n -------- -------- -------- --------");
printf(" -------- -------- --------");
printf("\n%9d %10g %10.2lf %10.2lf",num,sum,mean,median);
printf(" %10g %10.2lf %10.2lf\n\n",mode,variance,st_dv);
//螢幕輸出的編排;
}
long double functions(long double scroes[],int num)
/*
functions()函式執行概要 :
傳入 : 數列 scroes[],數量 num;
利用迴圈計算 : 各項和 sum,各項平方和 sum_of_sqr_scroes;
判斷中位數 Median : 奇數項與偶數項;
計算 : 變異 variance,標準偏差(standard deviation) st_dv,平均值 mean;
傳回 : sum,mean,median,variance,st_dv;
*/
{
for(i=0;i<num;i++)
//計算 : 各項和 sum,各項平方和 sum_of_sqr_scroes;
{
sum+=scroes[i];
sum_of_sqr_scroes+=scroes[i]*scroes[i];
}
//判斷中位數 : 奇數項與偶數項有不同的作法;
(num%2==1)?(median=scroes[(num-2)/2])
:(median=(scroes[(num-2)/2]+scroes[num/2])/2);
//計算 : 變異 variance,標準偏差(standard deviation) st_dv,平均值 mean;
variance=(sum_of_sqr_scroes-sum*sum/num)/(num-1);
st_dv=pow(variance,0.5);
mean=sum/num;
return sum,mean,median,variance,st_dv;
//傳回 : sum,mean,median,variance,st_dv;
}
long double mode_num(long double scroes[],int num)
/*
functions()函式執行概要 :
傳入 : 數列 scroes[],數量 num;
宣告 : 緩衝陣列 a[] & b[](並且歸零),緩衝區 temp;
利用迴圈 : 比較 scroes[num]每一項,若是有同值,則對應 a[num]就 +1;
使 a[num]==b[[num];
重新排序 b[num](由小到大),則 b[num-1]為最大次數;
比較 a[num]中,找出 a[]==b[num-1],使 temp等於該項次;
令 mode=scroes[temp];
傳回 : 最多值 mode;
*/
{
int a[255]={0},b[255]={0},temp=0;
for(i=0;i<num;i++)
for(j=0;j<num;j++)
//比較 scroes[num]每一項,若是有同值,則對應 a[i]就 +1;
{
if(scroes[i]==scroes[j])
a[i]++;
}
for(i=0;i<num;i++)
//使 a[]=b[[];
{
b[i]=a[i];
}
for(i=0;i<(num-1);i++)
for(j=0;j<(num-1);j++)
//重新排序 b[num](由小到大);
{
(b[j]<b[j+1])?(temp=b[j]):(temp=b[j+1],b[j+1]=b[j],b[j]=temp);
}
for(i=0;i<num;i++)
//比較 a[num]中,找出 a[]==b[num-1],使 temp等於該項次;
{
if(a[i]==b[num-1])
temp=i;
}
mode=scroes[temp];
return mode;
//傳回 : 最多值 mode=scroes[temp];
//bug 1.
}
/*
結果 :
How many numbers do you input ? : 11
//bug 2.
Number input : 4.5 3.4 5.7 4.5 8.5 10.3 3.4 4.5 5.7 8.3 0.1
the inputed numbers:
4.5 3.4 5.7 4.5 8.5 10.3 3.4 4.5 5.7 8.3
0.1
the new ones :
0.1 3.4 3.4 4.5 4.5 4.5 5.7 5.7 8.3 8.5
10.3
Number Sum
of of
Scroes Scroes Mean Median Mode Var. St.Dv.
-------- -------- -------- -------- -------- -------- --------
11 58.9 5.35 4.50 4.5 8.07 2.84
*/
--
一段動人的相遇
醉人的偶遇
你我都是相互的生命樂章上
一段優美的音節
一篇永恆的曲譜
@Jack E-mail:s89...@cc04.ccit.edu.tw
[m [1;34m※ 來源:台北科技大學紅樓資訊站 bbs.cc.ntut.edu.tw‧[FROM: a003.we.ccit.edu.tw] [m