Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

C++ question. How to display the contents of an array without displaying any duplicates that may be contained. This needs to display in one column. the second column next to the first would display the occurance of each number of the array. so if number 3 is in the array 4 times next to the #3 would be the number 4. there needs to be a count next to each unique number in the array.

OpenStudy (e.mccormick):

Two questions: What have you done so far? Is the array sorted?

OpenStudy (anonymous):

I have the basic structure while it is a mess I ask the user for the size of the array with a prompt that says 30 is the max. then they input a number based on the size that stated in the previous prompt. Then the array is sorted and the largest number is displayed. I also have sorted the array to display smallest to largest. What I am left figuring out which I am clueless and hours upon hours of google searching is doing me no good. I need to display that sorted smallest to largest list without any duplicates in a column on the left. In a separate column to the right of these numbers I need to display the frequency of duplicates for each number.

OpenStudy (rsmith6559):

How about for each time the number in the array changes, you start a while( array[ indice ] == number ) loop that increments the numberOfOccurances, and when the value changes, outputs the count of number, and starts over with the new number.

OpenStudy (e.mccormick):

What rsmith is decribing is sometimes called a fall forward. As you go through the array, you keep track of where you are and where you have been and increment a counter. When where you are is different from where you have been you print out where you have been and the counter. Then reset the counter to 1 and keep going. So if it changes each time it prints 1, but if it does not change it becomes 2, 3, etc. As you step through the array you print out results only if things have changed and then fall forward to the next time it changes before printing results again.

OpenStudy (anonymous):

That sounds great and all but can someone please provide the code as I understand the general theory but I a cannot seem to get the code right. I provided my code to see where I am at so far I need help or a push in the right direction in the form of necessary code to finish what I have started. Thanks...

OpenStudy (e.mccormick):

In psudocode, what I said is: ``` counter = 0 for i = 0 to array.length { if counter == 0 || array[i] == array[i-1] counter++ else { print "nubmer: " array[i] " count: " counter counter = 0 } } ``` Note the use of or. The left hand side of or evaluates first and is true the first time around. This prevents an out of range error for the test. You need this sort of safeguard whenever evaluating parts of an array to itself. Otherwise you might go outside the array. Also, by using a pointer and declaration of the array size after getting user input, you are avoiding part of the instructions. This is a more dynamic method, which is good, but the instructions seem to assume you would use a static array of 30 and a marker.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!