Calculate amount of coins - c++

I have the assignment from ZyLab
Given six values representing counts of silver dollars, half dollars, quarters, dimes, nickels, and pennies, output the total amount as dollars and cents. The variable totalAmount is used to represent the total amount of money.
Output each floating-point value with two digits after the decimal point, which can be achieved by executing results once before all other cout statements.
Ex: If the input is: 5 3 4 3 2 1
where 5 is the number of silver dollars, 3 is the number of half-dollars, 4 is the number of quarters, 3 is the number of dimes, 2 is the number of nickels, and 1 is the number of pennies, the output is: Amount: $5.66
For simplicity, assume input is non-negative.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double totalAmount;
int dollars;
int halfDollars;
int quarters;
int dimes;
int nickels;
int pennies;
/* Type your code here. */
/* get input */
cin >> dollars;
cin >> halfDollars;
cin >> quarters;
cin >> dimes;
cin >> nickels;
cin >> pennies;
/* calculate totalAmount */
cout << fixed << setprecision(2);
halfDollars = halfDollars * 0.5;
quarters = quarters * 0.25;
dimes = dimes * 0.10;
nickels = nickels * 0.05;
pennies = pennies * 0.01;
totalAmount = dollars + halfDollars + quarters + dimes + nickels + pennies;
/* output results */
cout << "Amount: " << totalAmount << endl;
return 0;
}
What is wrong?

Your variables should be of type float.
Rewrite your code as following:
float dollars;
float halfDollars;
float quarters;
float dimes;
float nickels;
float pennies;
When you are converting for example half dollars, 3 half dollars * 0.5 is 1.5, but it can't be stored inside integer variable, so you need the variable to be of type float.
So when compiler is executing this piece of code;
/* calculate totalAmount */
cout << fixed << setprecision(2);
halfDollars = halfDollars * 0.5;
quarters = quarters * 0.25;
dimes = dimes * 0.10;
nickels = nickels * 0.05;
pennies = pennies * 0.01;
it stores floating point value inside an integer declared variable, so compiler naturally casts it to integer type, producing incorrect output.

Related

How to split a Pounds decimal value correctly in C++ to Pounds and Ounces?

Following are the two functions I have written to convert Kg to Pounds and vice versa. I am calling these two from my main() using a switch case.
void poundToKg(double pounds, double ounces) {
pounds = pounds + (ounces / 16);
cout << "Pound to KG : " << pounds * 0.453592 << " Kgs" << endl;
}
void kgToPound(double kilograms, double grams) {
kilograms = kilograms + (grams / 1000);
cout << "KG to Pound : " << kilograms * 2.20462 << " Pounds" << endl;
}
For example, if I want to do a KG to Pounds conversion, I enter:
Kg : 300
Grams : 100
My Output : 661.606 Pounds
I want to print the output separately in kilograms and grams OR pounds and ounces.
I wanted to print something like x Pounds and y Ounces with 661.606 or any example.
I was thinking of separating like 661 Pounds and 0.606 Ounces. Is that the correct approach/solution?
Note: The input will be two values, input 1 - Weight in Kg or Pounds
input 2 - Grams or Ounces.
For the pounds, you can cast your double to an int :
std::cout << (int)x;
For the ounces, you can do something like :
std::cout << x - (int)x;
Considering 1 lbs = 16 oz, you need to get the fraction part and multiply by 16:
#include <cmath>
#include <cstdio>
void kg_to_lbs(double const kg) {
double total_lbs = 2.205 * kg;
double lbs;
double oz = std::modf(total_lbs, &lbs) * 16;
std::printf("%.0f lbs and %.2f oz\n", lbs, oz);
}
int main() {
kg_to_lbs(300.1);
}
This prints "661 lbs and 11.53 oz".

What's wrong with this simple piece of code? [duplicate]

This question already has answers here:
Why not use Double or Float to represent currency?
(16 answers)
Closed 3 years ago.
The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.
#include <iostream>
using namespace std;
int main()
{
int quarter, dime, nickle;
int result;
quarter = 25;
dime = 10;
nickle = 5;
int numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);
cout << "The total amount of pennies is: " << result;
return 0;
}
I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies
The output is 102
Edit: Code Fixed and working now!
Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...
#include <iostream>
using namespace std;
int main()
{
float quarter, dime, nickle, penny;
float result;
quarter = 0.25;
dime = 0.10;
nickle = 0.05;
float numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);
cout << "The total amount of pennies is: " << result * 100;
return 0;
}
Oh, and just like what #Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.

C++ coins conversion hw [duplicate]

