PDA

View Full Version : C++ Help



Gucci
01-15-2013, 05:12 AM
So my prof said this would be a good question to figure out:


Write a program that will prompt the user for an integer number of cents and will determine the number of toonies, loonies, quarters, dimes, nickels and pennies that are required for this amount of change. The program will display the results to the screen in a pleasing manner.
Examples:
Input: 150
Output: 1 loonie, 2 quarters

Input: 430
Output: 2 toonies, 1 quarter, 1 nickel


#include <cstdlib>
#include <iostream>

using namespace std;
void www()
{

}


int main()
{
double A, B, C, D, E, F, G, J, K, M, N;
short int H, L, P;

cout << "Input the number of cents.\n";
cin >> A;
if ( A >= 200) {

B = (A/200);
H = (A/200);
J = ((B - H)*200);

if ( J >= 100) {

K = (J/100);
L = (J/100);
M = ((K - L)*100);

if ( M >= 25) {
D = (M/25);
P = (M/25);
N = ((D - P)*25);
}
}

else if ( J < 100 ) {
D = (J/25);
P = (J/25);
N = ((D - P)*25);
}
}

cout << "Toonies: " << H << " Loonies: " << L << " Quarters: " << P << " Dimes: " << E << " Nickels: " << F << " Pennies: " << G << endl;

system("PAUSE");

return 0;
}

Came up with a method that basically continue ^that until pennies and then start for if (A >= 100) until pennies and repeat for quarters-pennies, dimes-pennies, nickels-pennies. Just a bunch of if..else statements there must be a easier way to do it. Please suggest/show examples

Brandon

Daniel

x[Warrior]x3500
01-15-2013, 05:26 AM
well this is really quick pseudo code that i just made. it should work. dont know exactly how u did it, or if this even contrasts your original approach, but if i was at ACM, thats the way i would do it.


var total...pennies : integer;


if( total>199 )then
inc toonies by floor( total/200.0 )
total= total - (toonies*200)

if( total>99 )then
inc loonies by floor( total/100.0 )
total= total - (loonies*100)

if( total>24 )then
inc quarters by floor( total/25.0 )
total= total - (quarters*25)

if( total>9 )then
inc dimes by floor( total/10.0 )
total= total - (dimes*10)


if( total>4 )then
inc nickel by floor( total/5.0 )
total= total - (nickel*5)

if( total>0 )then
inc pennies by floor( total/1.0 )
total= total - (pennies*1)

code i quickly wrote in simba:

program new;
var
total,a,b,c,d,e,f: integer;
begin
total:= 1235;

if( total>199 )then
incEx( a , floor( total/200.0 ));
total:= total - (a*200);

if( total>99 )then
incEx( b , floor( total/100.0 ));
total:= total - (b*100);

if( total>24 )then
incEx( c , floor( total/25.0 ));
total:= total - (c*25);

if( total>9 )then
incEx( d , floor( total/10.0 ));
total:= total - (d*10);

if( total>4 )then
incEx( e , floor( total/5.0 ));
total:= total - (e*5);

if( total>0 )then
incEx( f , floor( total/1.0 ));
total:= total - (f*1);

writeln(tostr(a));
writeln(tostr(b));
writeln(tostr(c));
writeln(tostr(d));
writeln(tostr(e));
writeln(tostr(f));
end.


and c++ code of my pseudo code since u insist this is wrong (post 4)...


#include <cmath>
#include <iostream>


using namespace std;

int main(){

int total;
int a=0,b=0,c=0,d=0,e=0,f=0;

total= 1235;

if( total>199 ){
a= a + floor( total/200.0 );
total= total - (a*200);
}
if( total>99 ){
b= b + floor( total/100.0 );
total= total - (b*100);
}
if( total>24 ){
c= c + floor( total/25.0 );
total= total - (c*25);
}
if( total>9 ){
d= d + floor( total/10.0 );
total= total - (d*10);
}
if( total>4 ){
e= e + floor( total/5.0 );
total= total - (e*5);
}
if( total>0 ){
f= f + floor( total/1.0 );
total= total - (f*1);
}
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
cin.get();
return 0;

}

