How could you convert the following recursion with dynamic programming? - c

How would you convert the following recursive program with dynamic programming (DP)?
I'm just having a little trouble trying to redefine this code into a dynamic programming form. I got the base case and the general case identified, and I am aware that DP is about a "bottom-up" approach.
int add(int, int);
int main()
{
int x = 0, y = 0;
printf("Enter positive integers x, y: ");
scanf("%d %d", &x, &y);
printf("Result: %d\n", add(x, y));
return 0;
}
int add(int x, int y)
{
if(x < 0 || y < 0){
fprintf(stderr, "Negative Integer received!\n");
return -1;
}
if (x == 1 || y == 1)
return 1;
else
return add(x, y-1) + add(x - 1, y) + add(x-1, y-1);
}

Why do you want to do it in recursive way? There is an iterative way, and iterative 'almost always' beats recursive. Besides it is less code:
int DP[500][500];
memset(DP, 0, sizeof(DP));
for(int i=1; i<=x; i++) DP[i][1] = 1;
for(int i=1; i<=y; i++) DP[1][i] = 1;
for(int i=2; i<=x; i++) {
for(int j=2; j<=y; j++) {
DP[i][j] = DP[i-1][j-1] + DP[i-1][j] + DP[i][j-1];
}
}
printf("Result: %d\n", DP[x][y]);
But if you insist on recursion you can pass your DP array to function by pointer. And every time check if you calculated DP[i][j] before, if so don't calculate it again and return back:
#include <stdio.h>
#include <string.h>
void add(int x, int y, int (*M)[500])
{
if(M[x][y] > 0) return;
if (x == 1 || y == 1) {
M[x][y] = 1;
return;
}
add(x, y-1, M);
add(x - 1, y, M);
add(x-1, y-1, M);
M[x][y] = M[x][y-1] + M[x-1][y] + M[x-1][y-1];
return;
}
int main()
{
int x, y;
printf("Enter x, y: ");
scanf("%d %d", &x, &y);
int DP[500][500];
memset(DP, 0, sizeof(DP));
add(x, y, DP);
printf("Result: %d\n", DP[x][y]);
return 0;
}

Your code will cause stack overflow for all the possible x,y and z integer(negative, positive) combinations

Related

How to fix this code written in Xcode? In the main function, Xcode is displaying the error:Function definition is not allowed here

#include <stdio.h>
int f(int x, int y) {
for (int i = 10; i > 5; i--) {
if (x % i == 0) {
y = x ^ 3;
printf("x is %d and y is %d\n", x, y);
return x + y;
}
else {
y = x + 1;
printf("x is %d and y is %d\n", x, y);
return x * y;
}
}
int main() { // I am getting error on this line.Function definition is not
// allowed here.
int a = f(30, 10);
int b = f(20, 5);
return 0;
}
}
Xcode displays this as a parse issue. please help me fix this code.
You missed a } to end function f(). So by mistake you placed main() inside the function f().
The problems within your code is,
Here the closing brace } of int f(int x, int y) is missing. Please check my comments inside the code itself.
One more closing brace } is added at the end of your program which is not required.
The corrected code is
#include <stdio.h>
int f(int x, int y) {
for (int i = 10; i > 5; i--) {
if (x % i == 0) {
y = x ^ 3;
printf("x is %d and y is %d\n", x, y);
return x + y;
} //Closing brace of 'if' condition
else {
y = x + 1;
printf("x is %d and y is %d\n", x, y);
return x * y;
} //Closing brace of 'else' condition
} //Closing brace of for-loop
} //Here add the closing brace of 'int f(int x, inty)'
int main() {
int a = f(30, 10);
int b = f(20, 5);
return 0;
} //Removed the last '}' in your code

find prime numbers including and between two numbers using

I need to find prime numbers between and including two numbers, using functions.
For example, with <<(3 23)>> the output is 3 5 7 11 13 17 19 23
This is my code so far, but I’m having troubles with it. What am I doing wrong or how can I improve my solution?
#include<stdio.h>
int check_prime(int l,int u){
int x, i;
for (x = l; x <= u; x++){
for (i = 2; i < x; i++){
if (x % i == 0) break;
}
}
if (i == x) return x;
}
int main(){
int x, y, f;
scanf("%d%d", &x, &y);
f = check_prime(x, y);
printf("%d", f);
return 0;
}
You are printing the value returned from check_prime() and that will be one value only. If you want to print all the prime numbers in a range, i suggest instead of returning value from check_prime() you print the value in that function.
#include<stdio.h>
void check_prime(int l,int u){
int x,i;
for(x=l;x<=u;x++){
for(i=2;i<x;i++){
if(x%i==0)
break;
}
if(i==x){
printf("%d ", x);
}
}
}
int main(){
int x,y;
scanf("%d%d",&x,&y);
check_prime(x,y);
return 0;
}
Here is the executable code: https://repl.it/#fiveelements/PrintPrimeNumbersInARange?language=c

