1: // C++Average.cpp 2: // 3: // Sample program in the C++ programming language. 4: // 5: // Calculates the average of positive integers entered by the user. 6: // 7: // Author: Mark Young 8: // Date: 2007-05-03 10: #include <iostream> 11: using namespace std; 13: int main() { 14: // Identify myself... 15: cout << "\n\n\n\n\n\n\n\n\n\n" 16: << "Sample Program (C++)\n" 17: << "====================\n" 18: << "\nFinds the average of positive integers entered by the user.\n" 19: << "Enter a negative number to stop input.\n\n\n\n"; 21: // Declare and initialize variables 22: int sum = 0; 23: int count = 0; 24: int n; 26: // Get the first number from the user 27: cout << "Enter a number: "; 28: cin >> n; 30: // keep going until you get a negative number 31: while (n >= 0) { 32: // Add the last number to the total 33: sum += n; 35: // add one to the count 36: count++; 38: // get the next number 39: cout << "Enter a number: "; 40: cin >> n; 41: } 43: // Print the result 44: cout << "\n\nAverage = " << ((double)sum / (double)count) << "\n\n"; 46: return 0; 47: }