-
December 3rd, 2013, 08:16 PM
#1
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]
-
December 3rd, 2013, 10:34 PM
#2
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 10:38 PM.
-
December 3rd, 2013, 11:50 PM
#3
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.
-
December 4th, 2013, 03:47 AM
#4
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:
If you don't understand, you need to do some basic studies first.
Nobody cares how it works as long as it works
-
December 4th, 2013, 07:27 AM
#5
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
-
December 4th, 2013, 11:49 AM
#6
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 4th, 2013, 03:45 PM
#7
Re: Explain the code please,how it outputs 2.
It is actually a sample final exam question given by our professor.
-
December 4th, 2013, 04:51 PM
#8
Re: Explain the code please,how it outputs 2.
Originally Posted by talhakhan797
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
-
December 4th, 2013, 05:26 PM
#9
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 4th, 2013, 08:01 PM
#10
Re: Explain the code please,how it outputs 2.
Originally Posted by 2kaud
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
-
December 5th, 2013, 08:13 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|