This question already has answers here:
c++ integer division
(3 answers)
Closed 3 years ago.
The last part of my code doesn't seem to run
#include <iostream>
#include <cmath>
int main() {
int dollars;
int cents;
std::cout << "Please enter your amount in the format of dollars and cents separated by a space: ";
std::cin >> dollars >> cents;
double quarters = dollars/0.25;
int whole_quarters = (int) quarters;
double dimes = cents/10;
int whole_dimes = (int) dimes;
double nickels = (dimes - whole_dimes)/0.5;
int whole_nickels = (int) nickels;
int pennies = std::fmod((dimes - whole_dimes),0.5);
std::cout << dollars << " dollars and " << cents << " cents are:\n";
std::cout << whole_quarters << " quarter " << whole_dimes << " dimes " << whole_nickels << " nickels " << pennies << " pennies ";
return 0;
}
I typed in 2 58 but the output was 8 quarters 5 dimes 0 nickels 0 pennies
It should be 1 nickels and 3 pennies
Can someone tell me what am i missing?
A better answer would be 10 quarters, 1 nickle, 3 pennies. Be that as it may.
I wouldn't have used this particular algorithm, although scanning it isn't telling me the obvious problem. I would debug by dumping out dimes (the double) and verify it's what you think it is, and then nickles (the double) and make sure that value makes sense, too.
I would do something like this:
int dimes = cents / 10;
int nickles = (cents % 10) / 5;
int pennies = (cents % 5);
If cents is 58, then dimes = 5, cents % 10 is 8, divided by 5 is 1, and cents % 5 is 3.
But it's worthwhile, if you're serious about programming, to put a lot of cout statements into your code and make sure values are becoming what you think they are.
Also, this will be faster if instead of asking for input, you comment out that code and hardcode your test data. Once you're getting proper test results that way, then switch back to asking for input.

C++ Students score average calculator array problem

I am trying to write a program that calculates students last scores as a midterm, quiz1, quiz2 and final then finds their class average depends on a number of students. Program will show the class average. User should enter the scores. I am new to c++, and my problem is right now i can't find a way to connect this for loop to an array for class average. Is my code wrong for this? I don't know what to do.
#include <iostream>
using namespace std;
int main()
{
int mt, q1, q2, fnl, stdn, num;
double cls[5], std, avg;
cout << "Enter a number of students: ";
cin >> num;
for (stdn=0; stdn<num; stdn++) {
cout<<"Enter mt, q1, q2, fnl of a "<<stdn+1<<". student in order:"<<endl;
cin>>mt>>q1>>q2>>fnl;
std = mt * 30/100 + q1 * 10/100 + q2 * 10/100 + fnl * 50/100;
cout<<stdn+1<<". students total score is "<<std<<endl;
}
}
Type int is always rounding down the value after a decimal point.
So, (int)3.84 == 3, and therefore your std variable will probably have a wrong value.
Define all variables as double for start. To calculate avg simply add marks then devide by the number of students in the end.
double mt, q1, q2, fnl, stdn, num, grades_sum = 0, avg;
...
for(stdn=0; stdn<num; stdn++){
...
grades_sum += mt * 30/100 + q1 * 10/100 + q2 * 10/100 + fnl * 50/100;
...
}
avg = grades_sum/num;
You don't need an array for class average. Just add up all the students scores and divide by the number of students. That just needs one variable (I've called it std_sum) not an array. Like this
double std_sum = 0.0;
for(stdn=0; stdn<num; stdn++){
...
std = mt * 30/100 + q1 * 10/100 + q2 * 10/100 + fnl * 50/100;
std_sum = std_sum + std; // add up all student scores
}
avg = std_total/num;
You can add up all of the students scores, then at the end divide by the total number of students. This will give you a total average of the class. Also, I avoided integer division by replacing your fractions with decimals that add up to 1. I also edited your for loop to start at 1 and go to the number, to avoid adding 1 to everything.
#include <iostream>
using namespace std;
int main()
{
int mt, q1, q2, fnl, stdn, num;
double cls[5], std;
double classAvg = 0; // add a new variable
cout << "Enter a number of students: ";
cin >> num;
for (stdn=1; stdn <= num; stdn++) {
cout << "Enter mt, q1, q2, fnl of a " << stdn << ". student in order:" << endl;
cin >> mt >> q1 >> q2 >> fnl;
std = (mt * 0.3) + (q1 * 0.1) + (q2 * 0.1) + (fnl * 0.5);
cout << stdn << ". students total score is " << std << endl;
classAvg = classAvg + std; // start adding the totals of all the students
}
avg = classAvg/num; // find the total average by dividing by total students
cout << "Class Average is " << classAvg << endl; // display average.
}

Get double as a result of division of integer values

If a person has low income (<= 25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.
For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.
(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 * 0.40 * (45 / 60) = $21.00.)
Write a program in C++ that prompts the user to enter the hourly rate, the total consulting time, and whether the person has low income. The program should output the billing amount.
This is what I have done so far and the program looks correct to me, but for some reason I do not get the correct output. I get 0.00 which is a wrong output value.
#include<iostream>
#include <iomanip>
using namespace std;
double calculateBill(int income, int consultingMinutes, double hourlyRate);
int main()
{
int income;
double consultingMinutes;
double hourlyRate;
cout << "Please enter the clients income: $" ;
cin >> income;
cout << "Please enter the consulting time in minutes: ";
cin >> consultingMinutes;
cout << "Please enter the hourly rate: $";
cin >> hourlyRate; cout << fixed << showpoint << setprecision(2);
cout << "Your total bill ammount comes to: $" << calculateBill(income, consultingMinutes, hourlyRate) << endl;
return 0;
}
double calculateBill(int income, int consultingMinutes, double hourlyRate)
{
if (income <= 25000) {
if (consultingMinutes <= 30)
return 0;
else
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
}
else {
if (consultingMinutes <= 20)
return 0;
else
return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);
}
}
Mixing of integers and floats is not a very good thing. Try this:
return hourlyRate * 0.40 * (((double) consultingMinutes - 30.0) / 60.0);
instead of this:
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
(And apply the same fix for the second wrong place return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);)