I am trying to make a program that takes an array and sorts it. When I try to compile this program, I get a message saying "undefined reference to "_Key"." What am I doing wrong?
sort.h
Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 100
#define key(A) (A)
#define less(A, B) (Key(A) < key(B))
#define exch(A, B) {int t = A; A = B; B = t;}
#define compexch(A, B) if(less(B, A)) exch(A, B)
void random_array(int a[]);
void sorted_array(int a[]);
void asorted_array(int a[]);
void selection(int a[], int l, int r);
sort.c
Code:
#include"sort.h"
void random_array(int a[]) {
int i;
for(i=0; i<N; i++)
a[i] = rand();
}
void sorted_array(int a[]) {
int i;
for(i=0; i<N; i++)
a[i] = i+1;
}
void asorted_array(int a[]) {
int i;
for(i=0; i<N; i++)
a[i] = i;
for(i=0; i<N; i+=10)
a[i] = rand();
}
void selection(int a[], int l, int r) {
int i, j;
for(i = l; i < r; i++) {
int min = i;
for(j = i+1; j <= r; j++)
if(less(a[j], a[min])) min = j;
exch(a[i], a[min]);
}
}
sortdriver.c
Code:
#include"sort.c"
int main() {
int a[N], i;
srand((unsigned) time(NULL));
random_array(a);
selection(a, 0, N);
for(i=0;i<10;i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}