T
int i;
i = 0;
*max = *(V + i);
*min = *(V + i);
What do you want with these lines? If you just wrote i = 0 it is not clear that V+i is the same thing V?Why not initialize i on the same line you declared, the top line?Consider it could have written justvoid maxmin(int* V, int tamanho, int* max, int* min)
{ // retorna a posicao do menor e maior valor do vetor
*max = min = 0;
for (int i = 1; i < tamanho; i++)
if ((V + i) > *(V+ *max))
max = i; // novo max
else if ((V + i) < *(V+ *min))
*min = i; // novo min
return;
}
Returning values positions and not values. The way you wrote you need to call another function Posicao() and look again for the same thing, and it will waste a lot of time.printIntArray()void printIntArray(int* V, int tamanho)
{
int i, elemento;
for (i = 0; i < tamanho; i++)
{
elemento = (V + i);
printf(" %d ", elemento);
}
printf("\n");
return;
}
This nomenclature is more common in java. In C probable is print_int_array()Anyway as it has a function for this and obvious two uses at least, show the vector before and after "sort" consider using an optional title. Here’s the example I’ll leave at the end.do not need elementodeclare the loop control variable in the loop. It took many years to get that in language, but it's been 40 years.maybe it's a good idea to control the number of columnsavoid using spaces before and after an element. It only makes control difficult. Do not use " %d ". Use "%d " or " %d"Example:void printIntArray(int V, int tamanho, const char* msg)
{
if (msg != NULL) printf("%s\n", msg);
for (int i = 0, col = 0; i < tamanho; i += 1)
{
printf("%6d", (V + i));
if ( ++col % 5 == 0 ) printf("\n");
}
printf("\n");
return;
}
CreateVetor()If you have printIntArray() why not createVector()? Maybe I can use a more linear nomenclature.By reading the vector you could go repeating the total, by courtesy.TEST always the return of scanf(). If you do not read the second of 10 items will let the reading follow? What is the value for the item you have not read?Maybe I shouldn't call printIntArray() from inside CriarVetor(). Keep the function simple and generic. Just create the vector. By calling CriarVetor() the program resolves if it will call also printfIntArray(). More flexible.If sort() returns a int with the "ordered" vector because creatingVetor()` does not return a pointer to the read vector? Use a uniform logic.Consider returning the pointer to the created vector. It's normal in C.int Posicao(int* v, int size, int element)Use a uniform convention for the names of the functions: language, initial capital...int Posicao(int* v, int tamanho, int elemento)
{
int i, pos;
for (i = 0; i < tamanho; i++)
{
if ((v + i) == elemento)
{
pos = (v + i);
return pos;
}
}
return NULL;
}
You cannot return NULL. It is the same as zero then as it will differentiate if the element does not exist or if it is the first, whose index is... 0?Case i inside fordo not need posand after all you don't need that function. It is wrong: just return the indices and not the values of maxmin()I preferint posicao(int v, int tamanho, int elemento)
{
for (int i = 0; i < tamanho; i++)
if ((v + i) == elemento)
return i;
return -1;
}
returning the index or -1sort()int* sort(int* v, int tamanho, int* v_ord)
{
int i, min, max, pos_min, pos_max;
maxmin(v, tamanho, &max, &min);
pos_min = Posicao(v, tamanho, min);
pos_max = Posicao(v, tamanho, max);
*(v_ord + 0) = min;
free(v + pos_min);
free(v + pos_max);
for (i = 1; i < tamanho - 1; i++)
{
*(v_ord + i) = *(v + i - 1);
if (v == NULL) { return NULL; }
}
*(v_ord + tamanho - 1) = max;
return v_ord;
}
Supposing that sort() will create and return the "ordered" vector as it returns int*, the question remains: what is this v_ord? It doesn't make sense.And what are those calls to free()? The argument for free must be something that came from malloc(). Can you imagine removing something from the middle of the vector? It doesn't work. And anyway the output is another vector, and I imagine it is that v_ord.
That part is wrong.And here you can see how much simpler it is to return the positions of the greatest and the lowest value, instead of returning the value and then have to go again in the vector to look for the position you had already found...Compare:int* sort(int* v, int tamanho)
{ // retorna o vetor com o menor no inicio
// e o maior no fim
int min;
int max;
maxmin(v, tamanho, &max, &min);
int* v_ord = (int*)malloc(tamanho * sizeof(int)); // cria o novo
for (int i = 0; i < tamanho; i++)
*(v_ord + i) = *(v + i); // copia tudo
int um = *(v_ord); // salva o primeiro
*(v_ord) = *(v_ord + min);
*(v_ord + min) = um; // inverte
um = *(v_ord + tamanho - 1); // o ultimo
*(v_ord + tamanho - 1) = *(v_ord + max);
*(v_ord + max) = um; // inverte
return v_ord;
}
The logic of maxmin() could very well estr included in sort()main()Considering what I wrote about routines, see a common implementation for main()int main(void)
{
srand(210925);
int tamanho = 0;
printf("Inserir quantidade de elementos:");
if ( 1 != scanf("%d", &tamanho)) return -1;
// le e mostra original
printf("Lendo vetor de %d elementos\n", tamanho);
int* Vetor_de_inteiros = criarVetor(tamanho);
printIntArray(Vetor_de_inteiros, tamanho, "\n Vetor Original\n");
// ordena e mostra
int* Vetor_ordenado = sort(Vetor_de_inteiros, tamanho);
printIntArray(Vetor_ordenado, tamanho, "\n Vetor Ordenado\n");
// apaga tudo
free(Vetor_de_inteiros);
free(Vetor_ordenado);
return 0;
}
That's the common way to write that. I used a version criarVetor() that returns a filled vector with numbers in the range [-500,500] only to be able to test without wasting time typing.sample output for 19 numbersInserir quantidade de elementos:19
Lendo vetor de 19 elementos
Vetor Original
202 -40 -245 -461 361
-186 454 -25 16 -253
376 15 383 218 -160
346 -375 -243 -153
Vetor Ordenado
-461 -40 -245 202 361
-186 -153 -25 16 -253
376 15 383 218 -160
346 -375 -243 454
The complete code/*
https://pt.stackoverflow.com/questions/528981/aloca%c3%a7%c3%a3o-din%c3%a2mica
*/
#include <stdio.h>
#include <stdlib.h>
int* criarVetor(int);
void maxmin(int*, int, int*, int*);
int posicao(int*, int, int);
void printIntArray(int*, int, const char*);
int* sort(int*,int);
int main(void)
{
srand(210925);
int tamanho = 0;
printf("Inserir quantidade de elementos:");
if ( 1 != scanf("%d", &tamanho)) return -1;
// le e mostra original
printf("Lendo vetor de %d elementos\n", tamanho);
int* Vetor_de_inteiros = criarVetor(tamanho);
printIntArray(Vetor_de_inteiros, tamanho, "\n Vetor Original\n");
// ordena e mostra
int* Vetor_ordenado = sort(Vetor_de_inteiros, tamanho);
printIntArray(Vetor_ordenado, tamanho, "\n Vetor Ordenado\n");
// apaga tudo
free(Vetor_de_inteiros);
free(Vetor_ordenado);
return 0;
}
int* criarVetor(int tamanho)
{ // retorna o endereco do novo vetor
int* V = (int*)malloc(tamanho * sizeof(int));
// usa um numero entre -500 e +500
for (int i = 0; i < tamanho; i++)
*(V + i) = -500 + rand() % 1001;
return V;
}
void maxmin(int* V, int tamanho, int* max, int* min)
{ // retorna a posicao do menor e maior valor do vetor
*max = min = 0;
for (int i = 1; i < tamanho; i++)
if ((V + i) > *(V+ *max))
max = i; // novo max
else if ((V + i) < *(V+ *min))
*min = i; // novo min
return;
}
int posicao(int* v, int tamanho, int elemento)
{ // retorna a posicao do primeiro com esse valor
for (int i = 0; i < tamanho; i++)
if (*(v + i) == elemento) return i;
return -1;
}
void printIntArray(int* V, int tamanho, const char* msg)
{ // mostra o vetor, com um titulo
if (msg != NULL) printf("%s\n", msg);
for (int i = 0, col = 0; i < tamanho; i += 1)
{
printf("%6d", *(V + i));
if ((++col)%5 == 0) printf("\n"); // 5 por linha
}
printf("\n");
return;
}
int* sort(int* v, int tamanho)
{ // retorna o vetor com o menor no inicio
// e o maior no fim
int min;
int max;
maxmin(v, tamanho, &max, &min);
int* v_ord = (int*)malloc(tamanho * sizeof(int)); // cria o novo
for (int i = 0; i < tamanho; i++)
*(v_ord + i) = *(v + i); // copia tudo
int um = *(v_ord); // salva o primeiro
*(v_ord) = *(v_ord + min);
*(v_ord + min) = um; // inverte
um = *(v_ord + tamanho - 1); // o ultimo
*(v_ord + tamanho - 1) = *(v_ord + max);
*(v_ord + max) = um; // inverte
return v_ord;
}
Maybe you think it's simpler to write like that. It's common in C.