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);
}
``````
5
Contributors
8
Replies
12
Views
6 Years
Discussion Span
Last Post by BobS0327

why are you using void qr() ??? instead of that use int qr() as it is nowhere mentioned to use void...

and you can use void main() there is no need of making it int.

edit your code as:

``````#include<conio.h>
#include<stdio.h>
int qr (int *first_ptr, int *sec_ptr);
void main()
{
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);
getch();
}
int qr (int *first_ptr, int *sec_ptr)
{
if((*sec_ptr)==0)
{
return 0;
}
else
{
int quo = (*first_ptr) / (*sec_ptr);
int rem = (*first_ptr) - (*sec_ptr) * quo;
printf ("quo = %d \n", quo);
printf ("rem = %d \n", rem);
return 1;
}
}
``````

and you can use void main() there is no need of making it int.

Absolutely incorrect. See void main()

well, i made it using void main() and it executes fine!!
then what is the problem???

and you can use void main() there is no need of making it int.

Disregard this advice, Hitman.

then what is the problem???

This should explain why `void main()` is not only wrong, but it can also be dangerous: http://users.aber.ac.uk/auj/voidmain.cgi

Otherwise, the above code is more or less correct (I would have organized this entire program a bit differently).

This should explain why void main() is not only wrong, but it can also be dangerous: http://users.aber.ac.uk/auj/voidmain.cgi

hmmmm... thanks for that!! :P Even i'm a newbie.. and the thing which is written in that link is correct!! we do most of our programs using void main()... thanks.. :)

well, i made it using void main() and it executes fine!!
then what is the problem???

So you'll read his link but not mine, eh? Fine... :-p

Just because you can drive without stopping at red lights, does that make it OK?

So you'll read his link but not mine, eh? Fine... :-p

the link you've given says that the page is unavailable.. :P sorry!!

the link you've given says that the page is unavailable.. :P sorry!!

Unfortunately, Waltp cut off the last character of his posted link. Below is the corrected link

void main

Also, here is another good link referencing the ISO standards.