Problem 3 Solution: Maximum Number With A Do-While Loop

The do-while loop is a test at the bottom loop and is useful for cases where a value used in the loop test is entered or calculated in the loop body. Compared to the while-loop version, the do-while version is smaller because it requires one less prompt and read. Overall, the following code is clean and straightforward.

int max3()
{
	int	max = -1;
	int	number;

	do
	{
		cout << "Please enter a number: ";
		cin >> number;

		if (number > max)
			max = number;

	} while (number >= 0);

	return max;
}
max3: do-while-loop finding the largest entered number. This solution requires the program to initialize max (the largest number entered so far) before the loop's beginning. The problem specifies that all data will be non-negative, so it can initialize max to -1, which is less than all valid input. But the program only requires one prompt and read in the loop body, initializing number before using it in the loop test and the if statement.