Results 1 to 11 of 11

Thread: C++ Help

  1. #1
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default C++ Help

    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

    C++ Code:
    #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
    Current Project: Retired

  2. #2
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    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.
    Code:
      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:
    Simba Code:
    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)...
    Code:
    #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;
    
    }
    Last edited by x[Warrior]x3500; 01-15-2013 at 06:34 AM.

  3. #3
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    I see, kind of like my code but still fairly different I'll give that a try in a bit
    Current Project: Retired

  4. #4
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Tried this, no luck.

    C++ Code:
    #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;
    }
    Current Project: Retired

  5. #5
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    C++ Tags looking Messed up on my browser atm.. Going to use code-tags I guess.. You can do something like this:

    Code:
    #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):

    Code:
    #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;
    }
    Last edited by Brandon; 01-15-2013 at 06:57 AM.
    I am Ggzz..
    Hackintosher

  6. #6
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    C++ Tags looking Messed up on my browser atm.. Going to use code-tags I guess.. You can do something like this:

    Code:
    #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):

    Code:
    #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
    Last edited by Gucci; 01-15-2013 at 01:59 PM. Reason: Saw warrior provided code for his method

  7. #7
    Join Date
    Jan 2012
    Posts
    915
    Mentioned
    13 Post(s)
    Quoted
    87 Post(s)

    Default

    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.

  8. #8
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Couldn't get Warriors or Brandons method to work, but I did work it out and ended up with this:

    C++ Code:
    #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;
    }
    Current Project: Retired

  9. #9
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    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).

  10. #10
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    Quote Originally Posted by x[Warrior]x3500 View Post
    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
    Current Project: Retired

  11. #11
    Join Date
    Jun 2008
    Location
    United States
    Posts
    818
    Mentioned
    60 Post(s)
    Quoted
    90 Post(s)

    Default

    I don't know C++ syntax, but here's a Pascal implementation:

    Simba Code:
    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
    Last edited by euphemism; 01-18-2013 at 03:43 AM.
    [10/14/13:19:03] <BenLand100> this is special relatively, just cleverly disguised with yachts

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •