1

So I am struggling with writing one piece of code that will remove the duplicates from an parallel array. the file will be read then processed using two arrays but that's easy the part which i cannot figure out is for example when i have numbers like this:

750  10
150  35
750  19
450  18
610  19
390  19
520  6
410  78
300  23
410  1
410  5
120  6

lets say that the first one refers to I don't know, flowers sold and the second one refers to the group that sold them. How would I write the code so that it would compare the arrays and if the group is the same added them to the previous group if found if not create one. I know I should probably use for loop but I spent tons of time and cant seem to figure it out. any help would be much appreciated. P.S. sorry for bad English its my secondary language I am learning it right now as well :) This is what i Have as of now but i cannot figure out what to put in the loops and what to send to a function. Im trying to solve this my loops and functions only no map etc.

    #include <iostream>
#include <fstream>

using namespace std;

ifstream inputFile;                   //stream object
void Duplicate(int);

int main()
{
    const int SIZE = 12;
    inputFile.open("boxes.txt");
    int candy[SIZE];
    int dept[SIZE];

    while (inputFile >> candy[SIZE] >> dept[SIZE] )
    {
        for (int i = 0; i < SIZE; i++)
        {
            if (dept[i])
                cout << "old: " << dept[i] << endl;
            Duplicate(s[i]);
            if (s[i])
                cout << "new: " << s[i] << endl;
        }

    }
    return 0;
}

void Duplicate(int s[])
{

}
uszy123345
  • 43
  • 9
  • Post some code to show us what you've tried. – Anon Mail Nov 17 '15 at 19:18
  • like I mentioned I cant figure out how to approach this i've never done anything like this using arrays – uszy123345 Nov 17 '15 at 19:28
  • Then post some pseudo code. Then ask specific questions with respect to that. What exactly can't you figure out? – Anon Mail Nov 17 '15 at 19:30
  • How would I write the code so that it would compare the arrays and if the group (second column) is the same to the one that appeared earlier add the numbers of it to the same group that appeared earlier, if not create a new group. – uszy123345 Nov 17 '15 at 19:32
  • You will need to keep the groups you've seen already in another container. So that you can look up the group when you read in the next group number. Does that ring any bells? – Anon Mail Nov 17 '15 at 19:35
  • no not really, haha. the only thing i can think of is assigning them to a variable but what if i have thousand values like that. – uszy123345 Nov 17 '15 at 19:39
  • I mentioned a container. I suggest you check out a std::map here: http://en.cppreference.com/w/cpp/container/map. – Anon Mail Nov 17 '15 at 19:41
  • sorry im not that deep into c++ i can't really use map – uszy123345 Nov 17 '15 at 19:46
  • Why not? And why don't you put your constraints in the question? – Anon Mail Nov 17 '15 at 19:53

0 Answers0