-2

I have a list of geometric figures witch contains : code of figure and volume. For example:

001 213.1

002 414.7

003 718.3

004 414.7

005 718.3

006 114.5

I need to find duplicate values of volume and display it,so at the exit i need to get:

002 414.7

004 414.7

003 718.3

005 718.3

Please help me

struct Sfere
    {
      char codsf[5];
      float volum;
    } sf[100];
Christian
  • 13
  • 2
  • I'm not sure I understand. Do you want to sort your collection of `Sfere` by `volum`? What happened to `001` and `006` in your second output? Edit : It seems you want to find duplicate values of `volum`. Is this correct? – François Andrieux Jun 12 '19 at 18:39
  • yes , but without using STL – Christian Jun 12 '19 at 18:41
  • 3
    @Christian What is wrong with using the STL? It makes this job very easy, using `std::vector` and `std::sort()`, for instance. – Remy Lebeau Jun 12 '19 at 18:47
  • The simplest fast way probably is to sort the array by volume which will put equivalent volumes next to each other. Then you could just scan the list looking for volumes that appear multiple times. – eesiraed Jun 12 '19 at 18:48
  • 3
    @bruno "*(std::\* are part of C++ since C++11)*" - standard containers and algorithms have existed in C++ long before C++11 came along. – Remy Lebeau Jun 12 '19 at 18:48
  • @RemyLebeau Probably some assignment requirements. – eesiraed Jun 12 '19 at 18:49
  • @bruno you are still mistaken – Remy Lebeau Jun 12 '19 at 18:50
  • @Christian what exactly do you not understand? Please be more specific. – Remy Lebeau Jun 12 '19 at 18:50
  • @bruno yes, they were part of the C++ standard way before C++11, as François said. – Remy Lebeau Jun 12 '19 at 18:51
  • 1
    Is this a significantly different question than https://stackoverflow.com/q/56531869/212858 and https://stackoverflow.com/q/56547185/212858 ? Are you creating new users each time deliberately, or having a hard time logging back in? – Useless Jun 12 '19 at 19:01
  • Please read ["What Every Computer Scientist Should Know About Floating Point"](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) before comparing floating point values for equality. – Thomas Matthews Jun 12 '19 at 19:10
  • Please stop creating new accounts. – Konrad Rudolph Jun 12 '19 at 20:11

1 Answers1

0

Let's give this another go:

static const unsigned int ID_CAPACITY = 256;
static const unsigned int MAX_UNIQUE_VOLUMES = 512;

struct Record
{
    double volume;
    int    ids[ID_CAPACITY];
    unsigned int ids_size;
};

Record database[MAX_UNIQUE_VOLUMES];

int main()
{
    double       volume;
    int          id;
    static const double epsilon = 1e-06;
    unsigned int database_size = 0U;

    std::ifstream input_file("data.txt");
    while (input_file >> id >> volume)
    {
       bool volume_is_duplicate(false);
       // Find a duplicate volume
       for (unsigned int i = 0U; i < database_size; ++i)
       {
           const double volume_from_database = database[i].volume;
           const double diff = std::abs(volume - volume_from_database);
           if (diff < epsilon)
           {
               unsigned int id_index = database[i].ids_size;
               database[i].ids[id_index] = id;
               ++(database[i].ids_size);
               duplicate_volume = true;
               break;
           }
       }
       if (!duplicate_volume)
       {
           Record r;
           r.volume = volume;
           r.ids[0] = id;
           r.ids_size = 1;
           database[database_size] = r;
           ++database;
        }
    }
    std::cout << "\n\nPaused. Press ENTER to continue.\n";
    std::cin.ignore(100000, '\n');
    return 0;
}  

In the above program, the record is read in as two separate variables. The database is searched for an equivalent volume. If an equivalent volume exists, the new value is append to the ID container. If the volume is unique, the record is appended to the database.

Printing of the database is left as an exercise for the OP (as well as boundary/range checking).

The trick here is to organize the database by volume, not by ID. Each database record will contain IDs of equivalent volumes:

  vol.    IDs  
+-------+-----+  
| 213.1 | 001 |
+-------+-----+-----+  
| 414.7 | 002 | 004 |  
+-------+-----+-----+  
| 718.3 | 003 | 005 |  
+-------+-----+-----+  
| 114.5 | 006 |  
+-------+-----+  
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154