Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Help With Functions In C++
zach101
post Dec 9 2005, 10:59 PM
Post #1


Premium Member
********

Group: Members
Posts: 178
Joined: 26-June 05
Member No.: 8,699



Hey guys I could really use some help wiht C++ because My C++ class consists of me three others, and a ton of people learning HTML. So in other words its basically independent study. He dosnt really care how you get the assignments done or when you do... just get them done. Well to say the least I am STRUGLING in this class do to the lack of instruction. Now the latest assignment im sure seems eazy for many of you but for me... well considering this is my first year of programming im still quitea a newbie. So the assignment says right a function named Reduce() that takes the integer numerator and denominator of a fraction and reduces it to lowest terms. and then it says test it with this code:
CODE

{
int Num, Denom;
cout << "Enter the numerator: ";
cin Num;
cout << "Enter Denominator: ";
cin Denom;
Reduce(Num, Denom);
cout << "The reduced fraction is " << Num << "/" << Denom << endl;
}


Now ive been working with this problem for a little while now and once i get on the right track i can usually get these but this one has me stumped.
Any help would be awesome.
Go to the top of the page
 
+Quote Post
zach101
post Dec 9 2005, 11:23 PM
Post #2


Premium Member
********

Group: Members
Posts: 178
Joined: 26-June 05
Member No.: 8,699



Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there?
CODE



#include<iostream.h>

//------------------------------------------------------------------------------
void Reduce(int Num, int Denom)
int Div = Num;

if (Num % Div == 0 && Div % Div == 0)
GFC = int Div
else
Div--;






//------------------------------------------------------------------------------

int main()

{
int Num, Denom;
cout << "Enter the numerator: ";
cin >> Num;
cout << "Enter the denominator: ";
cin >> Denom;
Reduce(Num, Denom);
cout << "The reduced fraction is " << Num << "/" << Denom << endl;
}
return(0);
}
Go to the top of the page
 
+Quote Post
switch
post Dec 11 2005, 11:43 PM
Post #3


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



firstly, you need curly braces { } around your reduce() function.

secondly you are effectively getting the remainder of the numerator divided by itself in both parts of your statement (as Num == Div):
CODE
if (Num % Div == 0 && Div % Div == 0)

each of these modulus statements will return a remainder of 0.

you need to find the highest common factor of the numerator and the denominator. you could do this by using a while loop and looping through every positive integer. Then stop when you get to half of the smallest number (as you won't get a highest common factor bigger than that).

i'm not giving you the code because there's every chance that you could be cheating on an assignment... seriously, your situation does sound a little dodgy. anyway, as for learning C++ by yourself, buy a book. You could probably pick one up for less than $30 if you know where to look.

Go to the top of the page
 
+Quote Post
methane
post Feb 19 2006, 04:22 PM
Post #4


Newbie
*

Group: Members
Posts: 5
Joined: 18-February 06
Member No.: 18,832



QUOTE(zach101 @ Dec 10 2005, 07:23 AM) *

Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there?
CODE

#include<iostream.h>

//------------------------------------------------------------------------------
void Reduce(int Num, int Denom)
int Div = Num;

if (Num % Div == 0 && Div % Div == 0)
GFC = int Div
else
Div--;
//------------------------------------------------------------------------------

int main()

{
int Num, Denom;
cout << "Enter the numerator: ";
cin >> Num;
cout << "Enter the denominator: ";
cin >> Denom;
Reduce(Num, Denom);
cout << "The reduced fraction is " << Num << "/" << Denom << endl;
}
return(0);
}




To find out the GCD/GCF of two integers, you can make use of euclidean algorithm. That is, GCD(a,cool.gif = GCD(a%b,cool.gif if a>b. You can use a recursive function to find out the answer in fewer steps. The function should look like this

int GCD(int a , int cool.gif
{
if (a == cool.gif
{
return a;
}
if (a * b == 0)
{
return a + b;
}
return ( a%b , b%a);
}

Hope it can help.

QUOTE(zach101 @ Dec 10 2005, 07:23 AM) *

Okay i got a little further take a llook at this code and tell me what you think do i need a while loop in there?
CODE

#include<iostream.h>

//------------------------------------------------------------------------------
void Reduce(int Num, int Denom)
int Div = Num;

if (Num % Div == 0 && Div % Div == 0)
GFC = int Div
else
Div--;
//------------------------------------------------------------------------------

int main()

{
int Num, Denom;
cout << "Enter the numerator: ";
cin >> Num;
cout << "Enter the denominator: ";
cin >> Denom;
Reduce(Num, Denom);
cout << "The reduced fraction is " << Num << "/" << Denom << endl;
}
return(0);
}



To find out the GCD/GCF of two integers, you can make use of euclidean algorithm. That is, GCD(a,b ) = GCD(a%b,b ) if a>b. You can use a recursive function to find out the answer in fewer steps. The function should look like this

CODE
int GCD(int a , int  b)
{
   if (a == b)
  {
     return a;
  }
  if (a * b == 0)
  {
     return a + b;
  }
   return ( a%b , b%a);
}


Hope it can help.
Go to the top of the page
 
+Quote Post
bidarshi
post Mar 23 2006, 02:22 PM
Post #5


Newbie
*

Group: Members
Posts: 8
Joined: 23-March 06
Member No.: 20,552




Well I am not an expert in C programming and I think I can address your problem with a logical approach. For reducing a fraction you first check whether the numerator and the denominator are relatively prime to each other. If yes it means the fractions are already reduced. If they are not relatively prime further reduction is possible. Now you find the greatest common factor between the two that is numerator and denominator.Divide both the numerator and denominator with the greatest common factor.The fraction is thus reduced. You check it and meanwhile I will also check whether it is working. But I think there is no bug in this approach.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Functions In Win32(2)


 



- Lo-Fi Version Time is now: 6th September 2008 - 06:54 PM