In: Computer Science
You will need to write a library of functions that uses pointers. Your functions must follow the headers below.
int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr); |
Create a function to do integer division that gives the division result and remainder via output parameters, quo_optr and rem_ptr.
Additionally, the function must return an int representing the success/failure of the function. The function should return 0 if it succeeds and -1 if there is a divide by zero error. (Note that you should not actually do the division if there will be a divide by zero error. Simply return -1.)
(I have written the total code to show how the code is working. If you need only the intDivide() function neglect the main function which I have given)
(Here the results of division will be in int only because we are performing integer division. Normally when we divide 5/2 the result will be 2.5. But here the result will be 2. Because the datatype int will neglect the values after decimal part.)
Code Explanation:
In the function first check if denominator is not equal to 0. If yes then calculate numerator / denominator and assign to *quo_pte
Then calculate numerator % denominator and assign to *rem_pte then return 0
If the If condition fails then return -1.
intDivide() function code:
int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr)
{
if(denominator!=0)
{
*quo_ptr = numerator / denominator;
*rem_ptr = numerator % denominator;
return(0);
}
else
{
return(-1);
}
}
code Implementation with main function:
#include <stdio.h>
int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr);
int main()
{
int numerator, denominator, quo_ptr, rem_ptr;
printf("Enter numerator: ");
scanf("%d",&numerator);
printf("Enter denominator: ");
scanf("%d",&denominator);
int res = intDivide(numerator, denominator, &quo_ptr, &rem_ptr);
if(res == 0)
{
printf("division result is: %d\n",quo_ptr);
printf("remainder result is: %d",rem_ptr);
}
else
{
printf("division by zero error");
}
return 0;
}
int intDivide(int numerator, int denominator, int *quo_ptr, int *rem_ptr)
{
if(denominator!=0)
{
*quo_ptr = numerator / denominator;
*rem_ptr = numerator % denominator;
return(0);
}
else
{
return(-1);
}
}
Sample O/P1:
Enter numerator: 23
Enter denominator: 5
division result is: 4
remainder result is: 3
Sample O/P2:
Enter numerator: 8
Enter denominator: 0
division by zero error
Code Screenshot:
Sample O/P1 screenshot:
Sample O/P2 screenshot:
(If you still have any doubts regarding this answer please comment I will definitely help)