# String and number vector ordering

## Question:

I have a problem with a college exercise. Basically it's a store system, where I enter the number of stores, the number of products, the name of the products, and the number of products in each store. So far so good, my problem is the fact that I have to print the matrix that contains the products in alphabetical order, that is, I need to create a function that sorts a vector of strings, and not only that, I also have to change the matrix with the quantities of products as well as I change the names of the products, otherwise I will have the names in order but the quantities of the products wrong. My idea would be to go through the array of strings and identify in which position the smallest elements are in ascending order and add their indices in an auxiliary matrix, so that later when printing the matrix with the names and products I follow the indices of this auxiliary matrix, not a for, for example:

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

Creating the auxiliary matrix and saving the indices of the matrixName, 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 matrix:

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

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

But I still couldn't think of 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;

scanf("%d", &loja);

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:");

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);
}
``````

Nick, I believe that using multiple arrays you end up using the same amount of memory (or even more) as if you used 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 a matrix of products, their "index" (which can serve other purposes) and their unit price.

When printing this matrix, all you need to do is SORT it or SORT it to a new matrix. Obviously you can still have different product matrices for different types of food (vegetables, vegetables, fruits).

If you think your lines might be the ones above, you'll see that containing an alphabetical indexing array is just creating more memory demand, out of necessity.

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