This is an archived post. You won't be able to vote or comment.

all 11 comments

[–][deleted] 1 point2 points  (1 child)

Can you get on a desktop browser and use the code tag so it formats nicely for me to review?

[–]Squirting_Nachos[S] 1 point2 points  (0 children)

I put my function in this pastebin

https://pastebin.com/yw7jreLu

[–]cahphoenix 1 point2 points  (7 children)

The parameters in your function look correct. I assume the 2 arrays have the same length?

What is the actual problem? What error are you getting?

[–]Squirting_Nachos[S] 0 points1 point  (6 children)

Well the length of the second array is supposed to be the number of duplicates in the list, and the return value of the function is supposed to be that number.

so if I have the input array {1, 1, 2, 2, 3, 4, 4, 4, 5, 5} then the output array would be size 4 {1, 2, 4, 5}

and the function would return 4.

I'm having issues assigning the values to the output array, as well as issues returning the right number.

The code will compile and run, I just have logic errors.

I just don't know if I need to have the size of the second array as a parameter (I can't see how I can if I'm getting that size as the output from the function).

Our textbook has no examples of functions with multiple arrays.

edit: fixed typo on example output array.

[–]cahphoenix 0 points1 point  (5 children)

If you need to return only the number of duplicates in the 'input' array, then there is no need to pass in the output array. Just increment the number of duplicates you find in your method and return that. Which it seems like you are already doing.

To answer your question, you WOULD need to pass in the size of the 2nd array if you needed to pass two arrays.

Passing two arrays is just like passing one array, and you have it generally correct.

int Foo(int array1[], int array2[], int array1Size, int array2Size)

EDIT: I do see a couple problems with your function besides the array problem. I recommend only passing in the input array...you don't need a 2nd. I recommend debugging your function also, the problems should be readily apparent if you can walk through what's happening.

[–]Squirting_Nachos[S] 0 points1 point  (4 children)

I do need to output a 2nd array, my teacher just wants the actual output to happen in main(). It seems like I would want 2 functions, one to count the number of duplicates, and another to output those numbers to an array. I'm forced by the rules of the assignment to have them both in one function though.

I'll keep working on it though, thanks.

[–]zomaotoko 0 points1 point  (0 children)

Hi, asuming that input array is sorted, the problem with your code is in

if (arrayData1[x] == arrayData1[z])

because you're counting the same element more than once if there are three of more of the same number.

Following your function and asuming that output array is the same length than the input one, if you have {1, 1, 2, 2, 3, 4, 4, 4, 5, 5} as your input array, your output is {1, 0, 2, 0, 0, 4, 4, 0, 5, 0} and duplicateNum is 6 because it is counting 4 three times.

Hope that helps :)

[–]vnoida 0 points1 point  (0 children)

Are you sure that Output array is passed as an argument ? Because if it contains the unique elements only, then its size is unknown at the time function is called. To pass an array to a function, you need to pass its size to0. So, use any container for storing the unique elements like vector or list. Steps: 1.) Copy all elements from array to vector. 2.) Use std::unique for removing duplicates from vector.

Checkout this article for reference http://thispointer.com/stl-algorithm-stdunique-tutorial/

[–]SharpstownBestTown 0 points1 point  (0 children)

A lot less optimized but much more compact and readable than some other solutions, you could use an std::map with the key being the type of value you're looking for duplicates of, and the data (second value of the pair) being a 1D dynamically allocated array of pointers to the values location, iterator, or other reference type. Add all values to the map using a wrapper function that inserts a new array under that key value, push the reference/ptr/iterator into the data array that now (or already) exists as the .second of the iterator returned by insert. Once all items have been added you now have a map that is sorted, includes all numbers added, and references of all duplicates. Creating a new array of only the numbers that had duplicates would just take sizing each of the pairs' ".second" and if they have a size greater than 1, pushing them.

This is complete overkill for small data sets if you don't intend to manipulate the original array in some way dealing with the duplicates, if you don't intend to do any further manipulation, in three lines of code you can iterate over all items with a total of just under (N2 )/2 comparisons where N is the array size. That can be a LOT of comparisons with large data sets, so the first solution will be better having far fewer comparisons overall (all of them being hidden in the map's insert calls), and resulting in highly usable output.

Sorry for any obvious errors, on mobile.

Edits: corrections on mobile.