
-----------------------------------
Naveg
Sun Oct 16, 2005 12:25 pm

Trick Question?
-----------------------------------
This is a question from my Java book:

Use a 1-dimensional array to solve the following problem: write an application that inputs 5 numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a nmber already read. Provide for the "worst case",  in which all 5 numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value.

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.

-----------------------------------
wtd
Sun Oct 16, 2005 1:03 pm


-----------------------------------
Would you need to keep track of the last number input?

-----------------------------------
rizzix
Sun Oct 16, 2005 1:16 pm


-----------------------------------
it should be 4 values long...

-----------------------------------
Naveg
Sun Oct 16, 2005 1:41 pm


-----------------------------------
how? int variable for first number and an array for the rest?

-----------------------------------
zylum
Sun Oct 16, 2005 1:57 pm


-----------------------------------
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  :?

-----------------------------------
wtd
Sun Oct 16, 2005 1:58 pm


-----------------------------------
No... you need to keep track of the first number to compare it to subsequent input numbers, right?

-----------------------------------
Naveg
Sun Oct 16, 2005 2:06 pm


-----------------------------------
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.

-----------------------------------
zylum
Sun Oct 16, 2005 3:48 pm


-----------------------------------
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.

-----------------------------------
rizzix
Sun Oct 16, 2005 5:00 pm


-----------------------------------
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

-----------------------------------
Naveg
Sun Oct 16, 2005 5:16 pm


-----------------------------------
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

-----------------------------------
beard0
Sun Oct 16, 2005 9:40 pm


-----------------------------------
Use an array with length two - use aa[0] * Math.pow(100, n)

By using a combination of divison and modulus, you have access to each stored number.

-----------------------------------
eNc
Sun Nov 20, 2005 4:08 pm


-----------------------------------
forgive my stupidity but what is a 1 dimesional array. I thought you could only have a 2d array and up.

-----------------------------------
wtd
Sun Nov 20, 2005 4:24 pm


-----------------------------------
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.
