|
Never presume.
|
|
|
|
|
I would however presume that if the teacher is teaching recursion and then assigns the above problem then the teacher is going to expect a code that does recursion. Whether it causes a stack overflow is outside the bounds of what the teacher is teaching. Although perhaps a teacher that is clever (or trying to be clever) might even provide inputs that would cause an overflow just so the students would need to fumble about with that.
|
|
|
|
|
If what you say is true, then the student would most likely have been able to figure the answer for him/her self.
|
|
|
|
|
I don't understand your response in terms of what they could figure out?
I know that when I first learned recursion that it took about 12/18 months for me to get it after I was first taught it. I remember it specifically because I had a very 'aha' moment studying late a night when I finally understood it.
|
|
|
|
|
Anyone who seriously considers this problem for a recursive solution must be one who has just recently learned about recursion and thinks of it as a universal cure for all the ills of the programming world.
(Any 'for' loop can easily be rewritten to a recursive solution!)
|
|
|
|
|
You wouldn't use a recursive solution in real code. But if it's a homework assignment to teach the student about recursion, as jschell suggested...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: But if it's a homework assignment to teach the student about recursion, as jschell suggested... ... it is a very poor choice.
When teaching, I did give my students a recursive problem statement to solve. Most of the solutions were recursive. The main point of this exercise was to, in the next round, show how some recursions could be rolled out to loops; the problem statement was selected for this specific purpose: Learning when not to use recursion, even when it looks like the straightforward, obvious solution.
I never wold give my students a 'linear' problem statement (such as the one in the subject line), hoping that they would discover 'Hey, this iteration can be reformualated as a recursive method! Let's do that!'
If I were a teacher of higher math, I might use something in that direction to teach them to identify recursive structures. But I was a programming teacher. That is very different.
|
|
|
|
|
kalberts wrote: it is a very poor choice
If in fact it is homework then it must serve some purpose.
So either the intent is to solve if using recursion or to solve it using a loop.
Would you claim that this is a good equation to teach the basics of a loop?
Would you claim that this equation would be 'better' to teach the basics of a loop versus being 'better' to teach the basics of recursion. Regardless of whether it is appropriate for either?
|
|
|
|
|
<block<u>quote class="quote"> trønderen wrote: Anyone who seriously considers this problem for a recursive solution
As I said "Looks like homework for recursion.."
But with my 40 years of development experience and numerous cases of recursion unrolling not to mention more than 30 years of formal designs and 20 of architecture...
Myself I would probably start by looking at the requirements to see why either solution was needed. Certainly in the past I have, after using all that experience, decided that a simple look up in a static data block would completely meet the needs of the application/requirements.
And of course that solution would be much more efficient than recursion or the similar unrolled version. In case you were unaware of that.
|
|
|
|
|
If you teach recursion to students, you should illustrate it, and create homework assignments, with problems suited for recursion.
Students should not, through inappropriate homework assignments, learn to use the wrong tools for the problem at hand.
So if that is the case here, the teacher should be blamed. Especially if the assignment is as suggested in the subject line, with no hint of recursion in the problem statement. A lot of problems can be described in a very concise form in recursive terms, but can easily be implemented as an iteration (and most students will not discover that and make a recursive implementation ... I speak of experience). Here is no trace of a recursive problem description.
|
|
|
|
|
trønderen wrote: and create homework assignments, with problems suited for recursion.
Not sure what you mean.
First of course teachers are human. So like any other group there a good ones, bad ones and a whole lot of average ones. So yes they might be using a bad example.
Second I can see where the equation can be solved by recursion. Probably too complicated both for teaching recursion and for teaching other methods as well. Although perhaps the class will also include unrolling and perhaps then it might be better (hypothetically of course.) And then perhaps the teacher feels that would be a good follow on from this specific example.
trønderen wrote: So if that is the case here, the teacher should be blamed. Especially if the assignment is as suggested in the subject line,
Quite possibly. But then what is the scenario where that specific equation is an effective tool for teaching the basics of say a 'for' loop.
Are you claiming it is a good example for that?
Or are you claiming that the teacher is still bad but in fact must be teaching about loops instead?
trønderen wrote: A lot of problems can be described in a very concise form in recursive terms, but can easily be implemented as an iteration (and most students will not discover that and make a recursive implementation ... I speak of experience). Here is no trace of a recursive problem description.
There is a lot in that.
First in my experience classroom work, never, teaches the use of the field in practical terms. Not in programming, not in engineering, not in the soft sciences, etc.
Not even in the field of teaching. One possible exception might be that if one wants to learn to be a university professor. And only a university professor. But not for example a middle school teacher.
Possible it has changed since I was in school but I haven't run into any young programmers that even know what bit fiddling is. So I doubt it.
Second, as I remember, university professors did like recursion quite bit. Probably because, as you noted, the expression is concise. They were using it to teach the algorithm and not, as I noted above, practical programming. So in that case it doesn't matter if the implementation would be impractical.
|
|
|
|
|
jschell wrote: with my 40 years of development experience and numerous cases of recursion unrolling not to mention more than 30 years of formal designs and 20 of architecture Holy mackerel, that's 90 years dude.
|
|
|
|
|
lol
Well of course that is concurrent and not sequential.
I wonder who the idiot was that went through and down voted most of the responses in this thread.
modified 22-Jun-23 14:07pm.
|
|
|
|
|
jschell wrote: I wonder who the idiot was ... Probably someone who's IQ is smaller than his shoe size.
|
|
|
|
|
|
I am confused with RegQueryValueExW and RegQueryValueExA
DWORD sz = 0
LSTATUS status = RegQueryValueExW(key, L"", 0, 0, 0, &sz);
Result:
status : 0
sz : 20
But in
DWORD sz = 0
LSTATUS status = RegQueryValueExA(key, "", 0, 0, 0, &sz);
Result:
status : 0
sz : 10
Why status is ERROR_SUCCESS (0) and sz value is different.
|
|
|
|
|
Because sz is the size of the value in bytes, not characters.
If you retrieve a string from the registry using RegQueryValueExA you get a string with 1-byte ASCII characters, and if you use RegQueryValueExW you get a string with 2-byte Unicode characters.
|
|
|
|
|
Thanks for reply but I am aware about Ansi and Unicode difference.
Here question is little different:
sz value coming different for empty value name which is strange for me.
|
|
|
|
|
What's so strange? The call is telling you the number of bytes needed to hold the value string that would be returned.
For an ASCII string, you need 10 bytes. For the Unicode version, you need 20 bytes. Read the documentation on RegQueryValueExA (ow W):
Quote: A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, this variable contains the size of the data copied to lpData.
The lpcbData parameter can be NULL only if lpData is NULL.
If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, this size includes any terminating null character or characters unless the data was stored without them. For more information, see Remarks.
---> If the buffer specified by lpData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of the lpData buffer are undefined. <---
If lpData is NULL, and lpcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the value's data.
If hKey specifies HKEY_PERFORMANCE_DATA and the lpData buffer is not large enough to contain all of the returned data, RegQueryValueEx returns ERROR_MORE_DATA and the value returned through the lpcbData parameter is undefined. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegQueryValueEx again passing the updated buffer size in the lpcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by lpcbData is unpredictable.
If the lpValueName registry value does not exist, RegQueryValueEx returns ERROR_FILE_NOT_FOUND and the value returned through the lpcbData parameter is undefined.
|
|
|
|
|
Hello Experts,
I just question in mind related to mutex.
We have 2 processes P1 and P2 using some global mutex (m1), P1 is having ownership of mutex m1 and P2 is waiting to release mutex m1 by P1 but somehow P1 got died what will happen to P2 process. Please explain.
|
|
|
|
|
|
Noting from the design or even architecture perspective anytime code is going to be 'waiting' on something else (thread, process, Rest, message, etc) then one should at least consider what would happen if it never happens.
So for example that is why one should consider timeouts. Doesn't mean you should implement that but one needs to at least put some thought into the impact on the application/enterprise if nothing every happens.
|
|
|
|
|
Is there a way to add all the elements of a vector to an integer sequentially?
So...the vector has 1, 2, and 3
The integer is 10
The result should be 11, 12, and 13
As a side note, is there a way to add a repeating number to an integer to infinity?
Example...the integer is 10 and I want to add 4 to it. Then 4 again. And again. And again in a loop
|
|
|
|
|
puckettrobinson675 wrote: is there a way to add a repeating number to an integer to infinity?
Example...the integer is 10 and I want to add 4 to it. Then 4 again. And again. And again in a loop
Yes, just add 4 in the loop. however, not infinitely but until your integer exceeds the possible nax value for its type.
See C and C++ Integer Limits | Microsoft Learn
|
|
|
|
|
Something like:
int number = 10;
std::vector vec = { 1, 2, 3 };
for (int value : vec)
{
std::cout << number + value << std::endl;
}
In the second case you need to add a check that the sum does not overflow beyond the maximum range of the integer.
|
|
|
|