Hey, this is one of the exam review questions, and I'm wondering how I would return 1 or 0 for bad input (i.e. cant divide by input 0) using return 0 and 1 if the function doesn't accept returns, i.e. void function (int variable) //cant have return statement. Here is the review question example, please provide me with the solution. I think you just need to tell me how to fix my qr function (it works but I need to add exceptions).
Question: (I made the part I need help with in bold
Question using pointers to simulate call-by-reference in functions:
You write functions main and qr
main reads 2 integers from stdin (call them x and y)
main calls function qr to obtain quotient and remainder for x and y.
quotient is: x/y
remainder is: x-y quotient
Function qr must:
calculate quotient variable first and then use quotient to calculate
-remainder (as in the formula above).
-must have 4 arguments corresponding to: x, y, quotient, and remainder
-must use pointers to insert the correct values into quotient and remainder
-return 1 if y was non-zero, and 0 if y was 0 (in the latter case, the
values put into quotient and remainder are unspecified).
-must not crash if y=0. (qr simply returns 0).
main prints quotient and remainder (as long as qr returned 1).
main prints message "BAD INPUT" on stderr otherwise.
EXAMPLE (> is my prompt):
./a.out
Enter x and y (ints): 20 3
quo: 6
rem: 2
./a.out
Enter x and y (ints): 20 0
BAD INPUT <--this went to stdout
My code:
void qr (int *first_ptr, int *sec_ptr);
int main(void)
{
int x, y;
int *ptr_x;
int *ptr_y;
printf("Enter x and y integers:");
scanf("%d", &x);
scanf("%d", &y);
ptr_x = &x;
ptr_y = &y;
qr (ptr_x, ptr_y);
system("PAUSE");
return 0;
}
void qr (int *first_ptr, int *sec_ptr)
{
//I believe I have to use if *sec_ptr != 0,
//but I dont understand what my prof means by returning 0 and 1 here??
int quo = (*first_ptr) / (*sec_ptr);
int rem = (*first_ptr) - (*sec_ptr) * quo;
printf ("quo = %d \n", quo);
printf ("rem = %d \n", rem);
}