Solved

C++ Activity for learning, need some Help :)

Posted on 2004-04-15
15
243 Views
Last Modified: 2010-04-01
Ok, really really new at programming, so I'm trying to get a head start for a C++ class I'll be taking in the June.  I have some exercises that I'm trying to go threw to practice.  Once I figure out the program I want to try an enhance it.  
The activity is:
Take an undetermined amount of pennies and have the program display how many pennies, nickels, dimes, quarters, 5, 10, 20, 100 dollar bills you will receive.  The math is killing me even though I know it should be simple.  I did some research and it looks like I should be using modular arithmetic (not familiar with that).  Can anyone help me?  I want to figure this out, so I can enhance it.  I want to have it display how much Euro it would be as well, not sure how to do that yet.
0
Comment
Question by:super_man_1976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 7

Accepted Solution

by:
leflon earned 70 total points
ID: 10834015
Hi super_man_1976,

have you already started with the prog? If so post what you have and we will try to help you to improve your prog.

the prog should (just a very rough outline)

- read dollar amount to be split
- subtract largest possible bill/coin and count how often you do this for each (do it till you reach zero)
- display result
- do the conversion to Euro (?? when and which value will you convert to euro)

if there are no special limitations to the coins/bills there should be no complex arithmetic involved.

leflon
0
 

Author Comment

by:super_man_1976
ID: 10834396
I have not started yet, just in my head.  I don't have a compiler at work so I'll have to wait till I get home.  I really confused my self when thinking about the simple math part.  I must be overthinking it.  As far as the Euro thing, I'm not sure.  I don't have to do that, I just want to do something to extend on the basic part of the program, not sure what.
0
 
LVL 7

Expert Comment

by:leflon
ID: 10834511
super_man_1976,

if you like to share your first steps and your prog with us you are welcome.

as long as you don't expect us to write the whole prog you can ask for help anytime :-). (you know this 'homework' part of EE policies)

cheers
leflon
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:super_man_1976
ID: 10834602
Right, I understand that.  
Just to give you an idea on how new  I am to programming and C++, below was my first program that I did last night.  I will start programming this next exercise and post it as soon as I can.  It's not for a formal class, just practice so I'm not lost when I do take a class.  Thank you. for your help.

#include <iostream.h>

int main ()

{

            int num1, num2, total;

            num1 = 12;
            num2 = 33;
            total = num1 + num2;
            cout << "The total of " <<num1 <<" and " << num2 << " is " <<
            total << endl;

            return 0;
 

}  
0
 
LVL 7

Expert Comment

by:leflon
ID: 10834718
that's a fine start. looks much better than my first (the typical "Hello World")

just one thing to mention.
when you like to use the 'pure' C++ you should omit the old style includes <*.h>

your prog would start like

#include <iostream>           // whithout .h

using namespace std;          // so you can use cout and endl without the compiler throwing errors
int main ()
{
            int num1, num2, total;
...
            cout << "The total of " <<num1 <<" and " << num2 << " is " <<
            total << endl;
            return 0;
}  

cheers
leflon
0
 

Author Comment

by:super_man_1976
ID: 10845828
Ok,
leflon this is what I have so far.  I know, you can laugh out load I'll really unsure here.  Is the //..  just to make a comment?  Because that's what I was trying to do.  If I'm heading in the right direction then I need to figure out the math part.  So I should use Modulus (%) to give give me the remainder?


//... This program will calculate any amount of pennies into the following:
//... $100 Dollar Bills
//... $50 Dollar Bills
//... $20 Dollar Bills
//... $10 Dollar Bills
//... $5 Dollar Bills
//... $1 Dollar Bills
//... Quarters
//... Dimes
//... Nickles
//... Left over pennies

#include<iostream>

main()
{

  {
      cout<<"\n \n "This program will calculate your pennies so you know what to expect when exchanging them at the Bank!";
      cout<<"How many Pennies do you have?:";
      cin>>Pennies;

  }

????Modulus (% in C++) should give give you the remainder???


  cout<<"\n\n I've determned that this is what you should receive when you go to the bank:";

}

<<endl
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10847938
>>>????Modulus (% in C++) should give give you the remainder???..............

Yes, % operator returns the remainder

And // are used for comments

Amit
0
 
LVL 7

Expert Comment

by:leflon
ID: 10848662
will be back soon. :-)

leflon
0
 

Author Comment

by:super_man_1976
ID: 10855574
Ok, this is what I have now.  I think the math is closer???????  

//... This program will calculate any amount of Pennies into the following:
//... 100 Dollar Bills       
//... 50 Dollar Bills      
//... 20 Dollar Bills      
//... 10 Dollar Bills      
//... 5 Dollar Bills       
//... 1 Dollar Bills       
//... Quarters             
//... Dimes            
//... Nickles            
//... Remaining pennies      

#include<iostream>

