Re: [Dev-C++] Print numbers up to n digits wide of number system base m.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-11-16 05:32:22
|
Sorry to be a nay-sayer, but it is quite feasable to have a number system of
base one. If you skipped that fact in the program I expect that marks would
be lost. In fact some of the earliest counting systems are unary (base one)
counting systems. To count to 3 (decimal) you would write 111 (unary). It's
like counting with matchsticks or lines. To help counting you can group
together certain amounts, such as putting a line through 4 (1111) to make 5.
It's because it became cumbersome to represent larger values that symbols
representing larger values were created. Roman numeral are still in use
today.
As I understand the question you have to count through all the possible
combinations from 1 to the largest figure available in n digits, in a number
base specified by m. 'm' can be between 0 and 11 (i.e. 1 to 10), and n can
be 1 to 80 (no more than 80 digits long).
If you look at couting in any base then there is a ripple effect from right
to left, each time the least significant digit reaches it's maximum value,
the next least significant increases by one, and the least significant
starts again from it's lowest value. The same happens in each digit until
all digits reach thier maximum, or to be more precise, when all digits have
rolled over to thier original values. All you have to do is achieve this
ripple effect and display each intermediary stage, as the number increases.
In fact there is no real reason for you to count up the numbers in sequence,
but that may be easier to get your head around.
Assuming you use an array to store the digits and work from right to left in
the array then to display all you do is skip over all zero values until you
reach a non-zero value and then output each value in turn one after the
other, that way on screen they appear as one whole number, even though you
are working with individual digit's.
I'm tempted to make a working program and present it to you, but as graeme
says, you should try for yourself. Maybe what we have said will give you
some indication as to how to go about it. What I have said is essentialy the
same as what graeme suggested, but just using different words that I hope
will help you figure it out.
You will probably have to detect specially the unary situation. Also don't
forget to check the entered values of m and n so that they fall in the range
specified in the question, or you'll lose marks.
Jason
----- Original Message -----
From: "graeme" <gr...@dr...>
To: <dev...@li...>
Cc: <do...@ho...>
Sent: Friday, November 16, 2001 3:27 AM
Subject: [Dev-C++] Print numbers up to n digits wide of number system base
m.
> m is the base system (1 < m < 11) // Base 1 doesn't make much sense...
> n is the number of digits (0 < n < 81)
>
> Set up an array of 80 chars and initialise it so that the each element is
> zero except the first which is set to 1
> // This is so that when you print it it will give 100000 for a six digit
> number
> repeat until all the numbers have been displayed
>
> add one to the least significant digit (hence the n-1 digit in the zero
> based array)
> BUT {that good old c command :-) } if the current digit is equal to m -
1
> then
> set the current digit to 0 and try with the n-2 digit // I've left
it
> particularly vague for you to work out
> // If you think about it you can use this to cause you repeat loop
> termination condition.
>
> Print out the number ( or each digit in the array from 0 to n-1) //
Maybe
> add some pauses to allow the screen to be read
> end repeat // All the numbers have been displayed have a break.
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|