[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference hydra::amiga_v1

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

1897.0. "Need help with sorting program." by RAVEN1::EVERHART () Tue Nov 22 1988 00:48

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.RTitleUserPersonal
Name
DateLines
1897.1Define the function before you use it!HPSTEK::SENNATue Nov 22 1988 02:3055
    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.2Obvious to a non-C programmer, with 1%2 :-)COOKIE::WITHERSTrad. Anon. c. 1988Tue Nov 22 1988 12:254
    Its obviously a dual problem:
    
    	fatal lack of comments
    	terminal use of meaningless variables
1897.3If you want to know the detailsLEDDEV::WALLACETue Nov 22 1988 15:4220
    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.4Thanks...my friend will be happy.RAVEN1::EVERHARTTue Nov 22 1988 16:1510
    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::WALLACETue Nov 22 1988 19:014
      >> I was just too tired to find the problem.
    Do we get a percentage of your commision?
    
    	Ray
1897.6CommisionRAVEN1::EVERHARTTue Nov 22 1988 21:1116
    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.
    :-)