C程式 為什麼算不出標準差???

#include <stdio.h>

#include <stdarg.h>

#include <math.h>

int main(void)

{

int number; /* account number */

int i;

double score[100]; /* account score */

double total;

double average;

double S;

double standard_deviation;

FILE *cfPtr; /* cfPtr = clients.dat file pointer */

/* fopen opens file; exits program if file cannot be opened */

if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {

printf( "File could not be opened\n" );

} /* end if */

else {

printf("讀取到的學生成績資料如下:\n\n");

printf( "%s%6s\n\n", "學生座號", "成績" );

for (i=0;i<number;i++) {

fscanf( cfPtr, "%d%lf", &number, &score[i] );

total = score[i];

/* while not end of file */

while ( !feof( cfPtr ) &&i<number ) {

printf( "%5d%10.2f\n", number, score[i] );

fscanf( cfPtr, "%d%lf", &number, &score[i] );

total += score[i];

} /* end while */

total = total-score[i];

printf("\n");

average = total / number;

printf("以上%d位同學的總分是 %.2f分\n", number, total);

printf("以上%d位同學的平均是 %.2f分\n", number, average);

for (i=0;i<number;i++) {

S += (score[i]-average)*(score[i]-average);

standard_deviation = sqrt(S/number);

}

printf("以上%d位同學的標準偏差是 %.2f分\n\n", number, standard_deviation);

}

fclose( cfPtr ); /* fclose closes the file */

} /* end else */

return 0;

}

*******************************************************

1 50.00

2 60.00

3 70.00

4 80.00

5 90.00

*******************************************************

1 50.00

2 60.00

3 70.00

4 80.00

5 90.00

*******************************************************

3Q

*******************************************************

Update:

Update 2:

dtsien 不好意思喔

Update 3:

Update 4:

YA 經過了這麼多次的修修改改 我終於成功囉 真是非常謝謝你

( 我發現你貼的程式+號都不見了 @@ 好奇怪喔 )

Update 5:

#include

#include

#include

int main(void)

{

Update 6:

int number; /* account number */

int i;

int counter; //計算有幾筆資料用的計數器

double score[100]; /* account score */

double total;

double average;

double S;

double standard_deviation;

Update 7:

FILE *cfPtr; /* cfPtr = clients.dat file pointer */

/* fopen opens file; exits program if file cannot be opened */

if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL )

{

printf( "File could not be opened\n" );

} /* end if */

Update 8:

else

{

printf("讀取到的學生成績資料如下:\n\n");

printf( "%s%6s\n\n", "學生座號", "成績" );

counter = 0;

Update 9:

while( true )

{

fscanf( cfPtr, "%d%lf", &number, &score[counter] );

if(feof( cfPtr )) // 如果讀到檔案結尾，就結束了

break;

printf( "%5d%10.2f\n", number, score[counter] );

total += score[counter];

counter++; // 每讀一筆就加一次

}

fclose( cfPtr ); /* fclose closes the file */

Update 10:

average = total / counter;

printf( "\n" );

printf("以上%d位同學的總分是 %.2f分\n", counter, total);

printf("以上%d位同學的平均是 %.2f分\n", counter, average);

for (i=0;i

Update 11:

printf("以上%d位同學的標準偏差是 %.2f分\n\n", counter, standard_deviation);

} /* end else */

return 0;

}

Update 12:

= = Yahoo 知識真的會改掉貼上去的內容耶

#include

#include

#include

for (i=0;i

Update 13:

@@ 為什麼後面的字無法顯現出來

Update 14:

dtsien 不好意思 上次說你可能寫錯的應該是Yahoo 知識的問題

Rating
• Heresy
Lv 7

恩…你的這個程式邏輯滿詭異的…

首先是讀取檔案的部分。

你在外面先用了一個 for (i=0;i<number;i ) 想要來處理數量為 number 的輸入？但是實際上，你並沒有給予 number 一個數值；在這個情況下，其實程式有可能直接在這邊就結束了，不會繼續跑。

而實際在透過 while 讀取的時候，number 又顯得多餘了…或者應該說，不應該這樣使用。你在程式中，是把 number 拿來讀取學號，但是又把它拿來當計數器（記錄有幾筆資料）；如果學浩不是 1~n 的話，會有很大的問題的。

修改後的結果如下：#include <stdio.h>

#include <stdarg.h>

#include <math.h>

int main(void)

{

int number; /* account number */

int i;

int counter; //計算有幾筆資料用的計數器

double score[100]; /* account score */

double total;

double average;

double S;

double standard_deviation;

FILE *cfPtr; /* cfPtr = clients.dat file pointer */

/* fopen opens file; exits program if file cannot be opened */

if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL )

{

printf( "File could not be opened\n" );

} /* end if */

else

{

printf("讀取到的學生成績資料如下:\n\n");

printf( "%s%6s\n\n", "學生座號", "成績" );

counter = 0;

while( true )

{

fscanf( cfPtr, "%d%lf", &number, &score[counter] );

if(feof( cfPtr )) // 如果讀到檔案結尾，就結束了

break;

printf( "].2f\n", number, score[counter] );

total = score[counter];

counter; // 每讀一筆就加一次

}

fclose( cfPtr ); /* fclose closes the file */

average = total / counter;

printf( "\n" );

printf("以上%d位同學的總分是 %.2f分\n", counter, total);

printf("以上%d位同學的平均是 %.2f分\n", counter, average);

for( i = 0; i < counter; i )

{

S = (score[i]-average)*(score[i]-average);

standard_deviation = sqrt(S/number);

}

printf("以上%d位同學的標準偏差是 %.2f分\n\n", number, standard_deviation);

} /* end else */

return 0;

}

2008-04-16 07:58:29 補充：

可能是貼錯了 @@

printf( "%5d%10.2f&#92;n", number, score[counter] );

2008-04-16 07:58:51 補充：

還是其實是被 Yahoo 知識改掉了啊？ @@

2008-04-16 12:58:08 補充：

Yahoo 會把小於符號當成 HTML 的標籤 <

• dtsien
Lv 6

#include < stdio.h >

#include < math.h >

int main(void)

{

int number; /* account number */

int i;

double score[100]; /* account score */

double total=0.0;

double average;

double S=0.0;

double standard_deviation;

FILE *cfPtr; /* cfPtr = clients.dat file pointer */

/* fopen opens file; exits program if file cannot be opened */

if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {

printf( "File could not be opened\n" );

} /* end if */

else {

printf("讀取到的學生成績資料如下:\n\n");

printf( "%s%6s\n\n", "學生座號", "成績" );

/* while not end of file */

while ( 1 ) {

fscanf( cfPtr, "%d", &number );

if (feof( cfPtr )) break;

fscanf( cfPtr, "%lf", &score[number] );

printf( "].2f\n", number, score[number] );

total = score[number];

} /* end while */

printf("\n");

average = total / number;

printf("以上%d位同學的總分是 %.2f分\n", number, total);

printf("以上%d位同學的平均是 %.2f分\n", number, average);

for (i=1;i<=number;i ) {

S = (score[i]-average)*(score[i]-average);

}

standard_deviation = sqrt(S/number);

printf("以上%d位同學的標準偏差是 %.2f分\n\n", number, standard_deviation);

}

fclose( cfPtr ); /* fclose closes the file */

/* end else */

system("pause");

return 0;

}

學習程式設計的方法技巧或經驗:

多想多做如此而已