Thread: [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: Dario P. <do...@ho...> - 2001-11-15 12:10:49
|
/* Let m be an integer, greater than zero but less than eleven. Numerical strings, in which each digit is greater or equal to 0 but less than m, are numbers of the number system in the base m. The numbers 10110 and 1981, for example, are numbers of the number system in the base the 10, but only the first one is a number of the number system in the base 2. The zeros at the beginning of the number are leading zeros, unless the number is 0. Write a program that reads the integers m and n, and outputs all numbers of the number system in the base m containing n digits at the most, and without leading zeros. Each number may be output only once. You may assume that m is greater than 0, but less than 11, and that n is greater than 0 but less than 81. The numbers should be output on consecutive lines, one number per line */ I've been banging my head over this problem since last week, and i'm no closer to an answer than i was when i started. I find myself always coming back to the idea of implementing a bidimensional array as such: nsys[n][m] But i can't figure out the system of loops and conditions that would correctly solve this problem. I'm hopelessly stuck. I'll keep banging my head, but i doubt i'll answer this. I thought i'd ask for some help. Some clever hints would be better than the straight answer, as i would like to figure out as much of it by myself as i can. But i really need help. Thanks... adios, biterman. <<That government is best which governs the least... that government is best which governs not at all.>> _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp |
|
From: graeme <gr...@dr...> - 2001-11-16 03:27:59
|
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.
|
|
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
|
|
From: David N. <dne...@mi...> - 2001-11-16 18:43:59
|
I can't believe this guy suckered all of you into completing his CS assignment for him... |