Nov 22, 2009

Help With Functions In C++

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > C/C++ Programming

Help With Functions In C++

zach101
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.

 

 

 


Comment/Reply (w/o sign-up)

zach101
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);
}

Comment/Reply (w/o sign-up)

switch
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.


Comment/Reply (w/o sign-up)

methane
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.

 

 

 


Comment/Reply (w/o sign-up)

bidarshi

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.

Comment/Reply (w/o sign-up)

(G)Author Name - e.g. John, Mike

He's right. You need to use a loop to do it. (I myself used a while loop nested in a for loop).

I had to do a simple fraction addition assignment, but I figured I would make it reduce fractions. I'm beginning C++ but I actually figured it out!

You need to think: numerator and denominators must have no common factors besides 1 in order for them to be considred reduced. Set a variable called common factor or something and divide the numerator and denominator by it if mod = 0. 



Comment/Reply (w/o sign-up)

Forte
QUOTE (zach101 @ Dec 9 2005, 07:23 PM) *
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?


I'd do it a little more like this:

CODE
   #include <iostream>
using namespace std;

int gcd(int m, int n)
{
   if(n == 0)
     return m;
   else
     gcd(n, m % n);
}
  
   void reduce(int &num, int &denom)
{
   int divisor = gcd(num, denom);
   num /= divisor;
   denom /= divisor;
}
  

   int main()
   {
   int num = 0, denom = 0;

   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;
   }


A few things to note:
- you had a few lines without a semicolon that really needed one.
- C++ headers don't end in .h, and the line "using namespace std;" should be used afterwards.
- functions, if statements, and loops that contain more than one line of code need to have that code contained within braces. {}
- it is C++ convention to name your variables in lower case.
- the statement "Div % Div == 0" is unnecessary as a number will always evenly divide into itself.
- in your original posted code, you had an if statement using =. In C++, this is the assignment operator. The equal to operator is ==.


QUOTE (methane @ Feb 19 2006, 12:22 PM) *
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.


Generally the code can be written much cleaner, not to mention the function you wrote is not recursive. The following code should work just as nicely:

CODE
int gcd(int m, int n)
   {
     if(n == 0)
       return m;
     else
       gcd(n, m % n);
   }

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


Searching Video's for functions, c
See Also,
advertisement


Help With Functions In C++

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com