Gucci
01-15-2013, 05:28 AM
I see, kind of like my code but still fairly different I'll give that a try in a bit

Gucci
01-15-2013, 06:20 AM
Tried this, no luck.

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
double total, total1, total2, total3, total4, total5;
short int A, B, C, D, E, F;

cout << "Enter total amount of cents\n";
cin >> total;

if (total > 199) {
A = (total/200);
total1 = total - (A*200);
}

if (total1 > 99) {
B = (total1/100);
total2 = total1 - (B*100);

}

if (total2 > 24) {
C = (total2/25);
total3 = total2 - (C*25);

}

if (total3 > 9) {
D = (total3/10);
total4 = total3 - (D*10);

}

if (total4 > 4) {
E = (total4/5);
total5 = total4 - (E*5);

}

if (total5 > 1) {
F = (total5/1);

}

cout << "Toonies: " << A << " Loonies: " << B << " Quarters: " << C << " Dimes: " << D << " Nickels: " << E << " Pennies: " << F << endl;

system("PAUSE");

return 0;
}

Brandon
01-15-2013, 06:46 AM
C++ Tags looking Messed up on my browser atm.. Going to use code-tags I guess.. You can do something like this:



#include <iostream>

void CalculateMoney(float Money)
{

int Amount = (int) (Money * 100.0 + 0.1); //To avoid Flooring/Ceiling/Rounding..

int Cash[10] = {0}; //Initialize All Values In The Array to 0..

Cash[0] = Amount / 10000;
Amount %= 10000; //Same as Amount = Amount % SomeValue

Cash[1] = Amount / 5000;
Amount %= 5000;

Cash[2] = Amount / 2000;
Amount %= 2000;

Cash[3] = Amount / 1000;
Amount %= 1000;

Cash[4] = Amount / 500;
Amount %= 500;

Cash[5] = Amount / 100;
Amount %= 100;

Cash[6] = Amount / 25;
Amount %= 25;

Cash[7] = Amount / 10;
Amount %= 10;

Cash[8] = Amount / 5;

Cash[9] = Amount % 5;


std::cout<<"Hundreds's: "<<Cash[0]<<std::endl;
std::cout<<"Fifty's: "<<Cash[1]<<std::endl;
std::cout<<"Twenty's: "<<Cash[2]<<std::endl;
std::cout<<"Ten's: "<<Cash[3]<<std::endl;
std::cout<<"Fives's "<<Cash[4]<<std::endl;
std::cout<<"Ones's: "<<Cash[5]<<std::endl;
std::cout<<"Quarters's: "<<Cash[6]<<std::endl;
std::cout<<"Dimes's: "<<Cash[7]<<std::endl;
std::cout<<"Nickels's: "<<Cash[8]<<std::endl;
std::cout<<"Pennies's: "<<Cash[9]<<std::endl;
}

int main()
{
CalculateMoney(25.5);
std::cin.get(); //So the program doesn't exit until you press enter.. Allows you to see the output.
return 0;
}




IF you're Canadian and accept Toonies (2$ coin):



#include <iostream>

void CalculateMoney(float Money)
{

int Amount = (int) (Money * 100.0 + 0.1); //To avoid Flooring/Ceiling/Rounding..

int Cash[11] = {0}; //Initialize All Values In The Array to 0..

Cash[0] = Amount / 10000;
Amount %= 10000; //Same as Amount = Amount % SomeValue

Cash[1] = Amount / 5000;
Amount %= 5000;

Cash[2] = Amount / 2000;
Amount %= 2000;

Cash[3] = Amount / 1000;
Amount %= 1000;

Cash[4] = Amount / 500;
Amount %= 500;

Cash[5] = Amount / 200;
Amount %= 200;

Cash[6] = Amount / 100;
Amount %= 100;

Cash[7] = Amount / 25;
Amount %= 25;

Cash[8] = Amount / 10;
Amount %= 10;

Cash[9] = Amount / 5;

Cash[10] = Amount % 5;


std::cout<<"Hundreds's: "<<Cash[0]<<std::endl;
std::cout<<"Fifty's: "<<Cash[1]<<std::endl;
std::cout<<"Twenty's: "<<Cash[2]<<std::endl;
std::cout<<"Ten's: "<<Cash[3]<<std::endl;
std::cout<<"Fives's: "<<Cash[4]<<std::endl;
std::cout<<"Toonie's: "<<Cash[5]<<std::endl;
std::cout<<"Ones's: "<<Cash[6]<<std::endl;
std::cout<<"Quarters's: "<<Cash[7]<<std::endl;
std::cout<<"Dimes's: "<<Cash[8]<<std::endl;
std::cout<<"Nickels's: "<<Cash[9]<<std::endl;
std::cout<<"Pennies's: "<<Cash[10]<<std::endl;
}

