
-----------------------------------
phazonknight
Wed Jan 16, 2008 5:19 pm

sorting problems
-----------------------------------
Im trying to make a program where the user inputs 10 numbers and the program sorts it from greatest to least, that i have no problem, but i have to make sure the user inputs no duplicate numbers, and if the user does, i want the program to be able to tell the user its a duplicate number and allow them to choose another number to replace it

-----------------------------------
aDevildog
Wed Jan 16, 2008 5:29 pm

Re: sorting problems
-----------------------------------
Are you using an array for storing the numbers?

-----------------------------------
phazonknight
Wed Jan 16, 2008 5:31 pm

Re: sorting problems
-----------------------------------
yes im using array

-----------------------------------
Nick
Wed Jan 16, 2008 5:42 pm

RE:sorting problems
-----------------------------------
use a for loop to check for duplicates

ex:loop
    for i:1..upper(num)
        if curNum = num(i) then
            re-enter num
        end if
    end for
    exit when all numbers are diffrent
end loop

-----------------------------------
aDevildog
Wed Jan 16, 2008 5:46 pm

Re: sorting problems
-----------------------------------
Just thinking out loud, basically, but couldnt you use a while statment to check to see if the newest number has allready been entered?

In my opinion, you'll need a counter to keep track of your entries. 


int count =0;

while (int counter =0; counter !=10; counter+= Counter)
{ 
if (yourArray [counter] == yourArray[count]
{
..//Re enter a number code
}
}


-----------------------------------
phazonknight
Wed Jan 16, 2008 5:50 pm

Re: RE:sorting problems
-----------------------------------
use a for loop to check for duplicates

ex:loop
    for i:1..upper(num)
        if curNum = num(i) then
            re-enter num
        end if
    end for
    exit when all numbers are diffrent
end loop

im not sure wat u mean here is the sorting portion of the code, the code is in a method so the main class and end class } wont be there
 public static void sort ()
    {
        int num = 10;
        int counter = 0;
        int tmp = 0;

        c.println ("Please enter 10 numbers");
        //Delcares the array
        int data[] = new int [num];

        //Loop which gets values of the arrays
        for (int k = 0 ; k < num ; k++)
        {
            c.print ("Please enter #" + (k + 1) + ": ");
            data [k] = c.readInt ();

            for (int l = 0 ; l < num ; l++)
            {
                if (data [k] == data [l])
                {
                    counter = counter + 1;
                }
                if (counter > 1)
                {
                    c.println ("Duplicate number");

                }
            }
        }
        //Prints the original order of the numbers
        c.print ("The numbers you entered in the orginal order are: ");
        for (int m = 0 ; m < num ; m++)
        {
            c.print (data [m] + " ");
        }



        //Sorts the arrays by insertion sort
        for (int j = 1 ; j > num ; j++)
        {
            int i = j - 1;
            tmp = data [j];
            while ((i >= 0) && (tmp < data [i]))
            {
                data [i + 1] = data [i];
                i--;
            }
            data [i + 1] = tmp;
        }


        //Loop that prints the numbers that are being sorted
        c.println ();
        c.print ("The numbers in order are: ");
        for (int n = 0 ; n < num ; n++)
        {
            c.print (data [n] + " ");
        }
the portion where it talks about the duplicate number is where my problem lies any assistance is greatly appreciated

-----------------------------------
Nick
Wed Jan 16, 2008 5:54 pm

RE:sorting problems
-----------------------------------
aha sorry thought it was turing since I came from the main page but my pseudo code should work

-----------------------------------
OneOffDriveByPoster
Wed Jan 16, 2008 6:01 pm

Re: sorting problems
-----------------------------------
Im trying to make a program where the user inputs 10 numbers and the program sorts it from greatest to least, that i have no problem, but i have to make sure the user inputs no duplicate numbers, and if the user does, i want the program to be able to tell the user its a duplicate number and allow them to choose another number to replace it10 is a reasonably small number that you can just check against all the numbers that are already there.

-----------------------------------
phazonknight
Wed Jan 16, 2008 6:04 pm

Re: sorting problems
-----------------------------------
Im trying to make a program where the user inputs 10 numbers and the program sorts it from greatest to least, that i have no problem, but i have to make sure the user inputs no duplicate numbers, and if the user does, i want the program to be able to tell the user its a duplicate number and allow them to choose another number to replace it10 is a reasonably small number that you can just check against all the numbers that are already there.

im sorry i dont know what you mean, im kinda new when it comes to sorting, i recently learned bubble, selection and insertion sorting methods

-----------------------------------
HeavenAgain
Wed Jan 16, 2008 7:38 pm

RE:sorting problems
-----------------------------------
for (int i = 0; i < array.length; i++)
{
 for (int j = 0; j < array.length; j++)
 {
   if ( i == j) continue;
   if (array[i] == array[j]){
    System.out.println("get a new number");
    array[j--] = input.nextInt();
   }
  }
}
:roll: might work, might not work, who knows ;)

in your code, you uh, do not need a "counter" to count how many duplicated numbers, all you gotta do is, if its duplicated, tell them it is, and get a new one, then go back 1 step, and check again to see if it is duplicated.

and uhh
 //Sorts the arrays by insertion sort
for (int j = 1 ; j > num ; j++)
{
int i = j - 1;
tmp = data [j];
while ((i >= 0) && (tmp < data [i]))
{
data [i + 1] = data [i];
i--;
}
data [i + 1] = tmp;
} this part will "never" run in your case, since num is equal to 10, and j is starting at 1, and 1 is not greater than num(10), so it will just skip this loop, you should look into the sort too, i believe theres some problem with it as well...

-----------------------------------
phazonknight
Thu Jan 17, 2008 5:28 pm

Re: sorting problems
-----------------------------------
thnxs everyone i got it to work thanks
