I am currently learning C++ following "Programming Principles and Practice" (I absolutely love the book so far) and have a bit of a query about some code I've written.
The aim of the code (as described by the exercise in the textbook, is to get the user to input two integers, and then output the smallest, the largest, the sum, the difference, the product and the ratio (assuming by simple division, don't really care to display as a 1:1 type ratio, a fraction will do).
It's a very simple code and I've implemented it in two ways and both work fine. But I am curious to know, which one of the two ways is better optimised, or better practice for when writing much more complex programs. I know for such a small programs, the difference is probably negligible, but I just wanted to know if there ever is a preference with these sorts of things.
Type 1:
Introduce the two integers, and write a set of if statements conditioned to each value being larger than the other. Then writing a set of actions with respect to the larger value for each condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int main()
{
cout << "Please enter two integer values: ";
int val1, val2, val3=0;
cin >> val1 >> val2;
if (val1 > val2)
cout << "\nSmaller Value: " << val2
<< "\nLarger Value: " << val1
<< "\nSum: " << val2 + val1
<< "\nDifference: " << val1 - val2
<< "\nProduct: " << val2*val1
<< "\nRatio: " << val1/val2 << "\n\n";
if (val2 > val1)
cout << "\nSmaller Value: " << val1
<< "\nLarger Value: " << val2
<< "\nSum: " << val2 + val1
<< "\nDifference: " << val2 - val1
<< "\nProduct: " << val2*val1
<< "\nRatio: " << val2/val1 << "\n\n";
}
|
Possible Benefits: Doesn't introduce a new variable.
Possible Drawbacks: Larger amount of code, a lot of repetition for each if statement.
-----------------------------------------------------------------------------
Type 2:
Introduce the two integers, and rearrange so the second value is always the largest by introducing a third variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main()
{
cout << "Please enter two integer values: ";
int val1, val2, val3=0;
cin >> val1 >> val2;
if (val1 > val2)
val3 = val2;
val2 = val1;
val1 = val3;
cout << "\nSmaller Value: " << val1
<< "\nLarger Value: " << val2
<< "\nSum: " << val2 + val1
<< "\nDifference: " << val2 - val1
<< "\nProduct: " << val2*val1
<< "\nRatio: " << val2/val1 << "\n\n";
}
|
Possible Benefits: Much shorter code, only one set of instructions.
Possible Drawbacks: Introduces an additional variable.
-------------------------------------------------------------------------
The main question is...
Which one of these methods is preferred from an efficiency and simplicity point of view if both can achieve the answer? (Though to be honest, I won't be surprised if you turned round and said that one method is efficient, and the other is simplistic)
Is it better to use repetition through IF statements and use less variables, or have shorter code and use more variables to express functions (assuming having more variables takes up more memory??). Imagine if my problem was a lot more convoluted, (which in truth would probably require both). I've yet to learn about proper programming techniques, and the effect of adding extra variables to a program, so apologies if my question seems a bit ignorant (but hence why I am asking).
Thanks.