Doubt in a matter of C pointers

Asked

Viewed 238 times

1

That’s the thing about:

Build a function that takes two integer values a and b, returns (passing by reference) the quotient, div, and the rest division, mod, of a for b. The function must return -1 where operations cannot be carried out, and 0 if possible. An algorithm to use such a function must be created, treating the return of the function.

I made the code like this, but I have no idea what I’m doing:

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

int div (int *a, int *b)
{
    int resultado, resto, aux;
    if (*b==0)
    {
        return -1;
    }
    else
    {
        resultado = (*a / *b);
        resto = (*a % *b);
        return 0;
    }
}
int main (void)
}

int a, b, *resto, *resultado, *aux;
printf ("Digite o valor do dividendo: ");
scanf ("%i", &a);
printf ("Digite o valor do divisor: ");
scanf("%i", &b);
div (&a, &b);
if (div == 0)
{
    printf ("O resultado da divisao eh: %i ", resultado);
    printf ("O resto da divisao eh: %i", resto);
}
else
{
    printf ("Nao eh possivel realizar uma divisao por 0");
}
return 0;
}
  • Your code is a bit messy. Perhaps the IDE was putting up either tab or spacing. If you can fix the code it would be much easier to read

  • was bad, first time using this, I don’t even know how to edit the post, I’ll look here...

  • Do the [tour], there much of how you should use the site is explained

1 answer

2


build a function that returns 0 if it is possible to do the operation, -1 otherwise

Here you did well, only it is not possible to make the split when the denominator is 0.

To make this comparison, as the denominator is passed by reference, one must make *var for dereferencing. In this case, I am considering that b is always the denominator and a as numerator. Therefore, to know if it is possible to divide is to make the following comparison:

*b == 0

must return the quotient and the rest by reference

Here begins the mess ;-)

I, who like to work with memory and only optimize when meets a bottleneck, would pass two additional parameters. The signature of the function would look like this:

int div(int *a, int *b, int *quociente, int *mod);

Thinking about this scheme, the assignment of quociente and mod would be made like this:

int va = *a; // guarda o valor de a para ficar mais limpa a manipulação 
int vb = *b; // idem para b

*quociente = va/vb;
*mod = va%vb;

But as I believe the intention would be to return on a and b the result of this operation, then could not change the signature of the function:

int div(int *a, int *b);

To store the values of the division and module without destroying the values pointed by a and b, would be necessary to save a copy of these values. Would be so to receive the values via a and b and return also via a and b:

int va = *a; // guarda o valor de a para ficar mais limpa a manipulação 
int vb = *b; // idem para b

*a = va/vb;
*b = va%vb;

The rest of the code seems right to me at first glance.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.