- #1
flemmyd
- 144
- 1
Homework Statement
I'm just writing a simple program to try and take the reciprocal and keep the value logged.
Homework Equations
? not a math problem..
The Attempt at a Solution
My code:
int = numerator, denominator; // global variables
void swap(int numerator, int denominator)
{
int temp = numerator;
numerator = denominator;
denominator = temp;
}
int main (void)
scanf("%d", numerator);
scanf("%d", denominator);
while (input != quit) //i'm omitting some lines here...
printf("The current fraction is %d/%d", numerator, denominator);
case 1:
swap(numerator, denominator)
That should be everything that is relevant for this program right now. I didn't want to copy everything out (most of it is junk right now).
Anyway, so I input the values in for numerator and denominator (of the type int). it gives me the value fine. when i use the swap function, it should give me the reciprocal (aka swap them) and it does-- if i insert a printf line in the function, it does give the reciprocal. but when it comes back the the main function, right under the while loop, it gives me the original number.
So i know about passing by reference and passing by value. It seems like when i leave the scope of the swap function and the main values aren't changing, it's a pass by value and not reference. I don't know how to get this to pass by reference without using a unit 1 array or a pointer (of which I'm apparently not allowed to use for the assignment).
Because I'm trying to return two values, I can't use
right?int swap
{
//blah
return a
}
any one have any ideas?
EDIT: now that I think about it, is it a scoping issue? how can I define the equations such that the numbers are beyond the scope of the function?
Last edited: