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

Username:   Password: 
 RegisterRegister   
 Trick Question?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Naveg




PostPosted: Sun Oct 16, 2005 12:25 pm   Post subject: Trick Question?

This is a question from my Java book:

code:
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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Oct 16, 2005 1:03 pm   Post subject: (No subject)

Would you need to keep track of the last number input?
rizzix




PostPosted: Sun Oct 16, 2005 1:16 pm   Post subject: (No subject)

it should be 4 values long...
Naveg




PostPosted: Sun Oct 16, 2005 1:41 pm   Post subject: (No subject)

how? int variable for first number and an array for the rest?
zylum




PostPosted: Sun Oct 16, 2005 1:57 pm   Post subject: (No 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 Confused
wtd




PostPosted: Sun Oct 16, 2005 1:58 pm   Post subject: (No subject)

No... you need to keep track of the first number to compare it to subsequent input numbers, right?
Naveg




PostPosted: Sun Oct 16, 2005 2:06 pm   Post subject: (No 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.
zylum




PostPosted: Sun Oct 16, 2005 3:48 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sun Oct 16, 2005 5:00 pm   Post subject: (No 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
Naveg




PostPosted: Sun Oct 16, 2005 5:16 pm   Post subject: (No 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
beard0




PostPosted: Sun Oct 16, 2005 9:40 pm   Post subject: (No 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
code:
a[0] * Math.pow(100, n)


By using a combination of divison and modulus, you have access to each stored number.
eNc




PostPosted: Sun Nov 20, 2005 4:08 pm   Post subject: (No subject)

forgive my stupidity but what is a 1 dimesional array. I thought you could only have a 2d array and up.
wtd




PostPosted: Sun Nov 20, 2005 4:24 pm   Post subject: (No 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. Smile

A one-dimensional array is just your average, run of the mill array.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: