Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

help with array plzzzz?

I have to do a program and start with the fraction class and write a main () program that obtains an arbitrary number of fractions from the user,stores them in an array of type fraction, averages them, and displays the result ..

and this is my code
# include <iostream>
using namespace std;

class fraction
{
private:
int a;
int b;
public:
void get_fraction()
{
char slash = '\\';
cout<<"Enter Fraction:";
cin>>a>>slash>>b;
}
void print_fraction()
{
int c = gcd(a, b);
char slash = '\\';
cout<<a/c<<slash<<b/c<<"\t";
}

fraction fadd (fraction g1,fraction g2)
{
fraction total;
total.a =(g1.a*g2.b+g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fsub (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.b-g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fmul (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fdiv (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;
}
void setA(int x)
{
a = x;
}
void setB(int x)
{
b = x;
}
int gcd(int x, int y)
{
int r;
while(y)
{
r = x % y;
x = y;
y = r;
}
return(x);
}
};

int main()
{
int count = 0;
char val;
fraction frac[100], sum, temp;;
sum.setA(0);
sum.setB(1);

do
{
frac[count].get_fraction();
cout<<"Would you like to continue(Enter'y' or 'n')? ";
cin>>val;
count++;

}while(val != 'n');

for(int i = 0; i<count; i++)
{
sum = sum.fadd(sum, frac[i]);
}
temp.setA(1);
temp.setB(count);

temp = temp.fmul(temp, sum);

cout<<"The average is : ";
temp.print_fraction();
cout<<endl;

return 0;
}

and it works fine but my teacher told me to change this code

void setA(int x)
{
a = x;
}
void setB(int x)
{
b = x;
}
int gcd(int x, int y)
{
int r;
while(y)
{
r = x % y;
x = y;
y = r;
}
return(x);
}

with is code
void lowterms()
{
long tnum,tden,temp,gcd;
tnum =labs(a);
tden =labs(b);
if(tden==0 )
{ cout<<"illegal fraction: division by 0";exit(1); }
else if (tnum==0)
{ a=0; b=1;return; }
while(tnum !=0)
{
if(tnum<tden)
{temp=tnum;tnum=tden; tden=temp;}
tnum=tnum-tden;
}
gcd=tden;
a=a/gcd;
b=b/gcd;
}

when I did that my program not working so what do I have to change in main () so it work please ??

thank youuuu :)
[3128 byte] By [malehda3y] at [2007-11-11 7:26:18]
# 1 Re: help with array plzzzz?
Can you elaborate on what isnt working?? ie errors etc..

Can you also show how you have implemented the new code into your program?
Code_Nerd at 2007-11-11 21:02:17 >
# 2 Re: help with array plzzzz?
this is my code after the new code

# include <iostream>
using namespace std;

class fraction
{
private:
int a;
int b;
public:
void get_fraction()
{
char slash = '\\';
cout<<"Enter Fraction:";
cin>>a>>slash>>b;
}
void print_fraction()
{
int c = gcd(a, b);
char slash = '\\';
cout<<a/c<<slash<<b/c<<"\t";
}

fraction fadd (fraction g1,fraction g2)
{
fraction total;
total.a =(g1.a*g2.b+g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fsub (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.b-g1.b*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fmul (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;
}
fraction fdiv (fraction g1,fraction g2)
{
fraction total;
total.a=(g1.a*g2.a);
total.b=(g1.b*g2.b);
return total;
}
void lowterms()
{
long tnum,tden,temp,gcd;
tnum =labs(a);
tden =labs(b);
if(tden==0 )
{ cout<<"illegal fraction: division by 0";exit(1); }
else if (tnum==0)
{ a=0; b=1;return; }
while(tnum !=0)
{
if(tnum<tden)
{temp=tnum;tnum=tden; tden=temp;}
tnum=tnum-tden;
}
gcd=tden;
a=a/gcd;
b=b/gcd;
}

};

int main()
{
int count = 0;
char val;
fraction frac[100], sum, temp;;
sum.setA(0);
sum.setB(1);

do
{
frac[count].get_fraction();
cout<<"Would you like to continue(Enter'y' or 'n')? ";
cin>>val;
count++;

}while(val != 'n');

for(int i = 0; i<count; i++)
{
sum = sum.fadd(sum, frac[i]);
}
temp.setA(1);
temp.setB(count);

temp = temp.fmul(temp, sum);

cout<<"The average is : ";
temp.print_fraction();
cout<<endl;

return 0;
}

so can you tell me what i have to change in main() ??please
thank you
malehda3y at 2007-11-11 21:03:17 >
# 3 Re: help with array plzzzz?
I cant see anywhere where you have a setA function??
Code_Nerd at 2007-11-11 21:04:27 >
# 4 Re: help with array plzzzz?
yeah it was in the old code but how can I change it please ?
thank you for reply :)
malehda3y at 2007-11-11 21:05:22 >
# 5 Re: help with array plzzzz?
Well for starters it wont work if you dont have setA in it..
Code_Nerd at 2007-11-11 21:06:26 >
# 6 Re: help with array plzzzz?
so what do I have to do please ??
thank you agin :)
malehda3y at 2007-11-11 21:07:25 >
# 7 Re: help with array plzzzz?
OK I see that you cannot use setA, setB from an early post..
I am finding it difficult with some of your variable names and not having any comments doesnt help either!

However it appears you are missing labs(a);.
What does this do?
Code_Nerd at 2007-11-11 21:08:29 >
# 8 Re: help with array plzzzz?
she did not told us what it dose she is only say that we have to use it !!
thank you
malehda3y at 2007-11-11 21:09:27 >
# 9 Re: help with array plzzzz?
I cannot work out what that function is doing really? It is like a getA() or something like that? However if you have to use this code and that function isnt given or you dont know what it is, I cant help much more mate..
Code_Nerd at 2007-11-11 21:10:25 >