int main()
{
CalculateMoney(22.5);
std::cin.get(); //So the program doesn't exit until you press enter.. Allows you to see the output.
return 0;
}

Gucci
01-15-2013, 07:03 AM
C++ Tags looking Messed up on my browser atm.. Going to use code-tags I guess.. You can do something like this:



#include <iostream>

void CalculateMoney(float Money)
{

int Amount = (int) (Money * 100.0 + 0.1); //To avoid Flooring/Ceiling/Rounding..

int Cash[10] = {0}; //Initialize All Values In The Array to 0..

Cash[0] = Amount / 10000;
Amount %= 10000; //Same as Amount = Amount % SomeValue

Cash[1] = Amount / 5000;
Amount %= 5000;

Cash[2] = Amount / 2000;
Amount %= 2000;

Cash[3] = Amount / 1000;
Amount %= 1000;

Cash[4] = Amount / 500;
Amount %= 500;

Cash[5] = Amount / 100;
Amount %= 100;

Cash[6] = Amount / 25;
Amount %= 25;

Cash[7] = Amount / 10;
Amount %= 10;

Cash[8] = Amount / 5;

Cash[9] = Amount % 5;


std::cout<<"Hundreds's: "<<Cash[0]<<std::endl;
std::cout<<"Fifty's: "<<Cash[1]<<std::endl;
std::cout<<"Twenty's: "<<Cash[2]<<std::endl;
std::cout<<"Ten's: "<<Cash[3]<<std::endl;
std::cout<<"Fives's "<<Cash[4]<<std::endl;
std::cout<<"Ones's: "<<Cash[5]<<std::endl;
std::cout<<"Quarters's: "<<Cash[6]<<std::endl;
std::cout<<"Dimes's: "<<Cash[7]<<std::endl;
std::cout<<"Nickels's: "<<Cash[8]<<std::endl;
std::cout<<"Pennies's: "<<Cash[9]<<std::endl;
}

int main()
{
CalculateMoney(25.5);
std::cin.get(); //So the program doesn't exit until you press enter.. Allows you to see the output.
return 0;
}




IF you're Canadian and accept Toonies (2$ coin):



#include <iostream>

void CalculateMoney(float Money)
{

int Amount = (int) (Money * 100.0 + 0.1); //To avoid Flooring/Ceiling/Rounding..

int Cash[11] = {0}; //Initialize All Values In The Array to 0..

Cash[0] = Amount / 10000;
Amount %= 10000; //Same as Amount = Amount % SomeValue

Cash[1] = Amount / 5000;
Amount %= 5000;

Cash[2] = Amount / 2000;
Amount %= 2000;

Cash[3] = Amount / 1000;
Amount %= 1000;

Cash[4] = Amount / 500;
Amount %= 500;

Cash[5] = Amount / 200;
Amount %= 200;

Cash[6] = Amount / 100;
Amount %= 100;

Cash[7] = Amount / 25;
Amount %= 25;

Cash[8] = Amount / 10;
Amount %= 10;

Cash[9] = Amount / 5;

Cash[10] = Amount % 5;


std::cout<<"Hundreds's: "<<Cash[0]<<std::endl;
std::cout<<"Fifty's: "<<Cash[1]<<std::endl;
std::cout<<"Twenty's: "<<Cash[2]<<std::endl;
std::cout<<"Ten's: "<<Cash[3]<<std::endl;
std::cout<<"Fives's: "<<Cash[4]<<std::endl;
std::cout<<"Toonie's: "<<Cash[5]<<std::endl;
std::cout<<"Ones's: "<<Cash[6]<<std::endl;
std::cout<<"Quarters's: "<<Cash[7]<<std::endl;
std::cout<<"Dimes's: "<<Cash[8]<<std::endl;
std::cout<<"Nickels's: "<<Cash[9]<<std::endl;
std::cout<<"Pennies's: "<<Cash[10]<<std::endl;
}

