Computer Science Canada Trick Question? |
Author: | Naveg [ Sun Oct 16, 2005 12:25 pm ] | ||
Post subject: | Trick Question? | ||
This is a question from my Java book:
Is this a trick question, or is the smallest possible array 5 values long? Note that the book has not yet taught ArrayList or any variable-length data structure yet. |
Author: | wtd [ Sun Oct 16, 2005 1:03 pm ] |
Post subject: | |
Would you need to keep track of the last number input? |
Author: | rizzix [ Sun Oct 16, 2005 1:16 pm ] |
Post subject: | |
it should be 4 values long... |
Author: | Naveg [ Sun Oct 16, 2005 1:41 pm ] |
Post subject: | |
how? int variable for first number and an array for the rest? |
Author: | zylum [ Sun Oct 16, 2005 1:57 pm ] |
Post subject: | |
its 5 values. you need each element to store the input. thats assuming you cant use other vairables like ints. unless you wanna be stupid and use an array of Strings, in which case you only need 1 element |
Author: | wtd [ Sun Oct 16, 2005 1:58 pm ] |
Post subject: | |
No... you need to keep track of the first number to compare it to subsequent input numbers, right? |
Author: | Naveg [ Sun Oct 16, 2005 2:06 pm ] |
Post subject: | |
wtd wrote: No... you need to keep track of the first number to compare it to subsequent input numbers, right?
well you'd have to keep track of the first, yes, and then every other unique value that follows. |
Author: | zylum [ Sun Oct 16, 2005 3:48 pm ] |
Post subject: | |
wtd wrote: No... you need to keep track of the first number to compare it to subsequent input numbers, right?
this would be true if the numbers were given in increasing or decreasing order... this is not the case though. |
Author: | rizzix [ Sun Oct 16, 2005 5:00 pm ] |
Post subject: | |
oh ic.. it takes in all the inputs first.. calculates.. and then outputs the result.. i thought it was more like: #1 get a number #2 if is 6th input -> exit #3 if number is duplicate -> goto #1 #5 print number #6 goto #1 |
Author: | Naveg [ Sun Oct 16, 2005 5:16 pm ] |
Post subject: | |
it is kind of like that calculation is done after each input: 1. get number 2. if duplicate, discard number 3. print all collected numbers |
Author: | beard0 [ Sun Oct 16, 2005 9:40 pm ] | ||
Post subject: | |||
Use an array with length two - use a[0] for input, and a[1] for storage. initialise a[1] to 0, and after number n (0-4) is input, add
By using a combination of divison and modulus, you have access to each stored number. |
Author: | eNc [ Sun Nov 20, 2005 4:08 pm ] |
Post subject: | |
forgive my stupidity but what is a 1 dimesional array. I thought you could only have a 2d array and up. |
Author: | wtd [ Sun Nov 20, 2005 4:24 pm ] |
Post subject: | |
eNc wrote: forgive my stupidity but what is a 1 dimesional array. I thought you could only have a 2d array and up.
It's not stupidity, until after we've answered and you ask again. A one-dimensional array is just your average, run of the mill array. |