tags:

views:

256

answers:

5

i dont understand what does following code?

#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
    int k; node * l,*r;
    node (int  t){
        k=t; l=0;r=0;
    }    
};

typedef node * link;
link Max(int a[],int l,int r)
{
    int m=(r+l)/2;
    link x=new node(a[m]);
    if (l==r) return x;
    x->l=Max(a,l,r);
    x->r=Max(a,m+1,r);
    int u=x->l->k;
    int v=x->r->k;
    if (u>v) 
        x->k=u;  
    else x->k=v;
    return x;

}

int main()
{
    int *a=new int[100];
    int n=sizeof(a)/sizeof(a[0]);
    for (int i=0;i<n;i++){
        a[i]=rand() ;        
    }
    //for (int i=0;i<n;i++){

    //  cout<<a[i]<< "  ";
    //}
    cout<<Max(a,0,n-1)<< "  ";

    return 0;
}
+2  A: 

Well, I only got as far as these lines: (starting from main())

int *a=new int[100]; 
int n=sizeof(a)/sizeof(a[0]); 

and I realized that it was broken, and figured that the rest wasn't worth my time.

James Curran
Why the down vote? The code clearly does not do what it was intended to do. Not being a mind reader, it's a waste of time to try to figure out what the author's intentions were...
James Curran
I wouldn't upvote it, but i don't understand the downvote either.
Constantin
upvoted for stupid downvote
Joel
Stupid downvote? What if everyone posted "This question isn't worth my time" to every bad code snippet? That's hardly a useful answer. (the downvote wasn't me, but I give -1 in spirit)
indiv
guys it is not my code it is code from algorithms in c++ by robert sedgewick so :)
Sedgewick really wrote that ?!
Cedric H.
yes really most of his code does not work
I agree that this belongs as a comment, not an answer.
Gabe
I was the downvoter. I figured it would be obvious why I did it, but apparently not. I guess now I'll start submitting flippant non-answers and raking in the rep.
Mike Daniels
+12  A: 

On my machine, it leaks about 1300 bytes, misuses the rand function, prints an object's memory address instead of the value it contains, and uses a needlessly complex and highly obfuscated algorithm to compute the maximum value.

John Dibling
On my machine, it summons a minor demon. Now excuse me, I must go and look for the vacuum cleaner.
Marco Mariani
@Marco: Be glad it was only one of those. The nasal ones are worse. _Much_ worse. `<wimpers>`
sbi
+2  A: 

I think this code is trying to figure out the maximum number from the randomly generated 100 numbers using binary tree approach. But i think this code has bug in this line :

x->l=Max(a,l,r);

It should be

x->l=Max(a,l,m);
Anil Vishnoi
+1  A: 

This code will produce a StackOverflow on machines where sizeof(int) >= sizeof(int*). On machines where sizeof(int) < sizeof(int*), it will print some address in memory.

Alexandre C.
+5  A: 

Analyzing in sections:

Section 1:

#include <iostream>
#include <cstdlib>

These lines indicate C++. One could expect that this program may use console I/O and some standard C language functions.

Section 2:

using namespace std;

Lazy typist. Doesn't like typing std::cout instead of cout.

Section 3:

struct node{
    int k; node * l,*r;
    node (int  t){
        k=t; l=0;r=0;
    }    
};

Looks like a typical node in either a binary tree or a linked list. Given that l could be for left and r for right. These are links that could point to a left subtree and right subtree or left node and right node. Further reading required.

Section 4:

typedef node * link;

Indication of a C language programmer using C++. This declares link as a synonym for a pointer to a node.

Section 5:

link Max(int a[],int l,int r)
{
    int m=(r+l)/2;
    link x=new node(a[m]);
    if (l==r) return x;
    x->l=Max(a,l,r);
    x->r=Max(a,m+1,r);
    int u=x->l->k;
    int v=x->r->k;
    if (u>v) 
        x->k=u;  
    else x->k=v;
    return x;
}

Looks like a recursive function to traverse a binary tree, looking for the maximum data value. It may insert this correctly; only deep analysis and debugging will tell.

Section 6:

int main()
{
    int *a=new int[100];
    int n=sizeof(a)/sizeof(a[0]);
    for (int i=0;i<n;i++){
        a[i]=rand() ;        
    }
    //for (int i=0;i<n;i++){

    //  cout<<a[i]<< "  ";
    //}
    cout<<Max(a,0,n-1)<< "  ";

    return 0;
}

Looks like it should be the main function which tests the building of the binary tree and displays the results. It's correctness to be determined (by debugging or careful review).

This is a classic example of code that would be faster to rewrite than to get working.

Thomas Matthews
ahem... "Foolish lazy typist". He could have said "using std::cout;" which is better *and* less keystrokes.
James Curran