Hello
I have this sorting algorithm using pointers to function. Yet, there is one part that I dont see how it works.(See UPDATE below if you want to see how I found my dumb missing understanding).
When in the main () function there is a call to function 'ordenamiento' (sorting) and this function in turn contains a call to another function (comparacion), the pointer that receives that call to that 'comparacion' where does it actually get the values that it sends to function 'comparacion' so that this can compare if value1 is bigger than value 2. (and return either 1 or -1) I dont see anywhere it picks them from anywhere.
see here
Code:
void Ordenamiento (int (*funcion)(int , int), int *Vector,int maximo)
I dont see those int, int getting their values from anywhere so that they are being used by function 'comparacion' that the pointer *funcion points to. (Aside from the fact that I havent seen any assignment that links the pointer to function *funcion to any address, and that is something that has to happen before.
Thanks a lot
Code:
#include <stdio.h>
#include <conio.h>
// Función para comparar dos valores. Retorna 1 o -1
int Comparacion(int valor1,int valor2)
{
return valor1 > valor2 ? 1 : -1; // where do valor1 and valor 2 are actually getting their values from ? I cant see it
}
// Función para intercambiar el valor de dos posiciones en un Vector
void CambiarDatos(int posI,int posJ,int *Vector)
{
int aux=Vector[posI];
Vector[posI]=Vector[posJ];
Vector[posJ]=aux;
}
// Función para ordenar, recibe un puntero a una función
void Ordenamiento (int (*funcion)(int , int), int *Vector,int maximo)
{
for (int i=0;i<maximo-1;i++)
{
for (int j=i+1;j<maximo;j++)
{
if (funcion(Vector[i],Vector[j])>0)
CambiarDatos(i,j,Vector);
}
}
}
// Función para imprimir un vector
void Imprime_Vector(int *Vector,int maximo)
{
for (int i=0;i< maximo;i++)
printf("-\n",Vector[i]);
}
int main()
{
int Vector[10]={5,76,4,8,50,2,44,7,23,6};
printf("Original: \n");
Imprime_Vector(Vector,10);
Ordenamiento(Comparacion,Vector,10);
printf("Ordenado: \n");
Imprime_Vector(Vector,10);
}