944,205 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 721
  • C RSS
Oct 27th, 2009
0

Basic arithemetic on rational numbers

Expand Post »
Hi
Modify this where possible
Attached Files
File Type: txt FRACTIONS.txt (3.6 KB, 56 views)
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
RIsagara is offline Offline
7 posts
since Apr 2009
Oct 27th, 2009
0
Re: Basic arithemetic on rational numbers
Click to Expand / Collapse  Quote originally posted by RIsagara ...
Hi
Modify this where possible
And what exactly do you want to modify? Ask a specific question.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Oct 27th, 2009
0
Re: Basic arithemetic on rational numbers
"interger" is spelled "integer"
"valve" is spelled "value"
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Beg.CProgrammer is offline Offline
12 posts
since Oct 2009
Oct 27th, 2009
0
Re: Basic arithemetic on rational numbers
Click to Expand / Collapse  Quote originally posted by yellowSnow ...
And what exactly do you want to modify? Ask a specific question.
you realize that it cannot give 0/4=0,
I also limited the user from entering 0,
modify the Gcd method to reduce the fraction recursively,
Reputation Points: 10
Solved Threads: 2
Newbie Poster
RIsagara is offline Offline
7 posts
since Apr 2009
Oct 27th, 2009
0
Re: Basic arithemetic on rational numbers
/*A PROGRAM PERFORMING BASIC ARITHMETIC ON RATIONAL NUMBERS*/

/*BY ISAGARA K ROBERT*/
/*26th october 2009*/
/*THIS WAS MY ASSIGNMENT(COMPUTER SCIENCE YEAR II) TO MR PAUL BAGYENDA ON BASIC ARITHMETIC OPERATION ON RATIONAL NUMBERS*/

#include <stdio.h>
#include <stdlib.h>



typedef struct rational_t {
int numer;
int denom;
} rational_t;
/*modify this for me*/
static int gcd(int a, int b)
{ int c;
while(a>0)
{
if(a<b)
{ c=a;
a=b;
b=c;
}
else
a=a%b;
}
return b;
}

/* r_init: creates rational number representing numer/denom and returns it*/
rational_t r_init(int numer, int denom)
{
rational_t fraction;
int GcD=gcd(numer,denom);
fraction.numer= numer/GcD;
fraction.denom= denom/GcD;
return fraction;
}

/* r_add: Returns a rational_t representing x+y */
rational_t r_add(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom+ x.denom* y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_sub: Returns a rational_t representing x-y */
rational_t r_sub(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom - x.denom* y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_mul: Returns a rational_t representing x*y */
rational_t r_mul(rational_t x, rational_t y)
{ rational_t result;
result.numer=(x.numer*y.numer);
result.denom=(x.denom*y.denom);
return result;
}

/* r_div: Returns a rational_t representing x/y */
rational_t r_div(rational_t x, rational_t y)
{
rational_t result;
result.numer=(x.numer*y.denom);
result.denom=(x.denom*y.numer);
return result;
}

/* r_toreal: Returns a double representing x */
double r_toreal(rational_t x)
{
}

/* r_print: prints rational as x/y (using printf */
void r_print(rational_t x)
{
int p=gcd(x.numer,x.denom);
printf("%d/%d",x.numer/p,x.denom/p);
}

int main(void)
{
printf("Rational arithmetic test program\n");
do {
int u,v, oper;
rational_t x, y, z;
here:
printf("x - numerator: ");
scanf("%d", &u);
if(u==0){
printf("Enter interger greater than Zero\n");
goto here;}
There1:
printf("x - denominator: ");
scanf("%d", &v);
if(v==0){
printf("Zero is undefined for a fraction ,Enter valve above Zero\n");
goto There1;
}
x = r_init(u,v);
here2:
printf("y - numerator: ");
scanf("%d", &u);
if(u==0){
printf("Enter interger greater than Zero\n");
goto here2;}
Thereback:
printf("y - denominator: ");
scanf("%d", &v);
if(v==0){
printf("Zero is undefined for a fraction ,Enter valve above Zero\n");
goto Thereback;
}
y = r_init(u,v);

printf("x is: ");
r_print(x);
printf("\n");

printf("y is: ");
r_print(y);
printf("\n");

printf("Operation (1=add,2=Sub,3=Mul,4=Div,0=End): ");
scanf("%d", &oper);

switch (oper) {
case 1:
z = r_add(x,y);
break;
case 2:
z = r_sub(x,y);
break;
case 3:
z = r_mul(x,y);
break;
case 4:
z = r_div(x,y);
break;
case 0:
return 0;
default:
printf("Unknown operation!\n");
break;
}

printf("z is: ");
r_print(z);
printf("\n");
} while(1);

return 0;
}
Reputation Points: 10
Solved Threads: 2
Newbie Poster
RIsagara is offline Offline
7 posts
since Apr 2009
May 26th, 2010
0
Re: Basic arithemetic on rational numbers
Gentle thanks for the modification but I did not really mean sp
Reputation Points: 10
Solved Threads: 2
Newbie Poster
RIsagara is offline Offline
7 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem reading file to string
Next Thread in C Forum Timeline: Factorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC