John Matthews wrote:Hi Mark
Do you know the range of the numbers? If you do, and it's 'small' eg. 0..9, then you can just use an array of counts which is indexed by number. You go through the array, and for each number N you increment count[N].
But if the range is 'big', or you don't know it, then it becomes a bit more complicated. I would probably use a list of number/count pairs, but there are various ways to implement a 'list'.
I'm assuming this is C - if it's C++ (not my strong point) then there are likely to be better options for the big/unknown range case based around hashing.
So, C or C++, and do you know the number range?
John Matthews wrote:But if you do want to count the number of lines first, a simple way is to just read a character at a time using fgetc() until you hit EOF, incrementing your line count if the character is the end-of-line '\n' character. Then, after allocating you array, use rewind() to go back to the beginning of the file and read the numbers.
Campbell Ritchie wrote:Please put code tags round your submitted code; it is very difficult to read as it is.
Why are you using floats for money? Why are you using floats at all? Any floating‑point arithmetic has too much imprecision to use for money, and floats are far worse than doubles.
Stop thinking in terms of a 0.01¢ coin; it is a 1¢ coin, and then do all your arithmetic in integers so €1 counts as 100.
Your arithmetic looks very complicated. You can calculate the number of 200 coins (=2€) simply by dividing the money by 200. Similarly you can calculate the remainder with the remainder operator %. It only works reliably in integer arithmetic, and your values shouldn't be negative, so use unsigned instead of int numbers. That means you can't calculate change < 0, but have to test whether you have enough money to buy the tickets first.
Mike Simmons wrote:Sounds like you would need to show some code of exactly what you did.
Campbell Ritchie wrote:Please format your code correctly. Spaces around operators please, and declarations on their own line please. Line 4 is illegible, and the variable names are difficult to follow.
Please explain what you want to happen and what does happen, with examples to make the problem clear. Put some debugging instructions in, printing the value of the char and showing progress of the copying.
Please also put some instructions for the user; who knows what they are supposed to write?