2

x and y are integers, the function f(x, y) = xy needs to be calculated. Calculate the function f(x, y) recursively.

#include <stdio.h>
#include <stdlib.h>

int f(int x, int y) {
    if (x == 0 && y != 0) {
        printf("answer: 0\n");
        return 0;
    } else if (x != 0 && y == 0) {
        printf("result: 1\n");
        return 1;
    } else if (x > 0 && y == 1) {
        f(x, 1) == x;
        return x;
    } else if (x > 0 && y > 0) {
        printf("result: %d\n", x * f(x, y - 1));
        return x * f(x, y - 1);
    } else {
        y = -y;
        printf("result: %d\n", 1 / f(x, y));
        return  1 / f(x, y);
    }
}
int main() {
    int k, l;
    float result;

    printf("*****************ust alma*********************\n\n");
    printf("enter two number: ");
    scanf("%d\n%d", &k, &l);
    result = f(k, l);
    printf("girilen result: %d", result);

    return 0;
}

I am waiting for your help I can not do this lesson. Really hard for me.

16
  • I need calculate the function f (x, y) recursively., the codes are just examples but not working Commented Apr 26, 2020 at 20:09
  • What do you expect of this kind of test x>0 && x<0 ? Commented Apr 26, 2020 at 20:12
  • 1
    f(x,1)==x; just discards the result of the function call and comparison - what were you intending to do here? Commented Apr 26, 2020 at 20:14
  • I have tried to reindent your code, return is missing for some else, some tests are always false. %d and result are not consistent. What is the definition on f ? Commented Apr 26, 2020 at 20:15
  • ohh sorry I'm fixing Commented Apr 26, 2020 at 20:15

4 Answers 4

2

You have some issue on your type, with negative y your need to use double or float. You will get 0 with int I have simplified your function, you were closed

Not the . for 1 to use a double and not an int

#include <stdio.h>
#include <stdlib.h>


double f(int x, int y){
    if(y == 0) {
        return 1;
    } else if(y > 0) {
        return x * f(x, y - 1);
    } else {
        y = -y;
        return  1. / f(x, y);
    }
}
int main() {
    int k,l;
    float result;

    printf("*****************ust alma*********************\n\n");
    printf("enter two number: ");
    scanf("%d\n%d", &k, &l);
    result = f(k,l);
    printf("girilen result: %f\n", result);

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect! Thank u very much!
0,0 should return 1 actually
@nikoss: Thank you for this gem. Learn something new everyday :)
0
#include <stdio.h>
#include<stdint.h>

double my_pow(ssize_t x,ssize_t y)
{
    if (y==0)return 1.0;
    else if (y==1)return x;
    else if (y<0) return 1.0/my_pow(x,-y);
    else return x*my_pow(x,--y);
}

int main(void) {
    printf("%.2lf\n",my_pow(2,-1));
}

11 Comments

The l in %.2lf is redundant and ignored. ssize_t is a non standard type.
isnt ssize_t posix standard ?
ssize_t is defined in POSIX, but not in the C Standard. Why use ssize_t instead of int or long?
because ssize_t is exactly a word size of the target machine but int or long might have alignment issues. I am writing rust normally and in rust we throw isize and usize everywhere possible so this is somewhat c equivalent of it @chqrlieforyellowblockquotes
Because, unlike scanf's, the printf conversion specifier for a double argument is %f, not %lf. float arguments are automatically converted to double when passed as extra arguments to vararg functions, so there is no need to distinguish float and double arguments.
|
0

Here is a compact solution:

#include <stdio.h>

double f(int x, int y) {
    return y ? y < 0 ? f(x, y + 1) / x : f(x, y - 1) * x : 1;
}

int main() {
    int x, y;
    printf("*****************ust alma*********************\n\n"
           "enter two numbers: ");
    if (scanf("%d%d", &x, &y) == 2)
        printf("girilen result: %f\n", f(x, y));
    return 0;
}

Comments

0
#include <stdio.h>

double f(int x, int y)
{
    return y<0 ? 1 / f(x, -y)  :
           y   ? x * f(x, y-1) : 1;
}

int main(void)
{
    printf("%.3f\n", f(4,0));  // To the 0-power: 1
    printf("%.3f\n", f(4,1));  // To the 1-power: X
    printf("%.3f\n", f(4,-2)); // To the negative power: 1/(X^Y)
    return 0;
}

Output

Success #stdin #stdout 0s 4472KB
1.0000
4.0000
0.0625

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.