Hello, I'm trying to try to write a program which allows the user to enter some numbers and get the numbers back, sorted from small to big. The only problem is that I'm stuck at sorting, normally this should work, it's the sorting algorithm I also used in java, anyone see the fault?
Code:
/* AUTH: Kevin Strijbos DATE: 17/01/2012
DESCR: includes 2 functions that combine arrays
*/
#include <stdio.h>
#define MAX_LENGTH 3
void initializeArray (int numbers[], int *pointers[]);
void initializeMinima (int numbers[], int pointers[]);
int main (void)
{
int numbers[MAX_LENGTH];
int *pointers[MAX_LENGTH];
int counter;
for (counter = 0; counter < MAX_LENGTH ;counter++)
{
printf("Geef een getal:\t\n");
scanf("%d", &numbers[counter]);
}
initializeArray(numbers,pointers);
for (counter = 0; counter < MAX_LENGTH ;counter++)
printf("Element %d:\t%d\n", counter, *pointers[counter]);
initializeMinima(numbers, pointers);
for (counter = 0; counter < MAX_LENGTH ;counter++)
printf("Element %d:\t%d\n", counter, *pointers[counter]);
return 0;
}
void initializeArray (int numbers[], int pointers[])
{
int counter;
for (counter = 0; counter < MAX_LENGTH ;counter++)
pointers[counter] = &numbers[counter];
}
void initializeMinima (int numbers[], int pointers[])
{
int temp, index, minima, counter, smallCounter;
for (counter = 0; counter < MAX_LENGTH - 1 ;counter++)
{
minima = numbers[counter];
index = counter;
for (smallCounter = counter + 1; smallCounter < MAX_LENGTH ;smallCounter++)
{
if (numbers[smallCounter] < minima)
{
minima = numbers[smallCounter];
index = smallCounter;
}
}
/* swapping minima */
temp = pointers[counter];
pointers[counter] = pointers[index];
pointers[index] = temp;
}
}