/* File: bar3.c Print a bar graph with the help of functions. */ #include void PrintAsterisks(int t) ; int GetPositiveInteger() ; int main() { int max, n, count ; double sum ; max = -1 ; count = 0 ; sum = 0.0 ; while (1) { n = GetPositiveInteger() ; if (n < 0) break ; PrintAsterisks(n) ; if (n > max) max = n ; count++ ; sum = sum + n ; } printf("Sum = %g, count = %d\n", sum, count) ; if (max > 0) { printf("Largest number found = %d\n", max) ; printf("Average number found = %g\n", sum / count) ; } else { printf("No numbers were entered\n") ; } return 0 ; } // Ask the user to enter a positive number. // Keep bugging the user until he enters // a positive number or a non-numerical entry. // // Returns positive number entered or -1 to // indicate a non-numerical entry. int GetPositiveInteger() { int m, r ; while (1) { printf("Enter a positive integer (type 'quit' to end): ") ; r = scanf("%d", &m) ; if (r <= 0) return -1 ; if (m > 0) return m ; printf("Your entry %d is not a positive number!\n", m) ; } } // Print t asterisks followed by a newline void PrintAsterisks(int t) { int i ; for (i = 1 ; i <= t ; i++) { printf("*") ; } printf("\n") ; }