Sorting of string and number vectors

Asked

Viewed 1,231 times

3

I’m having a problem with a college exercise. Basically it’s a shop system, where I come in with the amount of stores, the amount of products, the name of the products, and the amount of products in each store. So far so good, my problem is in the fact that I have to print the matrix that contains the products in alphabetical order, ie, I need to create a function that sorts a string array, and not only that, I also have to change the matrix with the quantities of products as well as change the name of the products, if I do not get the names in order but the quantities of the wrong products. My idea would be to go through the matrix of strings and identify in which position are the smallest elements in ascending order and add their indices in an auxiliary matrix, so that after printing the matrix with the names and with the products I follow the indexes of this auxiliary matrix, and not of a for, for example:

matrizNome[0] = macarrao
matrizNome[1] = alface
matrizNome[2] = cebola
matrizNome[3] = batata

Creating the auxiliary matrix and saving the indexes of the matrix:

matrizAux[0] = 1
matrizAux[1] = 3
matrizAux[2] = 2
matrizAux[3] = 0

And then, when printing, I use the values contained in the matrizAux:

for(i = 0; i < tamanho; i++){

    aux = matrizAux[i];
    printf("%d", matrizNome[aux]);

But I still can’t think of a way to do that, so I’m gonna leave my code down here and in case you guys can help me, I’d really appreciate it.

int ordena(char **matrizNome, int prod){

int i, j, k, auxnum;
int *vet = (int *) malloc (prod * sizeof(int));
char *aux[20];

for(i = 0; i < prod; i++){

    strcpy(aux, matrizNome[i]);

    for(j = i+1 ; j < prod; j++){

        k = strcmp(aux, matrizNome[j]);

    }
}
}

int main (int argc, char *argv[]){

int prod; //Numero de colunas
int loja; //Numero de linhas
int i, j, k, nprod, nloja, prodLoja;
int **matriz = NULL, *vet;
char **matrizNome = NULL;
char produto[20];
int tamanho;

printf("Insira a quantidade de lojas: "); //Recebe a quantidade de lojas
scanf("%d", &loja);

printf("Insira a quantidade de produtos: "); //Recebe a quantidade de produtos
scanf("%d", &prod);

matriz = (int **) malloc (prod * sizeof(int)); //Inicia a matriz com prod colunas
for(i = 0; i < prod; i++){
    matriz[i] = (int *) malloc (loja * sizeof(int));
}

matrizNome = (char **) malloc (prod * sizeof(char));

printf("\n");
gets(produto);

for(i = 0; i < prod; i++){

    printf("Insira o nome do produto %d: ", i + 1);
    gets(produto);
    matrizNome[i] = (char *) malloc (30 * sizeof(char)+1);
    strcpy(matrizNome[i], produto);
}

//Entra com os valores na matriz [produto][loja]
for(i = 0; i < prod; i++){

    for(j = 0; j < loja; j++){

        printf("Insira a quantidade de itens %d na loja %d: ", i+1, j+1);
        scanf("%d", &matriz[i][j]);
    }
}

//Pula uma linha e escreve LOJA
printf("\nLoja:");

//Imprime a quantidade de lojas
for(k = 0; k < loja; k++){

    if(k == 0){
        printf("\t\t%d", k+1); //Dois tabs depois de imprimir loja
    }
    else{
        printf("\t%d", k+1); //Um tab depois do 1
    }
}

//Imprime um pulo de linha
printf("\n");

//Imprime a matriz para o usuario
for(i = 0; i < prod; i++){

    printf("\n%s", matrizNome[i]);
    tamanho = strlen(matrizNome[i]);

    if(tamanho < 8){
        printf("\t");
    }

    for(j = 0; j < loja; j++){

        printf("\t%d", matriz[i][j]);
    }
}

//Imprime um pulo de linha
printf("\n");

 //Libera a memoria Heap onde a matriz estava armazenada
for(i = 0; i < prod; i++) free(matriz[i]);
free (matriz);
for(i = 0; i < prod; i++) free(matrizNome[i]);
free (matrizNome);
}

1 answer

1

Nick, I believe that using multiple Voce matrices ends up using the same amount of memory (or even more) as a single matrix with the name and quantities, separated by a "separator".

Something like:

 matrizNome[0] = macarrao|0001|R$ 23,00
 matrizNome[1] = alface|0202|R$ 44,00
 matrizNome[2] = cebola|0051|R$ 3,00
 matrizNome[3] = batata|0002|R$ 11,00

Therefore, Voce would have a matrix of products, its "Indice" (which can serve other purposes) and its unit price.

When printing this matrix, simply Cvoce perform a SORT on it or SORT for a new matrix. Obviously Voce may still have different product matrices for different types of foods (vegetables, fruits).

If you think that your lines can be the above, you will see that containing an alphabetical order indexer matrix is just creating more memory demand, no need.

BUT, following what Voce wants, I think that only doing a Bubble-Sort in its array of names already executes what you want - that is, the matrix will be drawn whenever you print it, which should not create CPU demand if using a good SORT algorithm.

See Google for SORT STRING ALGORITHMS and you’ll see some great things...

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.