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

Username:   Password: 
 RegisterRegister   
 sorting problems
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
phazonknight




PostPosted: Wed Jan 16, 2008 5:19 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
aDevildog




PostPosted: Wed Jan 16, 2008 5:29 pm   Post subject: Re: sorting problems

Are you using an array for storing the numbers?
phazonknight




PostPosted: Wed Jan 16, 2008 5:31 pm   Post subject: Re: sorting problems

yes im using array
Nick




PostPosted: Wed Jan 16, 2008 5:42 pm   Post subject: RE:sorting problems

use a for loop to check for duplicates

ex:
code:
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




PostPosted: Wed Jan 16, 2008 5:46 pm   Post subject: 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.

code:

int count =0;

while (int counter =0; counter !=10; counter+= Counter)
{
if (yourArray [counter] == yourArray[count]
{
..//Re enter a number code
}
}
phazonknight




PostPosted: Wed Jan 16, 2008 5:50 pm   Post subject: Re: RE:sorting problems

momop @ Wed Jan 16, 2008 5:42 pm wrote:
use a for loop to check for duplicates

ex:
code:
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




PostPosted: Wed Jan 16, 2008 5:54 pm   Post subject: RE:sorting problems

aha sorry thought it was turing since I came from the main page but my pseudo code should work
OneOffDriveByPoster




PostPosted: Wed Jan 16, 2008 6:01 pm   Post subject: Re: sorting problems

phazonknight @ Wed Jan 16, 2008 5:19 pm wrote:
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
10 is a reasonably small number that you can just check against all the numbers that are already there.
Sponsor
Sponsor
Sponsor
sponsor
phazonknight




PostPosted: Wed Jan 16, 2008 6:04 pm   Post subject: Re: sorting problems

OneOffDriveByPoster @ Wed Jan 16, 2008 6:01 pm wrote:
phazonknight @ Wed Jan 16, 2008 5:19 pm wrote:
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
10 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




PostPosted: Wed Jan 16, 2008 7:38 pm   Post subject: RE:sorting problems

code:
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();
   }
  }
}

Rolling Eyes might work, might not work, who knows Wink

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
code:
//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




PostPosted: Thu Jan 17, 2008 5:28 pm   Post subject: Re: sorting problems

thnxs everyone i got it to work thanks
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  [ 11 Posts ]
Jump to:   


Style:  
Search: