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));
}
}
Related
I have a problem. My code:
#include <stdio.h>
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
int compare (int, int);
int main() {
int x = readInteger(x);
int y = readInteger(y);
printf("%d is greater", compare(x,y));
return 0;
}
int compare(int x , int y) {
if(x > y) return x;
else return y;
}
only outputs
variable 1: ...
variable 1: ...
instead of
variable 1: ...
variable 2: ...
The issue is because return means "end function execution here". So, you have
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
printf("variable 2 :");
scanf("%d", &y);
return y;
}
which, because of the return x line, is equivalent to
int readInteger() {
int x,y;
printf("variable 1 :");
scanf("%d", &x);
return x;
}
One quick fix is to remove return x; line. Though then it won't do exactly what you want the function to do, since you want to read 2 integers and return them.
A better fix is to pass the string to the function as argument and then call it twice:
int readInteger(const char* prompt) {
int x;
printf(prompt);
scanf("%d", &x);
return x;
}
int main() {
int x = readInteger("variable 1 :");
int y = readInteger("variable 2 :");
printf("%d is greater", compare(x,y));
return 0;
}
PS. If a function has no arguments (readInteger in your original code, you should not call it with arguments (int x = readInteger(x) in main).
I just implemented my swap function but it does not print anything. Do you know why does the line printf does not execute?
#include <stdio.h>
int swap(int x, int y) {
scanf("%d", &x);
printf("%d, x is",x);
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
int main() {
swap(6,5);
}
You should not take user input inside the swap function. Its purpose should be to swap two integers only. You can move the scanf statements to main function.
#include <stdio.h>
int swap(int x, int y){
int temp = x;
x = y;
y = temp;
printf("After Swapping in swap function: x = %d, y = %d", x, y);
return 0;
}
int main(void){
int x, y;
scanf("%d", &x);
printf("%d, x is", x);
scanf("%d", &y);
printf("%d, y is", y);
swap(x, y);
printf("After Swapping in main function: x = %d, y = %d", x, y);
}
But the above code has a major issue. Though the swap function prints the integers passed as they are swapped but the fact is x and y in the main remains unaffected.
In this case to make it work, using pointers would be helpful
void swap(int *ptrx, int *ptry){
int temp = *ptrx;
*ptrx = *ptry;
*ptry = temp;
}
In the main function call the swap as swap(&x, &y);
Use this code for swapping.
#include <stdio.h>
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("After Swapping: x = %d, y = %d", x, y);
}
int main()
{
swap(6,5);
return 0;
}
And I don't understand why you need to scan x & y
Given n, the program should calculate 1^1 + 2^2 + 3^3 + ... till n-1^n-1. Below is my code, in which there is one function inside while loop which and the passed value is from n-1 in the function. The function definition has two variables which return the ans. Output is wrong always 1.
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
la= (x*power(x, y-1));
ans+=la;
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, a, b, res, res1;
scanf("%d%d", &n, &m);
while(n-- && n>0)
{
a = power(n-1, n-1);
}
printf("%d", a);
}
return 0;
}
Some problems in your code.
As pointed in another answer, your power function was broken:
ans was not initialized
{ } were missing after the else
in the while, you compute x^x, but you forget the result, whearas you
should sum it.
first thing you do in while loop is to decrease n and to compute power(n-1, n-1)
that sound not logical.
Hence, your corrected code could be:
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
if(y==0)
return 1;
else
return x*power(x, y-1);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, b, a = 0;
scanf("%d%d", &n, &m);
while(n>1)
{
--n;
b = power(n, n);
a += b;
printf("%d^%d -> %3d\n",n, n, b);
}
printf("sum= %d", a);
}
return 0;
}
Gives for n = 6:
5^5 -> 3125
4^4 -> 256
3^3 -> 27
2^2 -> 4
1^1 -> 1
sum=3413
C uses braces to form blocks, your power() function looks like it's wanting to use indentation like in Python.
It should probably be:
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
{
la= (x*power(x, y-1));
ans+=la;
return ans;
}
}
Of course since the first if has a return, the else is pointless, and you can simplify the code:
int power(int x, int y)
{
if (y==0)
return 1;
return x * power(x, y-1);
}
The variable ans was never assigned to, that looked broken so I simplified it out.
Of course this is susceptible to integer overflow.
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
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