아직도 고1때 작성한 소스파일이 남아있었을 줄이야....................ㅋㅁㅋ;;
옛날 기억이 새록새록~>ㅁ<
( 고1때 직접 작성한 것이기에, 영어 오타나 오기[誤記]가 있을지도 모릅니다;; )
// Program 2 : Read(Input) values and Calculate Average
// sentinel value is -1
#include <stdio.h>
// function main begins program execution
int main ( void ) {
// define variables
int i; // input count
int value;
int sum;
float average; // average is floating-point number
// initialize
i = 0 ;
sum = 0 ;
// program introduction
printf("*******************************************************************************\n");
printf("Program Introduction\n");
printf("*******************************************************************************\n");
printf("This program calculate integers average.\n");
printf("Do not enter zero or negative value. (Use only positive integer.)\n");
printf("If you enter value for -1, the input phase is end\n");
printf("and result(average) will be come out.\n");
printf("*******************************************************************************\n\n");
// first read value
printf("Input Value : ");
scanf("%d", &value);
// sentinel value is -1 >>> if value is -1, end input
// error case 1 >>> if value is -1 and i is 0, print error message 1
// error case 2 >>> if value is not legitimate (zero or negative except -1), print error message 2
while ( value >= 0 ) {
sum += value ; // add value to sum
++i ; // increment count
// next read value
printf("Input Value : ");
scanf("%d", &value);
} // end while
/********************************* termination conduct *********************************/
/***** error case 1 *****/
if ( ( value == -1 ) && ( i == 0 ) ) {
printf("\n[ERROR 1] You didn't input value\n"); // print error message 1
} // end if
/***** error case 2 *****/
else if ( value != -1 ) {
printf("\n[ERROR 2] Input Error - Don't input negative or zero\n"); // print error message 2
} // end else if
/***** no error *****/
else {
// calculate average
average = (float)sum / i ;
// print result(average) - precision is .2
printf("\nAverage is %.2f\n\n", average);
} // end else
return 0; // terminate program successfully
}