Quick maths problem...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Quick maths problem...

 
0
  #1
Nov 4th, 2004
Greetings,

I have my first assignment for my programming class to do, but first, I will say that I am aware of those people who post entire problems requesting members to do their homework for them. This is not really what my post is about.

I'm having a little problem with getting my head around the maths part. Let me explain...

Below is the assignment in full:

A small company is writing its cheques by hand and wants to change
over to computer generated cheques.

Your boss has asked you, the new programmer, to produce a prototype
cheque writer that will accept the payees name and the amount payable
and display a cheque on the screen. The amount should be broken up
into ten thousand, thousand, hundreds and pounds. If the amount in any
of the categories is 0 then the word "zero" is put in that place.

The name of the company is MODCON Ltd.

Input the payee name: Joe
Input the amount payable: EUR 12045.67
*******************************************************
MODCON
Promises to pay Joe EUR 12045.67
1 ten_thousand
2 thousand
zero hundred
45 euro
67 cent
*******************************************************


Note that only one name is used. We cannot yet input first and
second names together.
Use only what you have learned to date Use any compiler you like to
test your code.

Try to get the cheque in the middle of the screen using <iomanip>
Identify the Inputs Outputs and constants.

Identify the data objects. Note that cheque in NOT an object as far as
we are concerned but is a collection of smaller objects that can be
seen on the screen in the example.

List the actions to be performed on those objects.

Write the algorithm consisting of the list of actions ordered
correctly and any control structures used.
Translate the algorithm into C++ code.

You need only test with positive numbers. Your code need not take
negative numbers into account.
I'm unsure how to have it display how many tens' of thousands, 'hundreads, cents etc. are in the number that was put in. Any ideas? Just some advice would be much appreciated, perhaps a link to an appropriate tutorial.

I have most of the code written. I will post it if you wish to see it....
Salutations....
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: Quick maths problem...

 
0
  #2
Nov 6th, 2004
OK I see nobody has responded to my topic. I have recieved an answer to the maths problem using fmod to complete the part. Apparently there is a way to do this without fmod using division and subtraction only. Any ideas???

Please respond....

Answer using fmod;

#include <iostream.h>
#include <math.h> /* You need this to use the fmod dunction */

int main()
{
double amt,amt_over;
double field_val10k,field_val1k,field_valhun,field_val10,field_val1,field_valcent;

cout << "Enter amount to be paid:"<<endl;
cin >> amt;

/*Work out the Ten thousands field */
amt_over=fmod(amt,10000);
field_val10k=(amt-amt_over)/10000;

/*Work out the thousands field */
amt_over=fmod(amt,1000);
field_val1k=((amt-amt_over)/1000)-(field_val10k*10);

/*Work out the hundreds field */
amt_over=fmod(amt,100);
field_valhun=((amt-amt_over)/100)-((field_val10k*100)+(field_val1k*10));

/*Work out the tens field */
amt_over=fmod(amt,10);
field_val10=((amt-amt_over)/10)-((field_val10k*1000)+(field_val1k*100)+(field_valhun*10));

/*Work out the single unit field */
amt_over=fmod(amt,1);
field_val1=(amt-amt_over)-((field_val10k*10000)+(field_val1k*1000)+(field_valhun*100)+(field_val10*10));

/*Cent field is the easiest!!*/
field_valcent=(fmod(amt,1)*100);


cout << "#######################################"<<endl;
cout << "Ten thousands:\t" <<field_val10k<<endl;
cout << "Thousands:\t" <<field_val1k<<endl;
cout << "Hundreds:\t" <<field_valhun<<endl;
cout << "Tens:\t\t" <<field_val10<<endl;
cout << "Ones:\t\t" <<field_val1<<endl;
cout << "Cents:\t\t"<<field_valcent<<endl;
cout << "#######################################"<<endl;


return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Quick maths problem...

 
0
  #3
Nov 6th, 2004
If you want the number of 10,000's, say, you could do this:

float numberWithBigValueInIt = 45,678.0;

int tenThousands = (int) (numberWithBigValueInIt / 10000.0);

tenThousands would contain 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: Quick maths problem...

 
0
  #4
Nov 7th, 2004
Thanks for the reply Chainsaw, however would this way return something like '4.5678?' Whilst, what I need is '4'.

Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Quick maths problem...

 
0
  #5
Nov 7th, 2004
>however would this way return something like '4.5678?'
No, integers can't hold a precision, so everything past the radix is truncated. You would have 4. But you would also have known this if you had bothered to try it before asking this question.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: Quick maths problem...

 
0
  #6
Nov 9th, 2004
Originally Posted by Narue
>however would this way return something like '4.5678?'
No, integers can't hold a precision, so everything past the radix is truncated. You would have 4. But you would also have known this if you had bothered to try it before asking this question.
I was not aware. I didn't try it becuase I didn't think it would work.

Now that I've tried it, I can't seem to actually get it working....This is what I have come up with so far but I cannot determine what's wrong with it. Can someone see anything wrong with it?

#include<iostream>
#include<iomanip>
#include<string>


using namespace std;

int main()
{

string name;
double amount;
string prompt;
prompt = "Input payee name here like: ";
cout << prompt;
cin >> name;
cin.get();

prompt = "Input amount payable: ";
cout << prompt;
cin >> amount;
cin.get();

int ten_thousand;
int thousand;
int hundread;
int euro;
double cent;

cout << endl;
cout << endl;



// THE CHEQUE
cout << "*******************************************";
cout << endl << "\t"<< setw(20) << "MODCON" << endl;

cout << "Promises to pay " << "\t" << name << setw(20) << "EUR" << amount <<endl;

//Calculate amount of thousands.
int thousand = (int) (amount / 1000);
cout << "Thousands " << setw(20) << thousand;

cout << endl << "**********************************************";



cin.get();

return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: Quick maths problem...

 
0
  #7
Nov 9th, 2004
Update** I have got it to give me the amount of ten_thousands and thousands. The next one seems a bit tricky.

e.g.... lets say the amount is 35857.56

ten_thousand = (int) (amount / 10000);
ten_thousand comes up as 3.

thousand = (int) (amount / 1000) - (ten_thousand * 10);
thousand comes up as 5
However it's the next part I cannot seem to get, the 'hundread'...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 714
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Quick maths problem...

 
0
  #8
Nov 9th, 2004
>I didn't try it becuase I didn't think it would work.
Serves you right then.

>I can't seem to actually get it working
It could be something to do with your defining the name thousand twice.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 24
Reputation: Marauder is an unknown quantity at this point 
Solved Threads: 0
Marauder's Avatar
Marauder Marauder is offline Offline
Newbie Poster

Re: Quick maths problem...

 
0
  #9
Nov 10th, 2004
I have got it working. All of it! Fantastic! :mrgreen:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC