Arrays (Reading in 10 numbers)

"Write a program that reads in ten numbers to an array. The program then reads the array and displays distinct numbers (i.e. if a number appears multiple times, it is displayed only once)"

for example:

1 2 3 4 [B]5 5 [/B]6 7 [B]8 8[/B] 9

1 2 3 4 5 6 7 8 9

I am currently stuck in my program for about a couple of hours. So far, I can read in 10 numbers and print them out, but I don't know where to go from there. I'm not that experience at C++, so if someone can help me, I would appreciate it.

[QUOTE=VernonDozier;692487]You just want +rep from Narue. :)[/QUOTE]
Narue doesn't give +rep. She's an expert meanie ;)
But if you can find any site that describes sorting better/more complete then eternallyconfuzzled, be my guest to prove me wrong and I'll never link to Narue's site again! Read More

"Write a program that reads in ten numbers to an array. The program then reads the array and displays distinct numbers (i.e. if a number appears multiple times, it is displayed only once)"

for example:

1 2 3 4 5 5 6 7 8 8 9

1 2 3 4 5 6 7 8 9

I am currently stuck in my program for about a couple of hours. So far, I can read in 10 numbers and print them out, but I don't know where to go from there. I'm not that experience at C++, so if someone can help me, I would appreciate it.

You have at least two choices. Which approach you take depends on personal preference and the assignment requirements:

Don't automatically read the number into the array. Only insert the number into the array if it not already there.

Insert all numbers, including duplicates, into the array. Once the array is filled in, weed out duplicates and either place them into the same array or place them into a new array.

I'd say choice 2 fits better with the assignment requirements. As I said, you can either use the same array after weeding duplicates out or use a new one. Both are doable, but I'd use a new one. So your steps would be:

Create an array that holds ten numbers.

Create a second array that can hold ten numbers.

Read ten numbers into first array.

Go through first array an element at a time.

Check whether element from first array is already in second array. If not, insert element into second array. if it is already in the second array, do nothing.

The primary mistake in your code is that it starts accepting num[] values from num[1] instead of num[0]. Its better to accept all the values of the array and then go into the next "for" loop for number comparison. As soon as a single repeated value is found, the program should omit that value from being printed. In your program, during the comparison, say, for the array {1,2,1,1,3,4,1}, when the for loop is being executed for num[0], numCopy becomes true for num[2] and num[3] but reverts back to false for num[4], thus printing the value. Also, for, say, i=5, the second loop will be doing unnecessary comparisons with num[6], num[7]... which will be junk values. Hope this helped :)

l4z3r makes a good point. Your number input loop and your display loop should be different. There is more than one way to do this program, but you definitely don't want "The distinct numbers are " to be displayed inside of a loop. Also, look closely at where you are asking for user input and count how many times that is. If it is more than ten, see whether you need to take it outside of a loop.

The sorting method you are using now is Bubble Sort. You can use another popular sorting methods like Insertion Sort, Selection Sort, Merge Sort and Quick Sort. Actually these sorting are a lot more efficient than Bubble sort. ;)

For the others: The process used by Rachmaninov is called "Bubble sort" as the smaller elements are "bubbled up" in the process. Another method called "Selection sort" may also be used. In this sorting technique, the smallest element and its position is found at each pass (say, i th pass) and is then exchanged with the i th element. Clearly, this method will be more longer and more time consuming.

Insertion sort takes less time (in most cases) then bubble-sort. Bubble-sort is (with the exception of bogusort) the slowest sorting algorithm there is (to my knowledge). They're both quadratic algorithms which won't work very well on large arrays.
Here's the link I give everyone when it comes to sorting. It has all you need to know and more :)

Insertion sort takes less time (in most cases) then bubble-sort. Bubble-sort is (with the exception of bogusort) the slowest sorting algorithm there is (to my knowledge). They're both quadratic algorithms which won't work very well on large arrays.
Here's the link I give everyone when it comes to sorting. It has all you need to know and more :)

>Its only drawback is that, while it mentions bogosort, it doesn't give an implementation.
For two reasons. First, there's little point in providing an implementation of a sort that would never be used in practice. My site is about practical programming. Second, bogosort is a pain to verify for correctness. ;)