Home > Blockchain >  Array re-sort algorithm
Array re-sort algorithm

Time:06-13

I need to take out element of with index i and re-sort the array after that. For example if I have 1 2 3 4 in the array and take out the 3, I need to get 1 2 4 with the NULL elements at the end.

The elements I need to take of:

INVENTORY_AMOUNT_ARRAY[i];
INVENTORY_PRICES_ARRAY[i];
INVENTORY_NAMES_ARRAY[i];

My array definitions:

static intmax_t INVENTORY_AMOUNT_ARRAY[MAX_SIZE_OF_ARRAYS];
static intmax_t INVENTORY_PRICES_ARRAY[MAX_SIZE_OF_ARRAYS];
static char     INVENTORY_NAMES_ARRAY[MAX_SIZE_OF_ARRAYS][MAX_SIZE_OF_ARRAYS];

My function:

void cache_remove_product() {
    int i = 0;
    for (; (strlen(INVENTORY_NAMES_ARRAY[i])) != 0; i  ){}
    printf("Enter the product's name u want to remove:");
    char temp_string_for_prname[MAX_SIZE_OF_STRINGS];
    scanf("%s", &temp_string_for_prname);
    for (int i = 0; (strlen(INVENTORY_NAMES_ARRAY[i])) != 0; i  ) {
        if ((strcmp(temp_string_for_prname, INVENTORY_NAMES_ARRAY[i])) == 0) {
            memset(INVENTORY_NAMES_ARRAY[i], 0, sizeof(INVENTORY_NAMES_ARRAY[i]));
            INVENTORY_PRICES_ARRAY[i] = 0;
            INVENTORY_AMOUNT_ARRAY[i] = 0;
            int j = 0;
            for (;;) {
            }       
        }
    }
}

I need the algorithm to be placed in the blank for

CodePudding user response:

There are multiple issues in the code:

  • the first loop is useless, just remove it

  • you must tell scanf() a maximum count of characters to store into the destination array to avoid undefined behavior on invalid input. There is no simple way to do this with scanf() is the length of the array is not an explicit constant. I suggest using fgets() this way:

    char prname[MAX_SIZE_OF_STRINGS   1];
    if (!fgets(prname, sizeof prname, stdin))
        return;
    size_t len = strlen(prname);
    if (len > 0 && prname[len - 1] == '\n')
        prname[--len] = '\0';
    if (len == 0)
        return;
    
  • the test for end of array can be simplified as

    for (int i = 0; INVENTORY_NAMES_ARRAY[i][0] != '\0'; i  ) {
    
  • to remove the matching entry from all arrays, you must copy each of the following entries to the previous position and clear the last entry.

Note that there is no need to re-sort the array as removing an element by shifting all subsequent elements one position does not change the relative order of the remaining elements.

Here is a modified version:

#include <stdio.h>

static intmax_t INVENTORY_AMOUNT_ARRAY[MAX_SIZE_OF_ARRAYS];
static intmax_t INVENTORY_PRICES_ARRAY[MAX_SIZE_OF_ARRAYS];
static char     INVENTORY_NAMES_ARRAY[MAX_SIZE_OF_ARRAYS][MAX_SIZE_OF_ARRAYS];

void cache_remove_product(void) {
    int i, j;
    printf("Enter the product's name u want to remove:");
    char prname[MAX_SIZE_OF_STRINGS   1];
    if (!fgets(prname, sizeof prname, stdin))
        return;
    size_t len = strlen(prname);
    if (len > 0 && prname[len - 1] == '\n')
        prname[--len] = '\0';
    if (len == 0)
        return;
    // remove all matching entries
    for (i = 0; INVENTORY_NAMES_ARRAY[i][0] != '\0';) {
        if (strcmp(prname, INVENTORY_NAMES_ARRAY[i]) == 0) {
            // move subsequent entries
            for (j = i; j < MAX_SIZE_OF_ARRAYS - 1; j  ) {
                strcpy(INVENTORY_NAMES_ARRAY[j], INVENTORY_NAMES_ARRAY[j   1]);
                INVENTORY_PRICES_ARRAY[j] = INVENTORY_PRICES_ARRAY[j   1];
                INVENTORY_NAMES_ARRAY[j] = INVENTORY_NAMES_ARRAY[j   1];
                if (INVENTORY_NAMES_ARRAY[j][0] == '\0')
                    break;
            }
            INVENTORY_NAMES_ARRAY[j][0] = '\0';
        } else {
            i  ;
        }
    }
}

CodePudding user response:

You can try to swap the chosen item to the end of array one-by-one then make it null.

For example : you have given {1, 2, 3, 4, 5, 6, 7} you want to remove 3 you make the swap operation between 3 to 7 like :
step 0 {1, 2, 3, 4, 5, 6, 7}
step 1 {1, 2, 4, 3, 5, 6, 7}
step 2 {1, 2, 4, 5, 3, 6, 7}
step 3 {1, 2, 4, 5, 6, 3, 7}
step 4 {1, 2, 4, 5, 6, 7, 3}
after that you can just assign last index of the array is null.
array[lengthOfArray - 1] = NULL;
We could just swap the last element and the chosen element but if we do that our array won't be sorted anymore.

  • Related