Project 1 This is our first project for our course and it will be one that is a
ID: 3747584 • Letter: P
Question
Project 1
This is our first project for our course and it will be one that is a little easier than the following projects. We don’t know much yet and so we can’t do much in our program. This program will be focusing on selection statements, or if statements. Our later projects will continue to build on the skills practiced in our earlier projects.
Details
This project will read two doubles using cin. The numbers represent two monetary values. One value could be greater than the other, or they could be the same value. If they are not the same value, then the greater value is the amount someone paid for a purchase. The smaller value then is the purchase price. If the values are the same then it doesn’t matter which is the purchase price and which is the amount they paid, since the two values are equal.
In either case, you will subtract the purchase price from the amount they paid for the purchase. This will result in their change. You will compute the number of dollars, quarters, dimes, nickels, and pennies in their change. If any of the dollars, quarters, dimes, nickels, or pennies amount is 0, then you will not indicate an amount for that denomination.
You will use cout for your output.
Example Input
For example, you might enter:
into your program. Since the first number, 47.43, is larger this number represents how much the person paid for the item that was 40.69.
Another input might be:
in this case, since 12.95 is larger it is the amount the person paid for their purchase and 11.34 is how much is cost.
A 3rd input might be:
In this case, both values are the same so it doesn’t matter which you choose to be the purchase price and which you choose to be the cost, since they are the same.
Example Output
If you were to run your program and give it the inputs above, here would be the outputs for each set of inputs in turn.
The first line is how much their bill was, or what is the purchase cost.
The second line is the amount they paid for their bill.
The third line is the change that is due.
The following lines will indicate how many dollars, quarters, dimes, nickels, and pennies are required to make the needed change.
In this example, notice “Nickels:” is absent, that is because no nickels are needed.
Here’s the output for the second example:
The first three lines are present and mean the same things. Here you can see there are several missing denominations from the list at the bottom since there are several that have no amount for that denomination.
For the 3rd input, here’s the output:
In this case there is no list of denominations for the change since the change is 0.
Note: If any of the bill, the amount paid, or the difference ends with a 0 or 00, then you need to make sure you output the correct number of zeros. For example, 7.10 instead of 7.1 or 7.00 instead of 7. or 7. You should use a selection statement to do this.
Math
For all of this project you’ll need some basic math operations. Most notably, subtraction, -, multiplication, *, division /, and remainder, or modulus, %. The % will return the result of integer division. For example, if you divide 7 by 3, you would get 1 as the quotient, or answer, and 4 as the remainder. So if you did int x = 7 % 3; and then cout << x; you would see 4.
Requirements
All of your projects will have a list of requirements. The requirements are checked after the due date by the TAs and myself. This is were the remaining 25 points for the assignment will come from. Make sure you understand the requirements when you submit your code and make sure your code meets the requirements or you will not receive the points for the requirements.
This first requirement is how I can test your code and I’ll provide this file for this project. You must have a header filenamed change.h
This second requirement is how my code can call your code. The provided header file will do this, so for now just use the one I’ve given. You must declare a function name change with the following signature in the change.h file: void change();
You must follow the Code Style Guidelines from Canvas.
You must use a selection statement in your code. There are many opportunities to use them so this should be easy to meet.
You may not write any functions.
You may not use arrays, classes, structs, etc.
You may not use loops.
You may not work with other students on your solution. If you do so, this is an honor code violation and will treated as such.
Do not use cout for any other output other than what is described above. E.g. do not prompt for the values. If you do this your grade on Web-CAT will likely be 0.
Grading
You will submit your code to Web-CAT and Web-CAT will grade your code. It will grade it more or less immediately. Wait for it to complete and give you a grade. The grade will be out of 75 points for correctness. The remaining 25 points will be assigned based on how well you met the above requirements.
Web-CAT does not care about spacing within your output. That is to say, if I have a tab and you do not Web-CAT will not care. Web-CAT will care if you do not have spaces where I do. For example Bill: $10.00 and Bill:$10.00 are not the same. As far as Web-CAT is concerned Bill: $10.00 and Bill: $10.00 are the same. Web-CAT uses the spaces to break each line up. So 1 space or 10 it won’t matter. As long as you:
Have space where I have space
Don’t have space where I don’t have space
And what I have on a single line, you have on a single line
Submission
Zip up your change.cpp and change.h files and submit them to Web-CAT. To zip your files, in your File Explorer on Windows, select the 2 files you want to zip. Right-click while they are both selected and choose Send to -> Compressed (zipped) folder. That’s all you need to do. You do not need any 3rd party software.
When I say zip, I mean zip. Do not submit a 7zip file, or a WinRar file. Those use propriety compression and Web-CAT will not be able to unzip it. You may tar, gzip, or plain ole zip.
You get 10 submission and no more. If you use all 10, you will not get an 11th. Do not use 9 submissions and then ask me for help. If you use 1 or 2 and you don’t understand what Web-CAT is doing, please ask.
Files
I’ll post the sample files here for you to use. In addition to the code files you may use to begin your program, I’ll also give you samples of input and output files for your testing. I encourage you to try all the samples and make sure your output is correct.
change.h
change.cpp
So this is some skeleton code you can use to get started with change.cpp. Basically void change() replaces int main()
main.cpp
Since all C++ code needs a main, here’s a main you can use. It will simply call change();
Since there are 2 cpp files, to compile this you would use this command:
As long as change.cpp, change.h, and main.cpp are all in the same directory this will create change.exe. Then you can run the program using
Then you can type in the two values you want to use and your program should output the results.
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath> //so you can use round();
using std::cin;
using std::cout;
using std::endl;
using std::string;
void change();
using namespace std;
int main()
{
change();
}
void change()
{
double amount1, amount2, smaller, larger;
double diff;
double bill,paid,change_due;
double dollars,quarters,dimes,pennies,nickels;
const int PENNY = 1;
const int NICKEL = 5;
const int DIME = 10;
const int QUARTER = 25;
cin >> amount1 >> amount2;
// Step 1
// First calculate which is larger and which is smaller.
// You can make this efficient by turning it into if-else.
// I am using simple if to make it more readable.
if(amount1 > amount2) {
larger = amount1;
smaller = amount2;
}
if(amount2 > amount1){
smaller = amount1;
larger = amount2;
}
if(amount1 == amount2){
smaller = amount1;
smaller = larger;
}
// Step 2
// Calcluate Bill,paid and change due based on above calculations
bill = larger;
paid = smaller;
change_due = bill - paid;
// Step3
// Calculated Dollars.
// It is simply a truncation of double into int in c++
dollars = (int) change_due;
// Step4
// Remaining amount is in decimals, it is easy to convert it into int and then
// calculate other denominations. For instance 0.68 will be converted to 68
int remaining_amount = (change_due - dollars)*100;
// Step 5
// Calculation of remaining denominations is same
// Divide by Denomination to get required count
// Amount left to pay is remaing one.
quarters = remaining_amount/QUARTER;
remaining_amount = remaining_amount%QUARTER;
dimes = remaining_amount/DIME;
remaining_amount = remaining_amount%DIME;
nickels = remaining_amount/NICKEL;
remaining_amount = remaining_amount%NICKEL;
pennies = remaining_amount/PENNY;
// Print Details
cout << "Bill: ";
cout << larger << endl;
cout << "Paid: " ;
cout << smaller << endl;
cout << "Change Due: ";
cout << change_due << endl;
if( dollars != 0){
cout << "Dollars: ";
cout << dollars << endl;
}
if( quarters != 0){
cout << "Quarters: ";
cout << quarters << endl;
}
if(dimes!= 0){
cout << "Dimes: ";
cout<< dimes << endl;
}
if(nickels != 0){
cout << "Nickels ";
cout << nickels << endl;
}
if(pennies != 0){
cout << "Pennies: ";
cout<< pennies << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.