Ordering of string vectors and numbers

3

I have a problem with a facul exercise. Basically it is a store system, where I enter with the amount of stores, the quantity of products, the name of the products, and the quantity of products in each store. So far so good, my problem is in the fact that I have to print the array that contains the products in alphabetical order, ie, I need to create a function that orders a vector of strings, 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 will not get the names in order but the quantities of the wrong products. My idea would be to go through the array of strings and identify where the smallest elements are in ascending order and add their indices to an auxiliary array, so that at the time of printing the array with the names and with the products I follow the indices of that auxiliary matrix, and not a for, for example:

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

Creating the auxiliary array and saving the indexes of the arrayName, like this:

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

And then when I print, I use the values contained in the Aux array:

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

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

But I still can not think how to do this, I'll leave my code down here and if you can help me, I'll be very grateful.

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);
}
    
asked by anonymous 03.05.2016 / 22:07

1 answer

1

Nick, I believe that using multiple arrays you end up using the same amount of memory (or even more) using a single array 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

So you would have an array of products, your "index" (which may serve other purposes) and its unit price.

When you print this matrix, you just need to do a SORT on it or make SORT for a new matrix. Obviously you can still have different product arrays for different types of foods (vegetables, vegetables, fruits).

If you think your lines might be the ones above, you'll find that having an alphabetical index array is just creating more memory demand, no need.

BUT, following what you want, I think that just doing a Bubble-Sort in your array of names already performs what you want - that is, the array will be drawn whenever it is printed, which should not create demand of CPU if using a good algorithm of SORT.

Check out Google about SORT STRING ALGORITHMS and you will see very good things ...

I hope I have helped.

    
04.05.2016 / 01:05