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
Solved

How to write a division formula in c?

Posted on 2004-08-22
7
197 Views
Last Modified: 2010-04-15
In c program, would you be able to show me how to write a program to tell 185.80 is made up of the following please.
$100 x 1
$50 x1
$20 x1
$10 x1
$5 x1
$0.5x1
$0.2x1
$0.1x1
Many thanks!
0
Comment
Question by:Melissamcg
7 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 11864328
I guess it is an assignment, so I will give you some steps.
First divide your problem in 2 parts: entire and decimal portion.
You must make sucessive divisions and modulo to the entire portion

double f = 185.80; /* this is your original value */
int entire = (int)floor(f);  /* floor will give you the entire portion and (int) will "cast" the value to int */

This is just the starting point. So, to know if value has a $100 component, divide it
int bill;
bill = entire / 100;

if result (bill) is greater than 0, then the value contains one or more $100 bills
now to make next calculus, just rest $100 values from entire portion:
entire -= bill*100;

Well, next is your job, continue dividing, for decimal portion, the behaviour is the inverse.
Go ahead.
 
0
 
LVL 9

Expert Comment

by:keteracel
ID: 11864403
*** homework solution deleted by Netminder, Site Admin ***
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11864424
keteracel:
If this is a homework, you have violated the Members Agreement because you have provided the full-solution.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11868962
The fact that you've got '$' signs in your question suggests that this is like the automatic change calculators you get on cash tills in shops.

These require a list of the denominations of the currency you are working with. Once this table is entered the rest becomes trivial, just find the largest denomination in the table that is less than or equal to the value you have left. Then decide how many of them can be subtracted and do it.

The table is important because different currencies can make things get really wierd. Remember the old Pounds/Shillings/Pence we used to have here in the UK? There are still countries that use something like that.

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11868997
>>Then decide how many of them can be subtracted and do it.

Also ... keep going until the smallest denomination is larger than your current result (this will work when your result is zero too).

Note: I would strongly suggest you do not use floating point maths. Use integer pence (or cents in your case). Using floats will add unnecessary complexity to what is really a simple multiplication and subtraction problem.

Paul
0
 

Author Comment

by:Melissamcg
ID: 11872676
Thank you all for your input. Since I am still new to C programming, I have chosen the longest way to work out my division from a floating number. I will look to improve my skill. Many thanks!
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 434
Test against App 49 138
Finding a good hash function 4 127
How to increment counter variables in ANSI  C (Not C# or C++). 9 17
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

808 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