I completed a given code-example as a homework and it worked fine on my 64 Bit OS X device (compiled with gcc). But it won't execute on my Windows 8 64 Bit machine(compiled with MinGW - gcc). I used gcc sort.c -Wall -Wextra on both machines. Not execute means, that the programm stucks at line 64 in an infinity loop. Further I recognized that this happend after loop reaches 11 in line 64.
It also runs on Codepad (http://codepad.org/BoLhqtzv).
I tryed to use different ways of pointer arithmetics to access the arrays but none of them worked.
The programm sorts an array x of lengh n. In array x is like a bag. So there can appear one number multiple times. The function sort gets the lengh of the Array x, a pointer on the array x and the count of different numbers (10: from 0 to 9). The trick is that the Array muh knows how often which number appears in Array x. This works because the numbers in x are continous in N(atural Numbers).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define M 10
/* This function generates an array of random integers in the range [0,M-1] of length n. */
int* random_array(const int n) {
int *x;
int i;
x = (int*) malloc(n * sizeof(int));
srand(time(NULL ));
for (i = 0; i < n; i++) {
x[i] = rand() % M;
}
return x;
}
/* print an array. */
void print_array(const int n, const int *x) {
int i;
printf("array: ");
for (i = 0; i < n && i < 32; i++) {
printf("%d ", x[i]);
}
if (n > 32) {
printf("...");
}
printf("\n");
}
/* check if a given array is sorted in ascending order. */
void is_sorted(const int n, const int *x) {
int i;
for (i = 1; i < n; i++) {
if (x[i - 1] > x[i]) {
fprintf(stderr, "ERROR: Array is not sorted!\n");
return;
}
}
printf("Array is sorted!\n");
}
/* n is the length of the array x and m is the same m as on your exercise sheet.
* In this case m is set to 10. */
void sort(const int n, int *x, int m) {
/* allocates memory for an zero initialized array */
int *muh = (int*) calloc(m, sizeof(int));
int loop; //count variable
/*counts each number in the array*/
for(loop = 0; loop < n; loop++){
muh[x[loop]]++;
}
/*Overrides x, Each number appears muh[loop] times at the beginning of line 65*/
for(loop = 0; loop < n; loop++){
for(; muh[loop] > 0; muh[loop]--) {
*(x++) = loop;
}
}
}
int main() {
int *x;
int n;
/* set length of the arrays */
n = 1 << 5;
/* get a random integer array of length n */
x = random_array(n);
/* print the unsorted array */
print_array(n, x);
printf("\n");
printf("sorted array:\n");
/* sort x by using sort, check if it is sorted and print it out */
sort(n, x, M);
is_sorted(n, x);
print_array(n, x);
return 0;
}