making calculations with money - c++

so I am making a program that calculates the weight of a book and charges you according so.
as you know in decimal number the 0 is not considered in calculations so how do I make the computer consider it when it is dealing with money?
ps is it possible to make the computer display the pound sterling sign (£) on the program?
edit: sorry guys what i meant was like a number like 1.50 will just be written as 1.5 on a computer but money wise the 0 is important so how would make sure that the program includes it?

I am going to assume you mean printing because otherwise the '0' makes no difference to the compiler.
Use the following:
printf("%.2f ", 123.45678);
The computer can display '£' but I am not sure where youre displaying it. If it is within a print out statement, you should be able to just write it out.
Next time just be a little more careful about what you are asking.

Related

Any reason why Fortran is outputting some strange numbers for Project Euler 57?

learned python in the beginning of the summer, need to switch to Fortran for lab work. Could someone please help me discern why Fortran is outputting such odd numbers when doing simple addition? The photo below should be a good explanation of what I am trying to do with the program.
Fortran vs Python program
From Python's floating-point tutorial
almost all platforms map Python floats to IEEE-754 "double precision"
which in Fortran terms is a double precision or real(kind=REAL64) variable. Note that Python is weakly typed; you can stuff pretty much whatever you want into a Python variable and it just sort of knows what to do with it. Fortran is strongly typed so if you want your floating point data stored as REAL32, REAL64, or REAL128 (whatever your compiler defines in the ISO_Fortran_env module), you have to explicitly tell Fortran which specific type of float you want. By default, Fortran reals are REAL32 (so-called 'single precision') so you shouldn't be surprised that the results don't match what Python is generating.
That, of course, presumes you know the Secret Mystery Knowledge of the default numerical precision of both Fortran and Python, something we are all born with but which most of us lose along with our baby teeth.
Put another way, there's no way you could know this unless you knew the right question to ask in the first place, which nobody does the first time they see weird, seemingly-inconsistent floating point behavior. Back when FORTRAN was still taught, this sort of problem was introduced pretty early in the curriculum because the language is intended for crunching numbers and the problems with mixed-type and mixed-precision arithmetic are serious and well known. You had to learn about these pitfalls quickly because it was the difference between getting believable answers and garbage.
Modern languages are designed to simplify the delivery of cat videos. No real computer scientist would be caught dead discussing floating point mathematics so you need to search obscure backwater websites for information on how to make your numbers add up good and do other stuff good too. There is good info out there but again, you need to know what you're looking for in order to find it which most programmers don't when they hit this problem for the first time.
The short answer is to understand how computers simulate real numbers, how the languages you're using store those sorts of numbers, and ensure that the precision your application needs is supported by the data types you use. Hopefully that's more helpful than telling you to rephrase your question or RTFM.
And for what it's worth, I've been bitten by a similar problem recently where I had converted a code from single to double precision, forgetting that one of the binary files I was writing expected a single precision value. I only found this out during testing when visualization software choked on the broken binary file. The solution was obvious in hindsight; I reverted one variable back to single precision and all was well. The point is, even experienced people get tripped up by floating point. Barbie was right; math is hard...

Problems in coding small straight for Yahtzee game

So this is for a school assignment obviously, and for some reason I can not wrap my head around coding to calculate a small straight for the game of yahtzee. I have done all the coding for all the other possible rolls except this one. I know this is a question asked a lot but i cant find a simple answer anywhere. I need to be able to do it using if, for, and while loops. If anyone can help it would be massively appreciated!
Sort the rolls and count the number of consecutive dice. If you have four then you have a small straight. If all five rolls are consecutive then you have a large straight.

Limitations of the mod % operator with big integers

