Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Checking for similar items in sets

  1. Oct 9, 2004 #1
    Hi There Everyone!

    I was wondering if any of you could guide me in the right direction concerning the following problem (an algorithm would be much appreciated):

    I am writing a program for a Computer Science Project.
    The Program works as follows:
    1. It generates a random number with x items.
    2. The user then enters a set of numbers.
    3. The program then outputs how many of these numbers are in the right position
    4. This proccess continues until the user has entered the initial the random number.

    I now want to add a feature where the user enters a number of sets, each containg x items, as well as how many numbers are in the right position.
    My program should then work out which numbers are in the right positions.

    I want to know if there is some algorithm or mathematical logical process by which I could program this.


    Any help on this matter would be much appreciated!
    :smile:
     
  2. jcsd
  3. Oct 10, 2004 #2
    is this a guess the number of game?
    i guess where u say x items .. u mean x digits??

    -- AI
     
  4. Oct 10, 2004 #3
    Yes, I do mean x digits,
    4 for example.
     
  5. Oct 10, 2004 #4

    Gokul43201

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Okay, so the program generates a 4 digit number, say. Then the user is prompted to guess a list of 4 digit numbers ?

    Let's do an example, 'cause I'm not sure exactly what you have in mind - but this seems similar to a 2 person board game called "Mastermind" that I've played.

    Random number = 3579

    Now user must guess. How many guesses ? Let's say 3, for now.

    Guesses = 1234, 2345, 3456

    Now what happens ? In the first guess, the user got 1 digit right but it's in the wrong position. In the second guess, the user got 2 digits right, but they're both in wrong positions. In the third guess, the user got 2 digits right but only one of them is in the right position.

    So, what is the output ? Does it say that the user got 1 digit correct ? Does it say, in which guess the user got this digit ?

    Anyways, here's how you extract digits from a number, say a 4 digit number : (let the variable that stores this number be X)

    unit's digit, U = X mod 10 {remainder when X is divided by 10}
    ten's digit, T = ((X mod 100) - U)/10
    hundred's digit, H = ((X mod 1000) - (X mod 100))/100
    thousand's digit, M = (X - (X mod 1000))/1000

    Most programming languages have a mod (or equivalent ) function that outputs remainders.

    You can store these values into a 2-D array and compared them with values obtained from guesses.
     
    Last edited: Oct 10, 2004
  6. Oct 11, 2004 #5
    It only says that a digit is right if it is in the right position.

    But you are all missing the point. My program should solve the sequence, by using mathematical logic, when the user types in a number of sets and how many numbers are in the right positions.
     
  7. Oct 11, 2004 #6

    matt grime

    User Avatar
    Science Advisor
    Homework Helper

    Which langauge are you using for this?

    Haskell seems to offer the easiest solution from what little I know of the languages.

    Anway, given a (small, arbitrary) set of guesses there is no way of guaranteeing that you can deduce the correct answer.

    Why don't you try figuring out how you'd solve it, if you can, and try coding it. Try it for 2 digits only to begin with, or better, try it for a smaller set of digits than possibly ten in each position.
     
  8. Oct 12, 2004 #7
    I coded the program in Delphi, as I was very lazy, but I can also program in Java as well as C++ if I need to convert it.

    What matt grime posted is very true, an arbitrary set of guesses won't neccessarily be able to solve the code.

    The algorithm should still however work out which digits are in the right places, if any.

    I tried to solve the problem by using a lot of "If..Then" statements. This was first of all very tedious and secondly it was to no avail.
     
  9. Oct 12, 2004 #8

    matt grime

    User Avatar
    Science Advisor
    Homework Helper

    I'm a little unclear still: do you have a method you cannot code, or not even a method? If you can't solve it on paper you can't code it. So forget coding for now. How would you solve the problem on paper? Where did you start?
     
  10. Oct 13, 2004 #9
    I need help with the actual solving of the problem.
    Thats what I asked for - a mathematical way through which I can solve this program
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Checking for similar items in sets
Loading...