| Title: | AMIGA NOTES |
| Notice: | Join us in the *NEW* conference - HYDRA::AMIGA_V2 |
| Moderator: | HYDRA::MOORE |
| Created: | Sat Apr 26 1986 |
| Last Modified: | Wed Feb 05 1992 |
| Last Successful Update: | Fri Jun 06 1997 |
| Number of topics: | 5378 |
| Total number of notes: | 38326 |
Can anyone find the bug in this program? The error is included in the listing.
- Chris
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define nswap(x,y) (temp=(x), (x)=(y), (y)=temp)
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;
^ symbol redefine error
{
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); }
}
| T.R | Title | User | Personal Name | Date | Lines |
|---|---|---|---|---|---|
| 1897.1 | Define the function before you use it! | HPSTEK::SENNA | Mon Nov 21 1988 23:30 | 55 | |
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); }
}
| |||||
| 1897.2 | Obvious to a non-C programmer, with 1%2 :-) | COOKIE::WITHERS | Trad. Anon. c. 1988 | Tue Nov 22 1988 09:25 | 4 |
Its obviously a dual problem:
fatal lack of comments
terminal use of meaningless variables
| |||||
| 1897.3 | If you want to know the details | LEDDEV::WALLACE | Tue Nov 22 1988 12:42 | 20 | |
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
| |||||
| 1897.4 | Thanks...my friend will be happy. | RAVEN1::EVERHART | Tue Nov 22 1988 13:15 | 10 | |
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
| |||||
| 1897.5 | :-) | LEDDEV::WALLACE | Tue Nov 22 1988 16:01 | 4 | |
>> I was just too tired to find the problem.
Do we get a percentage of your commision?
Ray
| |||||
| 1897.6 | Commision | RAVEN1::EVERHART | Tue Nov 22 1988 18:11 | 16 | |
re .5
Sure. I'll give you 80% of my commision for this little program.
Let's see: .80 x 0 = $0.00 - Taxes = $4530.00
Gee, I guess you owe Uncle Sam $4530.00. Sounds about right for
our tax system.
:-)
- Chris
* P.S. I have no money to spread around.
:-)
| |||||