Stumped on solving a recurrence equation [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is the equation I'm working with (it's from a past exam question that I got wrong):
void foo(float[] array, int start, int end){
if((end-start) <= 1) return;
int x = (end-start) / 5;
int y = 2*x;
int z = 4*x;
foo(array,start,start+y);
for(index = y; index <z; index++){
array[index]++;
}
foo(array,start+z,end);
}
How would I go about coming up with a recurrence equation for this?
I'm not sure of the notation I should use since the function #recurrences is depending on the value of end-start...
T(1) = 1
T(N) = ____ + ____ + _____

for notation simplicity, lets call N = end-start
then:
foo(array,start,start+y); // T(2/5 * N)
for(index = y; index <z; index++) // 2/5 * N
foo(array,start+z,end); // T(N/5)
T(N) = T(2/5 * N) + 2/5 * N + T(N/5)
is that close enough?

Related

New to C++ finding results of an if and else function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
result = getResult( 2, 5)
  
int getResult(int m, int n)
{
int ans;
if (m < n)
  if (n <= 10)    
ans = m + n;
else
   ans = m * n;
else
ans = n / m;
return (ans);
}
I am stuck between 10 and 2,
does the second else apply because the second if is true? or do i still go with the first else?
For m = 2, n = 5, the first two if conditions are valid: m < n and n <= 10. Thus, ans = m + n = 7 and it's not modified later on, so we expect 7 as the answer.
This can be much easier deduced if you properly format your code (I did it for you in this case). Also, if you use {} in if/else, that's way easier and less error-prone.

How to implement this exercise (dynamic array)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}

A fibonacci like Algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For f(n) = f(n – 1) + f(n – 2^10), when 0 <= n < 2^10, f(n)=1 ,
write a function to compute f(n).(not using recursive method)
int compute_f(int n)
{
int result = 0;
...
return result
}
wondering is there any efficient way to do?
You can follow the same idea of fibonacci and do Dynamic Programming.
Pseudo code:
if n < 0:
//throw some exception
arr = new int[max(1024,n+1)]
for i = 0 to 1024:
arr[i] = 1
for i = 1024 to n+1:
arr[i] = arr[i-1] + arr[i-1024]
return arr[n]
Converting it to actual code is left for you.
Bonus: You can do it with O(1) extra space by holding an array of size 1024 and manipulating it and remembering your current place (use modolus operator) without changing the time complexity.
Here is my version, similar to the recursive version of Fibonacci.
int compute_f(int n)
{
if( n < 0)
return -1; //Error
if(n <= 1024)
return 1;
return (compute_f(n-1) + compute_f(n-1024));
}

Recursion Help Please [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11

C++ - How to get a binary representation of a decimal number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to get the binary representation of a decimal number. I have looked all over the internet but could not find anything useful.
Can anyone provide me with sample code?
Note that I want it to run on both 32-bit and 64-bit architecture.
Thanks!
Just isolate the bits one by one, starting from the highest, and print the appropriate character:
#include <limits.h>
#include <stdio.h>
void print_binary(unsigned x)
{
for (int i = sizeof(x) * CHAR_BIT; i--; )
{
putchar('0' + ((x >> i) & 1));
}
}
int main()
{
print_binary(123);
}
If you want the result in a string instead of printed to the console, I'm sure you can adjust the code.
Get the bits from the bottom up.
Then reverse the string when done.
string bits(long n)
{
string tmp ;
while ( n ) { tmp << ( n & 1 ) ? "1" : "0" ; n >>= 1 ; }
tmp= reverse( tmp) ;
return tmp ;
}
Try William Clinger's paper "How to read floating point numbers accurately".