Question: Homework 1: Structure of a C++ program, reference variables Create a hw branch. Create three files. compare.h should contain the declaration of three functions: whichIsBigger
Homework 1: Structure of a C++ program, reference variables
Create a hw branch.
Create three files.
-
compare.h should contain the declaration of three functions:
- whichIsBigger should take two ints, x and y, and a reference to a bool called same. It should return the larger of the two ints, and set same to true if they were the same and false otherwise.
- makeBothSame should take an int, x, and an int reference, y. It should set y to x's value and return y's former value.
- makeFirstBigger should take two int references, x and y, and a bool reference, swapped. It should compare them and if necessary swap them to make x bigger than y. It should set swap to true if x was originally smaller than y, and false otherwise.
- Make sure to use appropriate header guards.
-
compare.cpp should contain the implementation of those three functions.
-
main.cpp should use the three functions as follows:
- The main function should read two ints from the command line using the atoi function from the cstdliblibrary.
- main itself should never directly compare the two integers. All comparisons and modifications must be done through the three functions defined above.
- Use whichIsBigger to either print "[x/y] is larger" or "x and y are the same". (Choose x or y as appropriate.)
- Use makeFirstBigger to either print "x's new value is [] and y's new value is []" or "x and y were not swapped because they were already in the right order." (Replace the brackets with the correct values.)
- If x and y are not the same (again, do not compare them in main), use makeBothSame and print: "Now x and y both equal []. Previously, y's value was []." (Replace the brackets with the correct values.)
Follow the homework submission process outlined in Homework 0.
Your code must compile and must appropriately implement the elements above. Comment each function declaration and definition. Try to make your code elegant: readable and sparse.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