Testing recursive functions - C

I have a small problem on my homework here
I just need to write a recursive function to test this relation
f(x,y)=x if y==1
otherwise
f(x,y) = x + f(x,y-1)
Here is my source, I can't get it to print out correctly.
Note that I have the user enter X and Y to test.
#include <stdio.h>
int f (int x,int y);
int main (void)
{
int x, y, z;
printf ("\nEnter x: ");
scanf ("%d", &x);
printf ("\nEnter y: ");
scanf ("%d", &y);
x=f(x,y);
return 0;
}
int f (int x,int y)
{
if (y==1)
{
return x;
}
else
{
return (x + f(x,y-1));
}
}
Your recursive function should look like:
#include<stdio.h>
int main() {
int i = f(5,4);
printf("%d\n",i);
return 0;
}
int f (int x,int y)
{
if (y==1)
{
return x;
}
else
{
return (x + f(x,y-1));
}
}

C- function for printing each base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3

I can't seem to get my program running
Write a function integerPower(base, exponent) that
returns the value of
Baseexponent. For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use for to
control the calculation.
Do not use any math library functions.
i have this program
#include<stdio.h>
int power(int b, int e){
int x
for (x = 0; x <= e; x++)
b=b;
return b;
}
void main(){
int b = 0;
int e = 0;
scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);
power(b,e);
printf("%d" , b);
}
In the loop
for (x = 0; x <= e; x++)
b=b;
the line b=b; is useless. It just assign the value of b to itself. You need to multiply b by e times. For this you need to take another variable with initial value 1 and multiply it by b at each iteration of loop to get be.
Change your function to this
int power(int b, int e){
int x, y = 1;
for (x = 1; x <= e; x++)
y = y*b; // Multiply e times
return y;
}
#include<stdio.h>
int power(int b, int e){
int x;
int result = 1;
for (x = 0; x < e; x++)
result*=b;
return result;
}
int main(){
int b = 0;
int e = 0;
printf("Input a base ");
scanf("%d", &b);
printf("Input an exponent ");
scanf("%d", &e);
b = power(b,e);
printf("%d" , b);
return 0;
}
First problem is with the scanf function. you are using "input a base ". For this to output you have to use printf("input a base ").
Try using the below function to calculate the value of base raised to the power of exponent
int ipower(int b, int e)
{
int x, tmp = 1;
for (x = 0; x < e; x++)
tmp *= b;
return tmp;
}
Here the for loop is iterated for e times that is from 0 to e-1. "tmp" will hold the result.
Make sure you don't change the value of "b/e"(base/exponent) inside the loop, else will lead to wrong result.
doing b*=b in function body won't help either. And why initialize b and e to zero when you're supposed to input them from the user?
Try this:
#include<stdio.h>
int power(int b, int e) {
int temp = b;
int x;
for (x = 1; x < e; x++)
b *= temp;
return b;
}
void main() {
int b ;
int e;
scanf_s(" %d", &b);
scanf_s(" %d", &e);
int h= power(b, e);
printf("%d", h);
}
int power(int b, int e){
int x,t=1
for (x = 1; x <= e; x++)
t*=b;
return t;
}
if user give e= 1 then also its work.

Exercise in C to calculate sum from x to y

My teacher wants the sum of all numbers from x to y... like x+(x+1)+(x+2)...until y. But I think I'm doing something wrong here!
Can someone advice me what is wrong here?
#include <stdio.h>
int sum_naturals(int n)
{
return (n-1) * n / 2;
}
int sum_from_to(int m)
{
return (m-1) * m / 2;
}
void test_sum_naturals(void)
{
int x;
scanf("%d", &x);
int z = sum_naturals(x);
printf("%d\n", z);
}
void test_sum_from_to(void)
{
int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
int z = sum_naturals(x);
int b = sum_from_to(y);
printf("%d\n", z);
}
int main(void)
{
//test_sum_naturals();
test_sum_from_to();
return 0;
}
Your code should in fact be:
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_to(int m)
{
return (m+1) * m / 2;
}
Notice + instead of your -.
To find the sum just add in the function test_sum_from_to this line:
printf("The sum is %d", b-z);
Here's one solution :
#include<stdio.h>
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_x_to_y(int x, int y){
return sum_naturals(y) - sum_naturals(x);
}
main()
{
printf ("Sum: %d \n",sum_from_x_to_y(5, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 5));
return 0;
}
Note : sum from 0 to N is (n+1)*n/2 and not (n-1)*n/2

Resources