Is there any operator which I can use to attach 2 calculations?

43 Views Asked by At

I'm doing some homework where I use only elementary operations. I need to code a function which given a positive number, calculates the whole half of that number.

My problem is at:

int half(int x, int y)
{
    return x == 0 ? y : half(x-1-1, y+1) , x == 1 ? y : half(x-1-1, y+1);
}

I don't know what if exists any operator or something that connects these calculations. On that line of code i tried to use ( , ).

I tried to replace ( , ) by using ( | ) and ( & ). But i had many errors.


#include <stdio.h>

int sum(int x, int y)
{
    return y == 0 ? x : sum(x+1, y-1);
}

int half(int x, int y)
{
    return x == 0 ? y : half(x-1-1, y+1) , x == 1 ? y : half(x-1-1, y+1);
}

int main(void)
{
    int x;
    int y=0;
    scanf("%d", &x);
    int z = half(x, y);
    printf("%d\n", z);
    return 0;
} 

On this code i expect the output of 6/2 to be 3 and 5/2 to be 2.

Note: The function sum although is not doing nothing i cannot remove since the homework says not to remove from the code, perhaps i need to use it.

1

There are 1 best solutions below

0
On BEST ANSWER

You can just use || to put these together:

int half(int x, int y)
{
    return x == 0 || x == 1 ? y : half(x-1-1, y+1);
}