Computer Science Canada sorting problems |
Author: | phazonknight [ 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 |
Author: | aDevildog [ Wed Jan 16, 2008 5:29 pm ] |
Post subject: | Re: sorting problems |
Are you using an array for storing the numbers? |
Author: | phazonknight [ Wed Jan 16, 2008 5:31 pm ] |
Post subject: | Re: sorting problems |
yes im using array |
Author: | Nick [ Wed Jan 16, 2008 5:42 pm ] | ||
Post subject: | RE:sorting problems | ||
use a for loop to check for duplicates ex:
|
Author: | aDevildog [ 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.
|
Author: | phazonknight [ 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:
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 |
Author: | Nick [ 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 |
Author: | OneOffDriveByPoster [ 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. |
Author: | phazonknight [ 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 |
Author: | HeavenAgain [ Wed Jan 16, 2008 7:38 pm ] | ||||
Post subject: | RE:sorting problems | ||||
![]() ![]() 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
|
Author: | phazonknight [ Thu Jan 17, 2008 5:28 pm ] |
Post subject: | Re: sorting problems |
thnxs everyone i got it to work thanks |