| RE: < Note 1897.0 by RAVEN1::EVERHART >
-< Need help with sorting program. >-
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define nswap(x,y) (temp=(x), (x)=(y), (y)=temp)
extern void Zheapsort (int [],int) <<<<<<<<<< try this, it should compile!
Regards, Som Tenna
main()
{
int a[10], n=0;
while (n<10) scanf("%d",&a[n++]);
Zheapsort (a,9);
n=0;
while (n<10) printf("%d",a[n++]);
}
static void adjust(v,m,n)
int v[], m;
register int n;
{
register int *b, j, k, temp;
b=v-1;
j=m;
k=m*2;
while (k<=n)
{
if((k<n) && (b[k]<b[k+1])) ++k;
if (b[j] <b[k]) nswap (b[j], b[k]);
j=k;
k=k*2;
}
}
void Zheapsort (v1,n)
int v1[], n;
{
int *b, j, temp;
b=v1-1;
for (j=n/2; j>0; j--) adjust(v1,j,n);
for (j=n-1; j>0;j--) { nswap (b[1], b[j+1]); adjust (v1,1,j); }
}
|
| Just to add a little information to the fix in .1 ...
The first time the C compiler sees a particular function (whether
it be a declaration a definition or an invocation) it determines
the return type of that function. Type int is the default return
type of a function if none is specified. With these facts in mind...
The first time the compiler sees Zheapsort() is in the invocation
in main() "Zheapsort(a,9);". At this point the compiler determines
(by default) that the return type of Zheapsort() is int.
The next time the compiler sees Zheapsort() is in the definition
"void Zheapsort(v1,n)" where you "redefine" it's return type
to void.
So (as stated in .1) anytime you call a function before you define
it (or it's defined in another module/file) AND it returns something
other than type int THEN you must declare it (and it's return type)
before the place in the program where it is first called.
Ray
|
| OK. I see what happened now. Actually, I didn't write this, so
don't flame me for not putting in comments. A friend of mine typed
it in from a book exactly as you see it, but it didn't work. Maybe
I should put the name of the book in here so the authors can be
flamed for such a lousy example program?
I was just too tired to find the problem.
- Chris
|