CPPForSchool.com: Variable, Operator & Expression Set 2: Assignment 1
1.
Write a program to swap value of two variables without using third variable.
For this assignment, my approach differs greatly when compared to the solution provided by CPPforSchool.com. While my program met all of the requirements, it did not utilize math like the example solution. Nevertheless, I will include both versions in order to compare and contrast.
Admittedly, my version was a bit "cheap" in comparison, but it did in fact accomplish a swap of sorts. Ultimately, my approach was to have the user input the first number into the first variable, then copy it over to the second variable. From there, the user would input the second number into the first variable, thus overwriting the original input.
Here is what the user would experience. Perhaps this was not ideal, but it was the first solution that came to mind.
Nevertheless, I reviewed the code provided by CPPforSchool.com and quickly discovered that it would have been more accurate to utilize Algebra. For the sake of continuity and learning from mistakes, I kept the original code and simply commented it out. Right below that is code that was inspired by the sample solution.
Below is the result. In a word, success!
For this assignment, my approach differs greatly when compared to the solution provided by CPPforSchool.com. While my program met all of the requirements, it did not utilize math like the example solution. Nevertheless, I will include both versions in order to compare and contrast.
Admittedly, my version was a bit "cheap" in comparison, but it did in fact accomplish a swap of sorts. Ultimately, my approach was to have the user input the first number into the first variable, then copy it over to the second variable. From there, the user would input the second number into the first variable, thus overwriting the original input.
Here is what the user would experience. Perhaps this was not ideal, but it was the first solution that came to mind.
Nevertheless, I reviewed the code provided by CPPforSchool.com and quickly discovered that it would have been more accurate to utilize Algebra. For the sake of continuity and learning from mistakes, I kept the original code and simply commented it out. Right below that is code that was inspired by the sample solution.
Below is the result. In a word, success!
Comments
Post a Comment