calculates the number in change

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

calculates the number in change

Postby Tippman5 » Mon Dec 15, 2003 10:54 am

Code: Select all
#include <iostream.h>
main()
{
    int q=25, d=10, n=5, p=1,num;
    cout<<"The program will calculate the number of quarters, dimes, nickles, and pennies to generate the number of cents entered.\n";
    cout <<"Enter a number in cents.\n";
    cin >>num;

so far i came up with this....
How can i make an equation for this problem?
i know i cant divide the numbers...
Tippman5
 

Postby icujc » Mon Dec 15, 2003 11:10 am

You will need to use the modules operator "%" and the divisoin operator "/". Like this:

int cents

quarters = cents%25
cents -= quarters * 25
etc etc etc

Hope that helps ;)
--

"Computers are like air conditioners - They can't do their job
properly if you open windows."
User avatar
icujc
 
Posts: 83
Joined: Mon Sep 22, 2003 3:41 pm
Location: Alabama

Postby Tippman 5 » Wed Dec 17, 2003 11:47 am

Code: Select all
main()
{
    int cents,num,quarters,dimes,nickles=;
    cout<<"The program will calculate the number of quarters, dimes, nickles, and pennies to generate the number of cents entered.\n";
    cout <<"Enter a number in dollars.\n";
    cin >>num;

    cents=num*100;
   
   quarters = cents%25;
   cents = quarters * 25;
   
   dimes= cents%10;
   cents=dimes *10;

   nickles=cents%10;
   cents=nickles*10;


    cout <<"It took "<<quarters << " quaters, " << dimes<< " dimes, "<<nickles<< " nickles, " <<cents<< " cents.\n";
   
   return 0;
}


so i came up with this program ..but its not working... :(
if i put in $2.36 it outputs zeros for quarters, dimes, nickles, and pennies...
does anyone know why its doing that?
Tippman 5
 

Postby ADnova » Wed Dec 17, 2003 12:58 pm

You have declared num to be an int, so when you input 2.36, only the 2 will be assigned to num. num is an integer, not a floating point variable.

If you want a simple way to input a floating point number, try:

Code: Select all
// Always initialize your variables!
double num = 0.0;
int cents = 0, quarters = 0, dimes = 0, nickles = 0;

cout << "The program will calculate the number "
        "of quarters, dimes, nickles, and pennies to generate "
        "the number of cents entered.\n";
cout << "Enter a number in dollars.\n";
cin >> num;

// Just think about why this will work...
cents = num*100;

// 2.36 * 100 == 236 (or 236 cents, which is what you want)


That should take care of that problem.
Your other problem is that your calcualtions are wrong.
You need to think through the problem again.

You have:
quarters = cents%25;
cents = quarters * 25;

So for the 2.36 example, 2.36 goes to 236 (from the code above). Now, what's the remainder when you divide 236 by 25? What is the quotient?
236 / 25 = 9
236 % 25 = 11
Think about which one is the number of quarters you'll have, and which one is how many cents you have left over.
ADnova
 
Posts: 86
Joined: Tue Dec 09, 2003 6:04 pm
Location: Canada

Postby Madacgrav » Wed Dec 17, 2003 2:30 pm

Two things. First you may want to keep the dollar amount as a float variable and just multiply and divide by .25, .10 etc. If you chane the whole amount to int it will cause some rounding issues.
exp
Code: Select all
dimes= int(dollars/.10);

that will give you int but not round up.
Second, AdNova is right think about the math. Write the problem on a piece of paper and go thru how you would figure it out.
1. Find out how many times the type of change goes into the value then
2. Find the amount the answer to the problem 1 to get change value then
3. "This is the step you are missing"
4. Move on to next change type
Madacgrav
 
Posts: 137
Joined: Mon Dec 15, 2003 11:03 am
Location: MS --> USA

Postby tomcant » Wed Dec 17, 2003 2:32 pm

I remember writing something similiar in my early days......

Code: Select all
#include <iostream>

using namespace std;

int convert(int total);

int main()
{
   int total;
   
   cout << "Enter a total ammount: ";
   cin >> total;

   convert(total);

   system("pause");

   return 0;
}

int convert(int total)
{
   int ten_pound = 0, five_pound = 0, two_pound = 0, pound = 0, fifty = 0, twenty = 0, ten = 0, five = 0, penny = 0;
   bool done = false;

   while(total > 0)
   {
      if(total >= 1000)
      {
         ten_pound++;
         total -= 1000;
      }
      else if(total >= 500)
      {
         five_pound++;
         total -= 500;
      }
      else if(total >= 200)
      {
         two_pound++;
         total -= 200;
      }
      else if(total >= 100)
      {
         pound++;
         total -= 100;
      }
      else if(total >= 50)
      {
         fifty++;
         total -= 50;
      }
      else if(total >= 20)
      {
         twenty++;
         total -= 20;
      }
      else if(total >= 10)
      {
         ten++;
         total -= 10;
      }
      else if(total >= 5)
      {
         five++;
         total -= 5;
      }
      else
      {
         penny++;
         total -= 1;
      }
   }

   done = true;            

   if(done)
   {
      cout << "\nChange: Ten Pounds:  " << ten_pound << endl;
      cout << "\tFive Pounds: " << five_pound << endl;
      cout << "\tTwo Pounds:  " << two_pound << endl;
      cout << "\tPounds:      " << pound << endl;
      cout << "\tFifties:     " << fifty << endl;
      cout << "\tTwenties:    " << twenty << endl;
      cout << "\tTens:        " << ten << endl;
      cout << "\tFives:       " << five << endl;
      cout << "\tPennies:     " << penny << "\n\n\t";
   }

   return 0;
}


Thats what I came up with, it currently works for pounds and pence however.

Hope it helps,

_______
Tom
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Madacgrav » Wed Dec 17, 2003 2:53 pm

Sorry I should proofread my own post. Some of what I wrote did not make sense. Here is an example of what i wrote.
Code: Select all
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>

main()
{
    double dollars = 0.0, temp = 0.0;
    int cents=0,quarters=0,dimes=0,nickles=0, pennies = 0;;
    std::cout<<"The program will calculate the number of quarters, dimes, nickles, and pennies to generate the number of cents entered.\n";
    std::cout <<"Enter a number in dollars.\n";
    std::cin >>dollars;

    //cents=dollars*100;
   
    quarters = (int)(dollars / .25);
    dollars -= (quarters * .25);
   
    dimes= int(dollars/.10);
    dollars-=(dimes*.10);
    nickles=int(dollars/.05);
    dollars-=(nickles*.05);
    pennies=int(dollars/.01);
    dollars-=(pennies*.01);

    std::cout <<"It took "<<quarters << " quater(s), " << dimes<< " dime(s), "<<nickles<< " nickle(s), " <<pennies<< " penny(ies).\n";
    system("Pause");
   return 0;
}
Madacgrav
 
Posts: 137
Joined: Mon Dec 15, 2003 11:03 am
Location: MS --> USA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests