0

I'm trying to figure out how to remove integers from an array. The homework problem has this desired output of:

Enter input file name: t1.txt 

Min Number: -3

Number  Count
2           3
1           1
-3          1

But what I'm getting is this output:

Enter file input name: t1.txt

Min Number: 1

Number          Count
2               3
10              1
2               3
1               1
2               3

Which isn't what I need as the duplicates are still showing. My issue is that the text file the teacher provides for this has 5 integers in it. Which is all fine and dandy, but that means the code to get this specific output needs to remove integers from whatever he is printing out. I'm just having a hard time figuring out exactly how to remove integers from an array like this.

I've tried to implement a std::find bit within a for loop so that it finds the duplicates and replaces those values within a array with 0, but that didn't work and it just kept chugging along ignoring that bit of code. I've also tried to sort the array, but my problem is that it won't output in the same way as the desired output which has the numbers in the same order as the .txt file that I'm reading from.

#include <iostream>
using namespace std;
int main()
{   

    //initializer section
    int dupliCounter[30];
    int txtArray[30];

    //Section to find out how many numbers are duplicates
    for (int x = 0; x < size; x++)
    {
        int temp = txtArray[x];
        for (int y = 0; y < size; y++)
        {
            if (temp == txtArray[y])
            {
                dupliCounter[x] += 1;
            }
        }
    }

    //Final formatting section
    cout << "Min Number: " << min << endl;
    cout << endl;
    cout << "Number     Count" << endl;

    for (int k = 0; k < size; k++)
    {
        if (dupliCounter[k] != 0)
        {
            cout << txtArray[k];
            cout << "       ";
            cout << dupliCounter[k] << endl;
        }
    }
}

I have it at a point where the code can detect repeated numbers, and then modifies another value that is basically counting how many times it repeats, but I can't figure out a way to get it to delete multiple instances of the same number.

Thank you for the help everyone!

Daniel Jacoby
  • 11
  • 1
  • 2
  • You can't remove things from an array in `C++` - their size is constant. Either use a magic number that represents the lack of value (not recommended) or use dynamic memory management in form of `std::vector`. – Fureeish Sep 04 '19 at 21:45
  • 1
    possible duplicate: https://stackoverflow.com/questions/879603/remove-an-array-element-and-shift-the-remaining-ones/879629 – walnut Sep 04 '19 at 21:47
  • 1
    Welcome to Stack Overflow. I think you are taking the wrong approach. There is no reason to modify the array at all. Are you familiar with `std::map`? – Beta Sep 04 '19 at 21:48
  • The code in the question does not produce the output shown in he question. – Pete Becker Sep 04 '19 at 21:49
  • I think in such a simple case, using "magic empty values" instead of a std::vector is legitimate. Especially since the values only need be printed and are not further worked with. – hugo Sep 04 '19 at 21:51
  • @hugo but it enforces bad habits that should not be practised. – Fureeish Sep 04 '19 at 22:03
  • @Fureeish Yes, it could *encourage* (not enforce!) bad style. One should be allowed to pick the best solution for the problem at hand; but I agree that **in the general case**, the "magic empty values" approach is bad. – hugo Sep 04 '19 at 22:08
  • Can't mark as duplicate because of lack of reputation but here is a [relevant response](https://stackoverflow.com/questions/879603/remove-an-array-element-and-shift-the-remaining-ones). Basically you have to overwrite memory instead of what you're used to with "deleting". – NateCraft Sep 04 '19 at 21:49
  • Title of the question is clear. Body of the question is confusing. – acraig5075 Sep 05 '19 at 06:32
  • Perhaps your question should be retitled as _How do I output the count of duplicate items in an array and in the order that they were added?_ – acraig5075 Sep 05 '19 at 07:07

2 Answers2

0

Create a 2D vector(because of dynamic size beacuse of varying numbers) of int to store the number and counter. 1st row to store the number and the second row to store the number of occurences.

std::vector<std::vector<int>> counter;

for(int i=0;i<30;i++)
{
  for(int x=0;x<counter.size();x++)
  {
    if(txtArray[i]==counter[x][0])//repeat occurrence
    {
      counter[x][1]=counter[x][1]+1;
    }
    else//First occurence
    {
      std::vector<int> new;
      new.push_back(txtArray[i]);
      new.push_back(1);
      counter.push_back(new);
    }
  }
} 
Rhnbmpl
  • 88
  • 1
  • 11
0

Corrected code. Added changes to skip dupicates in calculations.

Change 1:

for (int x = 0; x < size; x++) {
    dupliCounter[x] = 1;
}

Change 2(first loop):

if (dupliCounter[x] == 0) continue;

Change 3(nested loop):

if (x != y) continue;

Final Code:

#include <iostream>
using namespace std;
int main()
{   

    //initializer section
    int dupliCounter[30];
    int txtArray[30];
    for (int x = 0; x < size; x++) {
        dupliCounter[x] = 1;
    }

    //Section to find out how many numbers are duplicates
    for (int x = 0; x < size; x++)
    {
        if (dupliCounter[x] == 0) continue;
        int temp = txtArray[x];
        for (int y = 0; y < size; y++)
        {
            if (x != y) continue;
            if (temp == txtArray[y])
            {
                dupliCounter[x] += 1;
                dupliCounter[y] = 0;
            }
        }
    }

    //Final formatting section
    cout << "Min Number: " << min << endl;
    cout << endl;
    cout << "Number     Count" << endl;

    for (int k = 0; k < size; k++)
    {
        if (dupliCounter[k] != 0)
        {
            cout << txtArray[k];
            cout << "       ";
            cout << dupliCounter[k] << endl;
        }
    }
}
v78
  • 2,803
  • 21
  • 44