int main()
{
CalculateMoney(22.5);
std::cin.get(); //So the program doesn't exit until you press enter.. Allows you to see the output.
return 0;
}



So you would do it with an array..... is there an effective way to do it with just loops? Also thanks for the help guys

Edit: nvm seems like warrior has code for his method.

Thanks a lot for the help :D

Vinyl Scratch
01-15-2013, 01:03 PM
Well, I wish I could work this out with you, and learn from it, but I need to know what Toonies and Loonies are.

Toonies are $2 coins, right?
Loonies are $1 coins, then?



Edit: Well, I'm late. Sorry.

Gucci
01-15-2013, 05:55 PM
Couldn't get Warriors or Brandons method to work, but I did work it out and ended up with this:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int total, total1, total2, total3, total4, total5, A, B, C, D, E, F;

cout << "Enter total amount of cents\n";
cin >> total;

if (total >= 199) {
A = (total/200);
total1 = (total % 200);
}

else if (total < 199) {
A = 0;
total1 = total;
}

if (total1 >= 99) {
B = (total1/100);
total2 = (total1 % 100);
}

else if (total1 < 99) {
B = 0;
total2 = total1;
}

if (total2 >= 24) {
C = (total2/25);
total3 = (total2 % 25);
}

else if (total2 < 24) {
C = 0;
total3 = total2;
}

if (total3 >= 9) {
D = (total3/10);
total4 = (total3 % 10);
}

else if (total3 < 9) {
D = 0;
total4 = total3;
}

if (total4 >= 4) {
E = (total4/5);
total5 = (total4 % 5);
}

else if (total4 < 4) {
E = 0;
total5 = total4;
}

if (total >= 1) {
F = (total5/1);
}

cout << "Toonies: " << A << " Loonies: " << B << " Quarters: " << C << " Dimes: " << D << " Nickels: " << E << " Pennies: " << F << endl;

system("PAUSE");

return 0;
}

x[Warrior]x3500
01-15-2013, 06:04 PM
i know u already got this working, but i just wanted to let you know: the code in your post #4 does NOT represent my pseudo code. i wrote c++ code last night that does represent the pseudo code correctly (compiled with g++) (added code to previous post).

Gucci
01-15-2013, 06:17 PM
x3500;1159176']i know u already got this working, but i just wanted to let you know: the code in your post #4 does NOT represent my pseudo code. i wrote c++ code last night that does represent the pseudo code correctly (compiled with g++) (added code to previous post).

Yeah I realized that, but even the code you added to your post didn't work. But it's all good pleased that I was able to work it out

euphemism
01-15-2013, 07:28 PM
I don't know C++ syntax, but here's a Pascal implementation:


function FormatCents(Cents: Integer): String;
var
I, Len: Integer;
CommaString: String;
Coins, Amounts: TIntegerArray;
CoinTitles: TStringArray;
begin
Result := '';
Coins := [200, 100, 25, 10, 5, 1];
CoinTitles := ['Toonie', 'Loonie', 'Quarter', 'Dime', 'Nickel', 'Penny'];
Len := Length(Coins);
SetLength(CoinTitles, Len);
SetLength(Amounts, Len);
for I := 0 to (Len - 1) do
begin
Amounts[i] := (Cents div Coins[i]);
Cents := (Cents mod Coins[i]);
if ((Amounts[i] > 1) or (Amounts[i] = 0)) then
if (I < 5) then
CoinTitles[i] := CoinTitles[i] + 's'
else
CoinTitles[i] := 'Pennies';
case I of
0..4: CommaString := ', ';
5: CommaString := '';
end;
Result := Result + IntToStr(Amounts[i]) + ' ' + CoinTitles[i] + CommaString;
end;
Result := Result + '.';
end;


Gucci