I want to mod out a number that's about 1.7x10^46 by several primes. Things did not look right so I tried hard coding the number mod 3 and 5. It is not giving me the correct answers. Mathematica says they should be 1 and 1, and yet I get 2 and 2.
Can someone please tell me what is going on?
It is my first time working with extremely large numbers, I am aware of the limitations of data types and their ranges, but this is hardcoded there is nothing stored in a variable.
Some time ago i used GMP library, maybe it will help you too. https://gmplib.org/
This should be a comment but can`t make one yet.

Fortran 95: super large numbers for prime test

I'm pretty new to Fortran, as in started learning it 2 days ago new. I started learning Fortran because I was getting into prime numbers, and I wrote a program in python that was so fast, it could determine 123098237 was a prime in 0.1 seconds.
Impressive, I know.
What's not impressive is when I try to find out if (2^127)-1 or 170141183460469231731687303715884105727 (it is, by the way) is a prime number. The program ran so long, I just ended up having to stop it.
So, I started looking for some faster languages to write it in, so I wrote the program in C.
It was faster, but the problem of super large prime numbers came into play.
I was going to to see if there was a solution but then I heard through the grapevine that, if your programming with numbers, Fortran is the fastest and best way to go. I vaguely remember my step dad's old Fortran 77 text books from college, but they were basically useless to me, because they were talking about working with punch cards. So, I went online, got gfortran for Ubuntu 12.04 x86, got a couple of pdfs, and started learning. Before you know it I made a program that received input and tested for primality, and worked!
But, the same old problem came up, the number was too big.
And so, how do I handle big numbers like this with Fortran?
Fortran, like many other compiled languages, doesn't provide such large integers or operations on them out-of-the-box. An up to date compiler ought to provide an integer with 18 decimal digits, but no more than that.
If you want to program, in Fortran, data types and operations for such big integers use your favourite search engine on terms such as Fortran multiple precision. You could even search around here on SO for relevant questions and answers.
If you want to investigate the mathematics of such large integers stick with Python; you'll struggle to write software yourself which matches its speed of operations on multiple precision arithmetic. One of the reasons that Python takes a long time to determine the primality of a large number is that it takes a program, any program written in any language, a long time to determine the primality of a large number. If you dig around you're likely to find that the relevant Python routines actually call code written in C or something similarly low-level. Investigate, if you wish, the topic of the computational complexity of primality testing.
I'm not saying you won't be able to write code to outperform the Python intrinsics, just that you will find it a challenge.
Most languages provide certain standard intrinsic types which are fully adequate for solving standard scientific and engineering problems. You don't need 80 digit numbers to calculate the thickness of a bridge girder or plan a spacecraft orbit. It would be difficult to measure to that accuracy. In Fortran, if you want to do extra precision calculations (e.g., for number theory) you need to look to libraries that augment the language, e.g., mpfun90 at http://crd-legacy.lbl.gov/~dhbailey/mpdist/ or fmlib at http://myweb.lmu.edu/dmsmith/fmlib.html
I'll guess that your algorithm is trial division. If that's true, you need a better algorithm; the implementation language won't matter.
Pseudocode for the Miller-Rabin primality test is shown below. It's probabilistic, but you can reduce the chance of error by increasing the k parameter, up to a maximum of about k=25:
function isPrime(n, k=5)
if n < 2 then return False
for p in [2,3,5,7,11,13,17,19,23,29]
if n % p == 0 then return n == p
s, d = 0, n-1
while d % 2 == 0
s, d = s+1, d/2
for i from 0 to k
x = powerMod(randint(2, n-1), d, n)
if x == 1 or x == n-1 then next i
for r from 1 to s
x = (x * x) % n
if x == 1 then return False
if x == n-1 then next i
return False
return True
I'll leave it to you to translate that to Fortran or some other language; if you're programming in C, there is a library called GMP that is frequently used for handling very large numbers, and the function shown above is built-it to that library. It's very fast; even numbers that are hundreds of digits long should be classified as prime or composite almost instantly.
If you want to be certain of the primality of a number, there are other algorithms that can actually provide a proof of primality. But they are much more complicated, and much slower.
You might be interested in the essay Programming with Prime Numbers at my blog.

4 randomly pulled cards at least one would be ace

Please help me my c++ program that I don't know how to write. Question is as below.
There is a well mixed deck of 32 cards. Method of statistical tests to obtain the probability of an event that of the 4 randomly pulled charts at least one would be ace.
Compare the value of the error of calculating the probability of the true error (the true probability value is approximately equal to 0.432). Vary the number of experiments n.
What are the odds of not drawing an ace in one draw?
In four successive draws?
What are the odds that that doesn't happen?
From what I understand of your question, you have already calculated the odds of drawing the ace, but now need a program to prove it.
Shuffle your cards.
Draw 4 cards.
Check your hand for the presence of an ace.
Repeat these steps n times, where n is the number of test you need to make. Your final, "proven" probability is a/n, where a is the number of times an ace came up.
Of course, given the nature of randomness, there's no way to ensure that your results will be near the mathematical answer, unless you have the time available to make n equal to infinity.
Unfortunately I need to 'answer' rather than comment as I would wish because my rep is not high enough to allow me to do so.
There is information missing which will make it impossible to be sure of providing a correctly functioning program.
Most importantly coming to your problem from a mathematical /probability background :
I need to know for sure how many of the reduced deck of 32 cards are aces!
Unfortunately this sentence :
Method of statistical tests to obtain
the probability of an event that of
the 4 randomly pulled charts at least
one would be ace.
is mathematical goobledygook!
You need to correctly quote the sentences given to you in your assignment.
Those sentences hold vital information on which depends what the c++ program is to simulate!