Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 i placed the digits from 0 to 9 in random order but they must not be repeating? how do i fix this? nd help on da other1?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kid105




PostPosted: Mon Jan 18, 2010 10:17 pm   Post subject: i placed the digits from 0 to 9 in random order but they must not be repeating? how do i fix this? nd help on da other1?

var i : int
var number : array 0 .. of int
for counter1 : 0 .. 9
randint (i, 0, 9)
put i : 2 ..
end for


so far this is what i have done.

the ten numbers apear on the outputscreen but they must not repeat .

i dont know how to do this part wat should i do a little coding will help:
den a number is subsequently entered by the user causing the first N numbers in the list to be reversed .

example if 6 is chosen the order of the first six numbers is reversed

example

random numbers:

1 5 8 3 4 9 7 6 2

the first 6 numbers are reversed

9 4 3 8 5 1 7 6 2
Sponsor
Sponsor
Sponsor
sponsor
jbking




PostPosted: Tue Jan 19, 2010 9:58 am   Post subject: Re: i placed the digits from 0 to 9 in random order but they must not be repeating? how do i fix this? nd help on da oth

There are at least a couple of different ways to do the first part:

1. Create an array that has 0-9 in it, a simple for loop can do this. Then, recursively generate a random number between 0 and the number of digits remaining to know which one to put into a second array that will hold the values in a random order. So the first time a value between 0 and 9 is generated, then 0 and 8, then 0 and 7, etc. so that as items are removed, you are only choosing from what is left.

2. Generate an array, but check what you already have. This has the minor gotcha that as you build it out, there may be more time spent waiting for the collection to complete. If you want a non-technical example, take a die of at least 4 sides and roll until you get one of every value. It may take a long time near the end as you'll be getting repeats of values already generated.

I have no idea how efficient or inefficiently this can be done however, so let me give that caution on using either approach.

For the other one, consider how you would manually reverse the first 6 digits. First, the 0 and 5th indices would be swapped, then 1 and 4 and lastly 2 and 3. Do you see the pattern? Could you write one that would also work for a lower number like if you had to do just the first 3 digits? What if the number entered was 1 or 0?

I'm just tossing out ideas as I don't know what answer your teacher is expecting or how much programming you know so do beware that some of these ideas may be terrible ideas while some may be very good ideas.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: