Explain the code please,how it outputs 2.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Explain the code please,how it outputs 2.

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    6

    Question Explain the code please,how it outputs 2.

    HTML Code:
    [CODE]#include<iostream>
    using namespace std;
    #include<string>;
    
    class node {
    public:
    	int num;
    	class node * next;
    	node(int n)
    	{
    		num=n;
    		next=NULL;
    	}
    };
    
    void f(class node **n,int val)
    {
    	*n=new node(val);
    	if(val==0)
    	return;
    
    	else
    		f(&((*n)->next),val-1);
    }
    int main()
    {
    	
    	class node * t= NULL;
    	f(&t,3);
    	cout<<t->next->num;
    	system("pause");
    }
    [/CODE]

  2. #2
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Explain the code please,how it outputs 2.

    Let's start with why you think it should (or shouldn't) output 2. Then when we see your explanation, we will correct it if it isn't correct.

    Otherwise, where do we start explaining to you C++? Do we start with what main() is? What a variable is? What pointers are? What a function is? etc. etc. We have no idea what your level of knowledge of C++ is, so it makes no sense for an explanation without first seeing how much you know.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 3rd, 2013 at 09:38 PM.

  3. #3
    Join Date
    Nov 2013
    Posts
    6

    Re: Explain the code please,how it outputs 2.

    The thing i dont understand is regarding the recursion call and how the double pointers will work in this code.

  4. #4
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,611

    Re: Explain the code please,how it outputs 2.

    Do you understand what a pointer is? Do you understand what a double or triple pointer is?

    Explain why the following will crash, and if you can do that we may be able to help:

    Code:
    int *p;
    *p = 5;
    If you don't understand, you need to do some basic studies first.
    Nobody cares how it works as long as it works

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,978

    Re: Explain the code please,how it outputs 2.

    Code:
    #include <iostream>
    using namespace std;
    #include <string>
    
    class node {
    public:
        int num;
        class node * next;
        node(int n)
        {
            cout << __FUNCTION__ << ": " << n << endl;
    
            num=n;
            next=NULL;
        }
    };
    
    void f(class node **n,int val)
    {
        *n=new node(val);
    
        cout << __FUNCTION__ << ": " << hex << *n << " : " << (*n)->next << " : " << val << endl;
    
        if(val==0)
            return;
    
        else
            f(&((*n)->next),val-1);
    }
    int main()
    {
        
        class node * t= NULL;
    
        cout << __FUNCTION__ << ": " << hex << t << endl;
    
        f(&t,3);
    
        cout << __FUNCTION__ << ": " << hex << t << " : " << t->next << endl;
        
        cout << t->next->num << endl;
        system("pause");
    }
    Code:
    J:\Temp\29>29.exe
    main: 00000000
    node::node: 3
    f: 005C13C8 : 00000000 : 3
    node::node: 2
    f: 005C1408 : 00000000 : 2
    node::node: 1
    f: 005C1418 : 00000000 : 1
    node::node: 0
    f: 005C1428 : 00000000 : 0
    main: 005C13C8 : 005C1408
    2
    Best regards,
    Igor

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    Re: Explain the code please,how it outputs 2.

    The other questions is: Why write code like this? What's the purpose? It looks like 'c' code for returning values as part of a function argument without using pass by reference. This isn't good c++ code - its extremely difficult to understand what's going on. Also, you have a memory leak. You are obtaining memory in function f but never releasing it.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Nov 2013
    Posts
    6

    Re: Explain the code please,how it outputs 2.

    It is actually a sample final exam question given by our professor.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Explain the code please,how it outputs 2.

    Quote Originally Posted by talhakhan797 View Post
    It is actually a sample final exam question given by our professor.
    What was the question given to you? It couldn't have just been "why does this output 2". If that was the question, then the answer would have been

    "It outputs 2 because that is what you coded it to do".

    If it was "what is the final output?", then yes, you need to know what passing a pointer to a pointer is supposed to achieve and simple recursion (plus the basic C++ stuff such as function calling, etc.)

    But otherwise, asking these types of questions many times isn't a test of how much you know. Even if you knew what a pointer to a pointer and what recursion does, a simple slip-up with questions that have moderate to difficult syntax could yield a wrong answer (because we are humans, not computers). Granted the question isn't that difficult, but I would be careful in giving these "run the program using pencil and paper" or "run the computer in your head" questions, unless the code is simple enough to debug by hand.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    Re: Explain the code please,how it outputs 2.

    Having a question like this as part of a written exam IMO serves no really useful purpose and doesn't do much for evaluating pupils understanding. It would be better if this type of question was asked in a practical where there is a logical error that the pupil has to find.

    In a written exam to test say function parameters, IMO a better question would be along the lines of
    'Describe with examples the different ways of returning values from a function highlighting the pros and cons of each method'
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Explain the code please,how it outputs 2.

    Quote Originally Posted by 2kaud View Post
    It would be better if this type of question was asked in a practical where there is a logical error that the pupil has to find.
    Sort of like the questions that Gimpel software would ask when they are promoting their PC-Lint product.

    They would ask "The programmer expected x, but y happened. Can you spot the error?". The code was simple enough, so the questions were answerable (the usual answer was that some aspect of C++ was ignored or assumed).

    But other than that, these questions with winding logic full of loops, if statements, recursion, and difficult syntax asking "what is the output" can have an experienced programmer give the wrong answer. Not because the programmer is not smart, but because a human can easily make a misstep when attempting to mimic the computer.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,895

    Re: Explain the code please,how it outputs 2.

    >.< Can't any programming educators make any exams that aren't made of this much fail in only so many lines of code...


    1) The code has a bug (a memory leak). Ok, this might have been omited to keep the code short. In that case, you can omit/simplify even more that isn't needed to answer the question.

    2) It's (as stated) more "C with objects" than "C++"

    3) in a proper design, the next pointer wouldn't be publicly accessible.

    4) in a proper design, you wouldn't write f() in that particularly fashion. It's asking for problems and usage issues.

    5) This is probably the most illogical, far fetched and crazy scheme to try to combing recursion, pointers and linked lists.

    6) I would fire anyone writing code like that, they're a danger to your company.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center