| Q. | Is there a recursion formula i can use to solve this integration by parts problem? | Related Search: Mathematics | | | I have this problem:
integrate: x^7cos(x^4)dx
I was wondering if there was a recursion formula I could use to simplify the process of solving this problem. Thanks!
| | A. | ∫x^7cos(x^4)dx
(x^3)(x^4)cos(x^4)dx
x^4 = t
4x^3 dx = dt
∫x^7cos(x^4)dx
substitue to get
= (1/8)∫tcostdt
use by parts with u = t and v' = cost
∫uv' = uv - ∫u' v
= (1/8) [tsint - ∫sint]
= (1/8) [tsint + cost] + c
= (1/4) (x^4)(sinx^4) + (1/4)(cosx^4) + c | | | |
| Q. | How to reverse a doubly linked list using recursion in C language? | Related Search: Programming & Design | | | Please help me in writing the code for reversing a doubly linked list using recursion in C language.
| | A. | This seems like it could be a mindbender, with plenty of opportunities to get pointers hopelessly tangled, especially with recursion involved. However, it's simpler than you might think.
Given a struct listNode, defined the usual way with prev and next pointers, including any data you want, the reverse function is:
struct listNode *reverse(struct listNode *p) {
if (p == NULL) return p;
swap(&(p->next), &(p->prev));
if (p->prev == NULL) return p;
return reverse(p->prev);
}
I'm including the (p == NULL) check just in case a null pointer is passed in initially, it'll never be true in a recursive call.
The swap function is:
void swap(struct listNode **p, struct listNode **q) {
struct listNode *temp = *p;
*p = *q;
*q = temp;
}
If you have a pointer, head, to the start of your list, you can reverse the list with this call:
head = reverse(head);
head will now point to what was the tail of the list, and all prev and next pointers will have been swapped, reversing the list. | | | |
| Q. | What is the difference between recursion and iteration? | Related Search: Mathematics | | | What is the difference between recursion and iteration? In relation to maths and computers.
| | A. | there is a very nice detailed discussion written here:
[Link]
thus depending on the technicality of the denotation applied, there may be a difference in the two. but overall, they both mean to imply that there is a repetition in the process
you may also visit this:
[Link] > | | | |
| Q. | What are some pros and cons of using recursion? | Related Search: Programming & Design | | | What would you tell a beginner in C# what some pros and cons are in recursion and when to use it and when not?
The only thing I can think off as far as cons is that it takes a lot of memory and for me it seems more confusing than a loop structure would be. But I am sure there are better pros and cons.
| | A. | Sometimes it's simpler to understand and is a shorter solution to use recursion, but it uses up more memory (because each time the function is called again it adds to the stack)... so I'd recommend just using loops wherever you can. | | | |
| Q. | How do I make a Java class that turns a string into pig latin using recursion and not loops? | Related Search: Programming & Design | | | for my AP comp. science class i need to make a program that takes a string with multiple words, and turns each word into pig latin. the hard part is that i can only use recursion, not loops, can anyone help out?
| | A. | Tell your teacher that's a bit contrived... there's no reason to use recursion for such a problem (unless perhaps you're programming in lisp).
But since you MUST, look at defining the problem in terms of itself, for example:
PigLatin("This is a sentence") =
"isThay"+PigLatin("is a sentence")
once you do that you find a recursive implementation just kinda falls into place. | | | |
| Q. | How do I make a recursion equation for this situation? | Related Search: Mathematics | | | The book says "Suppose that you make an initial deposit of $100 into an account that pays 4% annual interest, compounded monthly." So how do I write a recursion equation for that? I'm confused by the compounded monthly thing because I know of it were compounded annually it would be Un = 1.04U(n-1). Thanks!
| | A. | Capital=100(1+0.04/12)^t with t expressed in month
so capital at month (n) = capital at month(n-1)*(1+0.04/12) | | | |
| Q. | How do understand recursion and write recursive methods? | Related Search: Programming & Design | | | I'm finding it extremely difficult to understand recursion. How do I write a recursive program for say division without a guess and check approach. For doing a pre-order traversal on a BST, how do I write a recursive method?
| | A. | Writing a recursive method is a bit like making a plan to line up dominoes and knock them over. You have to anticipate how each individual recursive call (domino) is going to add its part to accomplishing the whole task.
That means that the "meat" of a recursive method is not in the recursive part, but in the "end cases" the parts of the code that execute when you are not going to re-call the method, the last domino in the chain, the one that you push to start the fun.
So, let's look at your first example, a recursive program for (integer) division. The division algorithm you are trying to implement is "for positive d and n, let n(0) be n. Keep subtracting d from n(i) step by step, until in some step q, n(q) is less than d. Your answer is q."
The key is to look at the END case first. What if at the start n is already less than d? Then you did "zero steps", so your division result is 0.
In pseudocode:
----------------------
int divide(int n, int d) {
if (n < d) {
return 0;
}
....
}
----------------------
Now what if n is not less than d (greater than or equal to d)? Then we want to try another step in the division process with a smaller n. That is, run the divide function again with "the same d" and n = "the old n" - d. But once THAT divide finishes it only tells us how many subtraction steps were required for (n-d)/d. We know that n/d requires one more step. So we have to add that step tally to the result:
----------------------
int divide(int n, int d) {
if (n < d) {
return 0;
} else {
return divide( n-d, d ) + 1;
}
}
----------------------
What is that second return actually saying? It says: " I don't know how to compute the result myself, but I do know that it is ONE MORE than the result for 'divide( n-d, d )'. So I will 'pass the buck' to that method call and then just add one to whatever it gives me back."
And the process continues. We keep adding "divide" dominoes to the chain until we reach a divide operation where n has "shrunk" to be smaller than d... our end case, our zero result. Now we knock over the first domino (the last one we added to the chain), returning "0". And the dominoes begin to fall. Every time one domino knocks over another domino we add "1" to the method result until finally the first method call is the last domino to fall, and it returns the division result.
Let's try some examples:
12/18:
----------------------
divide(12,18)
---> returns 0, since 12 is less than 18
result is 0.
----------------------
20/5:
----------------------
divide(20, 5)
---> returns divide(20-5, 5) + 1
------> returns divide(15-5, 5) +1
---------> returns divide(10-5, 5) +1
------------> returns divide(5-5, 5) +1
---------------> returns 0, since 5-5 is 0, which is less than 5
and now the dominoes fall...
------------> returns 0 + 1
---------> returns 1 + 1
------> returns 2 + 1
---> returns 3 + 1
result is 4.
----------------------
8/3:
----------------------
divide(8, 3)
---> returns divide(8-3, 3) + 1
------> returns divide(5-3, 3) +1
---------> returns 0, since 5-3 is 2, which is less than 3
and now the dominoes fall...
------> returns 0 + 1
---> returns 1 + 1
result is 2.
----------------------
The thinking is the same for a pre-order traversal on a BST. FIRST think about what happens at the end case, when you reach a node with no children. Now how do you get to the end case? What happens along the way?
If it helps, model some cases:
• a BST with one node,
• a BST with two nodes with the 2nd as left child,
• two nodes with 2nd as right child,
• three nodes: parent and two children,
• three nodes: parent, left-child, right-grandchild,
• three nodes: parent, right-child, right-grandchild,
• etc.
Try to see how the dominoes fall in order to build the steps in your traversal. | | | |
|
|