- ベストアンサー
大学の課題です。
C言語のプログラミングIの問題です。 問:n人の成績を入力し、最高点、最低点、合計点、平均点、分散を求めるプログラムを作成せよ。 わかる方是非お願いします。 #include <stdio.h> int main(void) から始めて下さい
- みんなの回答 (3)
- 専門家の回答
質問者が選んだベストアンサー
#include <stdio.h> int atoi( const char* string ) { int num = 0; while( '0' <= *string && '9' >= *string ) { num *= 10; num += *string - '0'; ++string; } return num; } int bestScore( int scores[], int scoreCount ) { int best = 0; for( int ix = 0; ix < scoreCount; ix++ ) { if( !ix ) best = scores[ ix ]; if( best < scores[ ix ] ) best = scores[ ix ]; } return best; } int worstScore( int scores[], int scoreCount ) { int worst = 0; for( int ix = 0; ix < scoreCount; ix++ ) { if( !ix ) worst = scores[ ix ]; if( worst > scores[ ix ] ) worst = scores[ ix ]; } return worst; } int totalScore( int scores[], int scoreCount ) { int total = 0; for( int ix = 0; ix < scoreCount; ix++ ) { total += scores[ ix ]; } return total; } double bunsanScore( int scores[], int scoreCount, double average ) { double total = 0; for( int ix = 0; ix < scoreCount; ix++ ) { double num = scores[ ix ] - average; total += ( num * num ); } return total / scoreCount; } int _tmain() { int scoreCount = 0; int scores[ 100 ] = { 0 }; printf( "点数を入力してください(最大100個)\n" ); printf( "終了する場合は[Q]を入力してください\n" ); while( 1 ) { char istr[ 32 ]; int result = scanf( "%s", &istr ); if( 'q' == istr[ 0 ] || 'Q' == istr[ 0 ] ) { break; } scores[ scoreCount ] = atoi( istr ); if( ++scoreCount >= 100 ) { break; } } if( 0 < scoreCount ) { int best = bestScore( scores, scoreCount ); int worst = worstScore( scores, scoreCount ); int total = totalScore( scores, scoreCount ); double average = total / scoreCount; double bunsan = bunsanScore( scores, scoreCount, average ); printf( "最高点=%d\n", best ); printf( "最低点=%d\n", worst ); printf( "合計点=%d\n", total ); printf( "平均点=%.2f\n", average ); printf( "分散=%.2f\n", bunsan ); } return 0; }