d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > C Programming > Need Assistance
Add Reply New Topic New Poll
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Nov 30 2013 10:18am
I am writing a program to modify fractions: reducing, adding, converting to/from strings. I am not looking for the assignment to be done for me, I am looking for help with it.

If you can help, please PM me. I will donate FG to those that help. Thanks
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 30 2013 10:54am
i recommend you post what your problem is.
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Nov 30 2013 01:45pm
Quote (carteblanche @ Nov 30 2013 12:54pm)
i recommend you post what your problem is.


We are using multi-dimensional arrays to house the fractions..so something like: complete the following code (that will reduce a fraction)

int gcd(int a, int b )
{
if (a == 0 && b == 0) {
printf("Illegal args to gcd: %d, %d\n",a,b);
exit(1);
}
int aa = abs(a);
int bb = abs(b);
if (aa == 0)
return bb;
if (bb == 0)
return aa;
return gcd(bb,aa%bb);
}

void reduce_fraction(Fraction *R)
{

}

This post was edited by SleepyUnit on Nov 30 2013 01:46pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 30 2013 04:07pm
i dont see a single multidimensional array...whats your strategy? what are you going to do with the arrays?
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Dec 1 2013 10:42am
The pointer will be pointing to the array. It will be something like

int A[ ] [ 1 ] = { {3,6}, {4,5}};

So the fractions would be 3/6 and 4/5.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 1 2013 11:12am
what exactly are you having problems with? converting the array to a Fraction struct or reducing a Fraction struct?
Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Dec 2 2013 03:03pm
I suppose that the arrays had nothing to do with this, he just teaches very confusing.

So I should have all of my reducing + addition + comparing fractions functions working.

I just need to figure out how to finish the two following functions (and I believe I use sscanf - but i've never used it before):

Fraction string_to_fraction(const char *S)
{
Fraction result = {0,1};
return result;
}

void fraction_to_string(Fraction R,char repr[])
{
repr[0] = 0;
return answer;
}

Member
Posts: 4,625
Joined: Jul 3 2009
Gold: 6,520.00
Dec 2 2013 07:36pm
I have everything running, but I get the wrong result when I try to test Function string_to_fraction. Any ideas on why? Thanks.

typedef struct fraction {
int numer;
int denom;
} Fraction;

Fraction string_to_fraction(const char *S)
{
int n, d;
sscanf(S, "%d/%d", &n, &d);

Fraction result = {n,d};
return result;
}


String to Fraction Errors:
expected: 5/1
actual: 5/-4
expected: 10/1
actual: 10/-4

This post was edited by SleepyUnit on Dec 2 2013 07:37pm
Go Back To Homework Help Topic List
Add Reply New Topic New Poll