Computer Science Canada Rearranging numbers |
Author: | Jina [ Sun Jun 05, 2005 5:04 pm ] |
Post subject: | Rearranging numbers |
hi, I have a problem in this program. The program is supposed to randomly generate 10 numbers from 0 to 9 only once . The output should not be duplicate numbers. After that, the user has to select any number from 1 to 10 so that he/she will start reversing the order until the order is in numerical form that is : 0,1,2,3,4,5,6,7,8,9 then the program will count how many numbers they inputted and we have to use counter to show the user how many numbers they inputted. I really need help. I cannot use functions and boolean as this is a class assignment. I can only use single-dimensional arrays. here's my program. I need an answer tomorrow. This is a long way and this program duplicates numbers even with all the statements. Also, I do need help on how to reverse the numbers so that they come to the right order. Then the program has to exit and show the number of times a user enters a number. This is an unfinished prog. var number,finish, fin2, fin3, fin4,fin5,fin6,fin7 :int var count : array 1..10 of int randomize for i : 1..10 loop randint (count(1),0,9) finish := count(1) randint (count (2),0,9) if count (2) = finish then randint (count (2),0,9) exit when count (2) not = finish end if fin2 := count (2) randint (count(3),0,9) if count(3) = finish and count(3) = fin2 then randint (count (3),0,9) exit when count (3) not = finish and count (3) not= fin2 end if fin3 := count (3) randint (count(4),0,9) if count (4) = finish and count (4) = fin2 and count (4)= fin3 then randint (count (4),0,9) exit when count (4) not = finish and count (4) not= fin2 and count(4) not = fin3 end if fin4 := count (4) randint (count(5),0,9) if count(5)=finish and count(5)=fin2 and count(5)= fin3 and count (5) = fin4 then randint (count (5),0,9) exit when count(5) not = finish and count (5) not= fin2 and count(5) not = fin3 and count(5) not = fin4 end if fin5 := count (5) randint (count(6),0,9) if count(6) = finish and count(6)=fin2 and count(6)= fin3 and count(6) = fin4 and count(6) = fin5 then randint (count (6),0,9) exit when count(6) not = finish and count (6) not= fin2 and count(6) not = fin3 and count(6) not = fin4 and count (6)not = fin5 end if fin6 := count (6) end loop end for put finish :5.. put fin2:5.. put fin3:5.. put fin4:5.. put fin5 :5.. put fin6 :5.. |
Author: | lyam_kaskade [ Sun Jun 05, 2005 5:25 pm ] | ||||
Post subject: | |||||
To do away with if statements in your random number array: Simply assign your array from 0-9 the normal way
And then do a shuffle (that is, randomly switch the numbers)
As for the second part, I'm not sure I follow. You want the user to select numbers in the array by their location, so that they go in order. For example, if element 6 in the array was 1, and element 9 was 2, the user would select 6 then 9. Sort of like a counting program for young students. ? If that's not it I'm afraid you'll have to explain more.[/code] |
Author: | Delos [ Sun Jun 05, 2005 6:18 pm ] |
Post subject: | |
Hello. Please use [code] or [syntax] tags in the future when posting your code. As for your question: - follow lyam_kaskade's advice for generating random numbers, but you might want to use Rand.Int() instead, since it's more efficient. To expidite your check to see if numbers have been used or not, you could use a boolean array the size of your range of numbers. So if you had 10 numbers to distribute (0-9), then you'd have an array with 10 elements. You would initialize them all to 'true', and teach time an element is distributed, set it to 'false'. You can then cross reference it to make sure that the number is still available to be distributed or not. Check the Tutorials if you're not sure how to use Arrays. - as for switching numbers, you can think about this in terms of 3 variables. Let's say we have 3 buckets of paint. Bucket A has blue paint, bucket B has red paint, bucket C is empty. You want to switch the contents of A and B, but you don't want to end up with purple paint. So, you pour A into C, then B into A, and finally C into B. Now you have A with Red, B with Blue, and C empty. This is the same idea with switching numbers. The use selects a number to switch, and you switch them using a temporary variable as a stop-over. |
Author: | Jina [ Sun Jun 05, 2005 7:41 pm ] |
Post subject: | rearranging numbers |
thanks for the help but i cannot use functions or boolean as for the program, it has to display 0,1,2,3,4,5,6,7,8,9 in random order. After that , the user selects a number from 1 to 10 for that many nos. to be reversed. eg- random order 6,5,3,2,1,7,8,0,9 user inputs 4 order becomes 2,3,5,6,1,7,8,0,9 user has to keep inputting till the order becomes 0,1,2,3,4,5,6,7,8,9 then program has to count how many times it entered a number to get the series in order Cannot use functions like Rand.Int and boolean can use counter and array of int and string please send me a reply. |
Author: | Delos [ Sun Jun 05, 2005 8:24 pm ] |
Post subject: | |
When you say you 'cannot use functions', I'm supposing you mean that your teacher has explicitely instructed you not to use anything like that? Or are you just using an older version of Turing? Anyway, 'booleans' are just types, so you shouldn't have any restrictions on those. If you do, you could just as easily use an array of integers - 0 being 'off' and 1 being 'on'. If you're not allowed arrays either, then a series of such variables would have to replace them. This entire programme should be done with arrays - you assign your values to the various elements. Then you would use the 3-bucket method to switch elements as the user enters positions (so, if they saw {3, 2, 5, 4, 1} and entered '5', they'd be selecting to move element 5 which is {1}). Again, if you're not allowed to use arrays, then you would just end up making a seperate variable for each would-be element. Don't forget that after each swap is entered by the User, to do a check to see whether or not the array (or vars) are ordered. In this case, it can be done very easily by using a for loop, and comparing the value of each element to the for loop counter. |