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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
has
  • 23
  • 3
  • I need calculate the function f (x, y) recursively., the codes are just examples but not working – has Apr 26 '20 at 20:09
  • What do you expect of this kind of test `x>0 && x<0` ? – Ôrel Apr 26 '20 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? – UnholySheep Apr 26 '20 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` ? – Ôrel Apr 26 '20 at 20:15
  • ohh sorry I'm fixing – has Apr 26 '20 at 20:15
  • @has What is your function f is supposed to do exactly are you trying to raise x to the power of y ? – nikoss Apr 26 '20 at 20:18
  • Your `if(x==0 && y==0)` has no `return` so it "falls through" to the function end where there is no `return`. Compile with `-Wall -O2` [which you should _always_ do] and this would be flagged. _Side note:_ Convention/style guides [almost universally] say do `if (...)` and _not_ `if(...)`. That is reserved [usually used] for function calls. – Craig Estey Apr 26 '20 at 20:18
  • (x) (y) is just `x*y`, in other words the answer should be `return x*y;`. You should force use of recursion where the problem doesn't call for it. – bobobobo Apr 26 '20 at 20:20
  • @bobobobo I think he is trying to raise x to y – nikoss Apr 26 '20 at 20:21
  • My English little, i can't understand some words sorry.. – has Apr 26 '20 at 20:21
  • Use `superscript` for exponents (xy if that's what you intend). The other way programmers sometimes show exponentiation is `x^y` – bobobobo Apr 26 '20 at 20:24
  • thank you for info – has Apr 26 '20 at 20:27
  • Sorry, [`` stopped working](https://meta.stackexchange.com/questions/296134/sup-superscript-html-tag-has-ceased-to-work-correctly) – bobobobo Apr 26 '20 at 20:28
  • @has what are you doing here `else if(x>0 && x<0 && y>0)` ? this is never going to be TRUE – Zain Arshad Apr 26 '20 at 20:29
  • I fixed it right now – has Apr 26 '20 at 20:29
  • xy doesn't seem to render properly! – bobobobo Apr 26 '20 at 20:31
  • Think about how you might write it as a loop. What would happen for each iteration of the loop? The loop iteration will be your recursive function call. The loop counter is going to be one of the arguments. The recursive function will need an exit condition. It should return the value that it calculates. Give it another try with these points in mind. – hookenz Apr 26 '20 at 20:39

4 Answers4

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;
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
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));
}
nikoss
  • 3,254
  • 2
  • 26
  • 40
  • The `l` in `%.2lf` is redundant and ignored. `ssize_t` is a non standard type. – chqrlie Apr 26 '20 at 20:53
  • isnt ssize_t posix standard ? – nikoss Apr 26 '20 at 21:00
  • `ssize_t` is defined in POSIX, but not in the C Standard. Why use `ssize_t` instead of `int` or `long`? – chqrlie Apr 26 '20 at 21:03
  • 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 – nikoss Apr 26 '20 at 21:05
  • I understand your concern, but it is largely irrelevant in this context (IMHO). – chqrlie Apr 26 '20 at 21:09
  • why is .2lf redundant ? – nikoss Apr 26 '20 at 21:10
  • 1
    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. – chqrlie Apr 26 '20 at 21:33
  • thanks for the info it is kinda hard to get a proper documentation about c features – nikoss Apr 26 '20 at 21:35
  • I suggest you download a copy of http://www2.open-std.org/JTC1/SC22/WG14/www/abq/c17_updated_proposed_fdis.pdf and monitor this page for up to date standards info: https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents – chqrlie Apr 26 '20 at 21:51
  • @chqrlieforyellowblockquotes that is awesome thanks. Can you also suggest anything on cross compilation, linking and stuff? I am having really hard time trying to compile openssl for musl to use in rust for a week already to no avail. – nikoss Apr 26 '20 at 21:58
  • If you run into issues with the musl libc, you can ask specific questions here, Rich Felker, the author, is quite active on SO as @R..GitHubSTOPHELPINGICE. – chqrlie May 01 '20 at 19:25
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;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
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
abelenky
  • 63,815
  • 23
  • 109
  • 159