int product;
product = num_1 * num_2 * num_3;
cout << "The product is " << product << end;
sum
that gets
the sum of the three numbers, and then to do something like
this:
cout << "The sum is " << sum << endl;
cout << "The average is " << sum / 3 << endl;
The reason is that almost any compiler will recognize that the sum of
the three numbers is needed twice and will generate the code to
calculate the sum just once and preserve the value to use in the second
statement automatically. Thus, avoiding the variable sum
gives your reader less code to read while still making the meaning of
the program clear, and it produces an equally efficient program.
Computing the minimum and maximum values can be done in a number of
ways. This is probably the simplest one, because it avoids the need for
extra variables:
cout << "The smallest is "; if ((num_1 < num_2) && (num_1 < num_3)) cout << num_1 << endl; if ((num_2 < num_1) && (num_2 < num_3)) cout << num_2 << endl; if ((num_3 < num_1) && (num_3 < num_2)) cout << num_3 << endl;Unfortunately, you wouldn't know about "
&&
" when
you finished reading Chapter 1, so you might use auxiliary variables for
the minimum and maximum, like this:
int minimum = num_1; if (num_2 < minimum) minimum = num_2; if (num_3 < minimum) minimum = num_3; cout << "Minimum is " << minimum << endl;