Home > Software design >  Remove unnecessary data from string array (C)
Remove unnecessary data from string array (C)

Time:10-08

Suppose that I have an array with the following strings:

 1.00 -2000
 1.99 -2000
48.00 35000
54.99 35000
55.00 35000
55.39 35000
58.99 35000
59.00 37000
59.39 37000

The first column corresponds to a given point in time, while the second column corresponds to a sensor reading obtained from a CSV file. The data will be feed to a custom-build simulator, which expects the start & end time point for a specific sensor reading.

In other words, the expected output format should be:

 1.00 -2000
 1.99 -2000
48.00 35000
58.99 35000
59.00 37000
59.39 37000

For the moment, I have written the following code:

//
// PART 1: Tag elements to remove from array
//
int i;
double timestep, value, previousValue;
for (i = 0; i < numTestCases;   i) {
  sscanf(BUFFER[i], "%lf %lf", &timestep, &value);

  // First element, set previous value
  if (i == 0) {
    previousValue = value;
    continue;
  }

  // Current value is equal to previous value, tag unnecessary elements
  if (value == previousValue) {
    while (i   1 < numTestCases) {
        i;
      sscanf(BUFFER[i], "%lf %lf", &timestep, &value);
      if (value == previousValue)
        sprintf(BUFFER[i], ".");

      else {
        --i;
        break;
      }
    }
  }

  // Current value is different from previous value, update previous value...
  else
    previousValue = value;
}

//
// PART 2: Remove tagged elements from array, trim the array
//         & count new number of elements
//
int newNumTestCases = 0;
for (i = 0; i < numTestCases;   i) {
  if (strcmp(BUFFER[i], ".") == 0) {
    int j;
    for (j = i; j < numTestCases - 1;   j) {
      strcpy(BUFFER[j], BUFFER[j   1]);
      strcpy(BUFFER[j   1], "");
    }

    --i;
    --newNumTestCases;
  } 

  else
      newNumTestCases;
}

Aside from the code being very ugly & complicated, it doesn't work, since it generates the following output:

 1.00 -2000
 1.99 -2000
48.00 35000
54.99 35000 <-- Notice incorrect time point here
59.00 37000
59.39 37000

My question is, how should I implement in a more efficient (and readable) way the first part of the code? And is it possible to do it in such a way that the second part wouldn't be necessary?

Thanks in advance!

CodePudding user response:

I believe this will get a new buffer (savebuffer) with just the ones you want

int saveindex=0;
char *SAVEBUFFER[numTestCases 1];
for (int index = 0; index < numTestCases;   index) {    

   if ((i > 0) and (i 1 < numTestCases)) // we are not on the first or last one.
   {
      if (BUFFER[index-1] == BUFFER[index] && BUFFER[index 1] == BUFFER[index])
        continue;
      else
        SAVEBUFFER[saveindex  ] = BUFFER[index];
   }
   else
     SAVEBUFFER[saveindex  ] = BUFFER[index];

}
SAVEBUFFER[saveindex] = "<END OF BUFFER MARKER>";

You can update this if you want to suit your needs. Add in checking the values with parsing if you think it is needed. But the key here is not to try and do it "inline" Making a new buffer is SO MUCH easier -- as you can see.

CodePudding user response:

Based on Hogan's answer, I solved the problem with the following code:

double t1, t2, t3;
double v1, v2, v3;
int trimmedNumCases = 0;
for (int i = 0; i < numTestCases;   i) {
    // Only trim cases BETWEEN start & end case
    if ((i > 0) && (i   1 < numTestCases)) {
        sscanf(TMP_BUFFER[i], "%lf %lf", &t2, &v2);
        sscanf(TMP_BUFFER[i - 1], "%lf %lf", &t1, &v1);
        sscanf(TMP_BUFFER[i   1], "%lf %lf", &t3, &v3);

        if (v1 == v2 && v3 == v2)
            continue;
    }

    // Copy necessary value to final output buffer
    strcpy(OUT_BUFFER[trimmedNumCases], TMP_BUFFER[i]);
      trimmedNumCases;
}

Thanks a lot for the suggestions!

  • Related