main()
{

  {
      cout<<"\n \n "This program will calculate your Pennies so you know what to expect when exchanging them at the Bank!";
      cout<<"How many Pennies do you have?:";
      cin>>Pennies;

int One Hundred Dollar Bills = Pennies / 10000;  
int Fifty Dollar Bills = Pennies / 5000;
int Pennies = Pennies % 5000;
int Twenty Dollar Bills = Pennies / 2000;
int Pennies = Pennies % 2000;
int Ten Dollar Bills = Pennies / 1000;
int Pennies = Pennies % 1000;
int Five Dollar Bills = Pennies / 500;
int Pennies = Pennies % 500;
int One Dollar Bills = Pennies / 100;
int Pennies = Pennies % 100;
int Quarters = Pennies / 25;
int Pennies = Pennies % 25;
int Dimes = Pennies / 10;
int Pennies = Pennies % 10;
int Nickles      = Pennies / 5;
int Pennies = Pennies % 5;
int Pennies = 1;
int Pennies = Pennies % 1;

cout<<"\n\n I've determned that this is what you should receive when you go to the bank:";
cout<<int

}

<<endl
0
 
LVL 7

Expert Comment

by:leflon
ID: 10857892
Hi super_man_1976,

i think the basic idea is ok.
you ask the user to input the ammount of pennies and then get the amount of each bill/coin by doing an integer division and you get the still to distribute rest by doing the modulo operation.
but there are a few errors in the code.

1. the variable Pennies is not defined
2. variable names with spaces in it (One Hundred Dollar Bills etc.) are not allowed. make it OneHundredDollarBills
3. do not redefine Pennie in each modulo operation (int Pennies=Pennies%5000 => Pennies=Pennies%5000)
4. you forgot (either in your prog or just in your post here) the modulo operation for the hundret dollar bills.
5. there seems to be a missing closing bracket '}' at the end of the prog. (you can omit the inner pair of { }.
6. you don't need the last modulo op, cause what is left after %5 should be the amount of single pennies.

so you have something like

int main(int argc, char* argv[])
{
  int Pennies = 0;
  int OrgPennoes = 0;  // for storiung the original amount of pennies

  // your initial output, program description etc.
  cin >> Pennies;
  // here you may want to insert some error handling if user enters wrong values (like 'aaaa')
  OrgPennies = Pennies,

  // now comes the splitting, the way you supposed to do
  int Bill100 = Pennies/10000;
  Pennies = Pennies%10000;
  ... // allthe other bills and coins
 
  // at this point you have your Pennies divided into the different bills and coins and have stored the amount of each in the proper var
  // now we have to do the output

  cout << "For " << OrgPennies << " you will get:" << endl;
  cout << Bill100 << "onehundret dollar bills" << endl;
  ...// all the other bills and coins
  cout << Pennies << "pennies" << endl;

  return 0;  // program end
}

for the first shot the prog shall do what you want it to do, but you may like to add some error checking or make it a litlle shorter (and some say more readable) by adding some other stuff like using arrays etc.

hth
leflon
0
 

Author Comment

by:super_man_1976
ID: 10858729
Ok, first of all I want to thank you for all the help. :) I'm learning on the fly here.

Here is what I have so far, thought I was headed in the write direction until I tried compileing it with 1 error.  Not sure what it is.  

int main
{
  int Pennies = 0;
  int OrgPennoes = 0;

  // This program will calculate any amount of Pennies into: $100's, $50's, $20's, $10's , $5's, 1's, Quarters, Dimes, Nickles and the remaining pennies.
  cin >> Pennies;
  // Possible error handling.
  OrgPennies = Pennies,

  // Splitting up the money
  int Bill100 = Pennies/10000;
  Pennies = Pennies%10000;.

  int Bill50 = Pennies/5000;
  Pennies = Pennies%5000;.

  int Bill20 = Pennies/2000;
  Pennies = Pennies%2000;.

  int Bill10 = Pennies/1000;
  Pennies = Pennies%1000;.

  int Bill5 = Pennies/500;
  Pennies = Pennies%1000;.

  int Bill1 = Pennies/100;
  Pennies = Pennies%100;.

  int Coin25 = Pennies/25;
  Pennies = Pennies%25;.

  int Coin10 = Pennies/10;
  Pennies = Pennies%10;.

  int Coin5 = Pennies/5;
  Pennies = Pennies%5;.

  int Coin1 = Pennies/1;
  Pennies = Pennies%1;.

  cout << "For " << OrgPennies << " You will get:" << endl;

  cout << Bill100 << "One Hundred dollar bills" << endl;

  cout << Bill50 << "Fifty dollar bills" << endl;

  cout << Bill20 << "Twenty dollar bills" << endl;

  cout << Bill10 << "Ten dollar bills" << endl;

  cout << Bill5 << "Five dollar bills" << endl;

  cout << Bill1 << "One dollar bills" << endl;

  cout << Coin25 << "Quarters" << endl;

  cout << Coin10 << "Dimes" << endl;

  cout << Coin5 << "Nickels" << endl;

  cout << Coin1 << "Pennies" << endl;

  cout << Pennies << "pennies" << endl;

  return 0;  // program end
}
0
 
LVL 7

Expert Comment

by:leflon
ID: 10858884
you have a typo in OrgPennies declaration -> int OrgPennoes = 0;

this will give you a syntax error (something like undeclared.....) for the line ->
   OrgPennies = Pennies, (and here is a ',' where a ';' has to be)
and
   cout << "For " << OrgPennies << " You will get:" << endl;


leflon
0
 
LVL 7

Expert Comment

by:leflon
ID: 10858926
but maybe you can post the error message here too

leflon
0
 

Author Comment

by:super_man_1976
ID: 10859161
I'll check it out tonight.........I'm at work now with now.  Thanks agian.
0
 
LVL 7

Expert Comment

by:leflon
ID: 10859197
no prob. if you have any further questions, you where to find us :)

cheers
leflon
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question