Home > Net >  Values inserted to my parameter suddenly vanished after the first expression
Values inserted to my parameter suddenly vanished after the first expression

Time:11-20

I have problems for my function, since after the first expression, the values inserted to my parameters (float x, float y) suddenly becomes 0.

I called this function with main (), the output for c = x/y works as expected. But for d = x - (y * c), it gives me an output of 0, I check where the problem is and it appears to be because of x and y since they both have 0 values for some reason

I have not finished my function for greatest common divisor, since I'm still stuck at this problem

'''

int gcd (float x, float y)
{

int c = x / y;                                  
printf ("This's your quotient: %d\n", c);
int d = x - ( y * c );                         // d = 0 since y, x = 0
printf ("This's your remainder: %d\n", d);     

printf ("c %d\n",c);                           // works normally
printf ("y %f\n",y);                           // y = 0
printf ("x: %f\n",x);                          // x = 0
}

CodePudding user response:

Here is example code that fixes the issues I noticed and changing the signature to separate calculations from output:

#include <stdio.h>

void quotient_reminder(unsigned x, unsigned y, unsigned *q, unsigned *r) {
    if(y == 0) return;
    *q = x / y;
    *r = x - (y * *q); // d = 0 since y, x = 0
}

int main() {
    unsigned q, r;
    quotient_reminder(25.0, 7.1, &q, &r);
    printf ("This's your quotient: %u\n", q);
    printf ("This's your remainder: %u\n", r);
}

and it will return:

This's your quotient: 3
This's your remainder: 4
  •  Tags:  
  • c
  • Related