Help With Functions In C++

free web hosting
Open Discussion > CONTRIBUTE > 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.

 

 

 


Reply

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

Reply

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.


Reply

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.

 

 

 


Reply

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.

Reply



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*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. how to get the gcf on c - 24.30 hr back. (1)
  2. euclidean algothrim in c - 38.97 hr back. (1)
  3. gcd programming codes for bloodshed - 41.20 hr back. (1)
  4. reduce fraction using gcd in c - 46.40 hr back. (1)
  5. fraction lowest terms in c - 50.50 hr back. (1)
  6. reduce a fraction by c - 52.93 hr back. (1)
  7. reducing fraction c - 53.81 hr back. (1)
  8. reducing fractions in c - 57.27 hr back. (1)
  9. write a program to find a highest common factor of two numbers in c language ? - 60.02 hr back. (1)
  10. help in c div function - 68.18 hr back. (1)
  11. reduce fractions c - 71.32 hr back. (1)
  12. how to reduce a fraction in a class c - 72.52 hr back. (1)
  13. c simplify fractions iostream - 73.72 hr back. (1)
  14. numerator in c - 76.72 hr back. (1)
Similar Topics

Keywords : functions

  1. Functions In Win32 - (2)
    Hey, all. Hope someone can make some useful suggestions, as i'm pretty much at a dead end!
    I'm writing some code to run in a win32 envrionment, using the API to interface with audio
    hardware. At the moment this is very simple, and I'm pretty much just regurgitating
    micro$oft tutorials. I'm using the Bloodshed Dev-C++ compiler. My problem is this --
    i'm trying to use the CWaveFile class to provide a simple interface to .wav files. This class is
    defined in DSUtil.cpp, and makes use of the 'standard' mmio functions. To use these, I ha...
  2. Various String Functions In C - (5)
  3. Functions Failing When Embedded In If() - Just a beserk problem that's plagued me on and off for years (3)
    I've had this problem on and off over the years (me just being silly, generally), so I thought I
    might share how to fix it. This has come up several time whilst using sockets, actually, so
    here's an example. For instance, CODE if(hd_ = bind(AF_INET, SOCK_STREAM,
    IPPROTO_TCP)  <= 0) {  perror("socket() failure");
     exit(1); } What's the problem? The problem is that, CODE hd_ =
    bind(AF_INET, SOCK_STREAM, IPPROTO_TCP) should be encapsulated within parentheses.
    Why? When the ...



Looking for functions, c

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for functions, c

*MORE FROM TRAP17.COM*
advertisement



Help With Functions In C++



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE