Ok, it's been a long time since a challenge has been posted. Here's one:
1. Start with an array of all zeros, user defined length.
2. Prompt user to input the number of 1s to distribute through this array, must be less then the length.
3. Distribute the 1s over the array as evenly as possible.
4. Display the array.
Have fun!!
if (i_forgot && this_is_about_code)
language = c++;
Originally posted by tecknophreak Ok, it's been a long time since a challenge has been posted. Here's one:
1. Start with an array of all zeros, user defined length.
2. Prompt user to input the number of 1s to distribute through this array, must be less then the length.
3. Distribute the 1s over the array as evenly as possible.
4. Display the array.
Have fun!!
Can I just clarify
- the array is of a user defined length - so I need to include a means of him entering the length he wants?
- When you say 'even' distibution, I am assuming you mean in a reasonably symmetrical pattern - ie 8 0s and then 4 1s would be 10101010 rather than 11110000, yes?
Here is my effort to tecknophreak's recent challenge, its not perfect though. Depending on the numbers used you may end up with un-even speard of 1's over the array. For example array length = 20 and amount of ones = 11.
Im almost certain its to do with integers used in the algorithm when they are divided and the result is less than 1 it does not round the value up, so it then uses the value 0.
Originally posted by o0zi If you want it to round up, just use ceil().
And make sure that the argument to ceil() is a float or double, not an integer. In other words, this:
Code:
int x = 5, y = 22;
printf("%lf\n", ceil(y/x));
won't work, it'll print 4. The reason is that both x and y are integers, so the division is integer division, which always truncates the result. The result (4) is then passed to ceil(), which returns 4.0, which gets printed.
The fix is to either make y a float or double, or cast one or both of x and y to either float or double before doing the division, like this:
Code:
int x = 5, y = 22;
printf("%lf\n", ceil((double)y / x));
I have found an interesting result while using Perl to program the Array with equally distributed ones. Because Perl doesn't have any preset data types, when dividing two integers it automatically uses the result as a float or double. So when I calculate the GAP it is stored as a float or double. Then when I calculate START and START += GAP it is also stored as a float or double. (Start += GAP calculates the location of the next 1) Perl allows me to keep the precision until I update the array and then it truncates START to an integer value to be used in the array index. What this means is that my code produces a perfectly symmetrical array, also known as a homophone, (word or number same forwards as backward). There are only two cases where this is not true, when LENGTH / NUMONES = 2 or LENGTH is even and NUMONES is odd.
In C++ or Java you could keep the variables LENGTH, NUMONES, GAP, and INDEX or START as floats or doubles to keep the precision and then when updating the array at position INDEX or START cast INDEX or START as an integer value. This should result in the same output.
This is a solution to the previous question about precision of integer division.
Here is a holiday challege for you guys...someone write a quick duckhunt type program in c++ where your goal is to shoot santa ( or if youd like buddha, reindeer, etc...) make either the mouse or keyboard do the actual shooting, whatever you guys would like. lets just see if anyone has some holiday programming spirit.