Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A couple of Reference Function questions

A couple of Reference Function questions

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    j4express
    wrote on last edited by
    #1

    Hello I'm not sure if this is a beginners forum, but I will start here. I am a beginning C++ student and have a couple of mini-projects invloving fractions that I need some guidance on. First the assignments: 1. Write a Fraction-Reduction program that takes in any fraction (maybe with denominator not equal to 0 or 1), and reduces it lowest terms. The function prototype will take two integer variables passed by reference. 2. Write a function that takes in any number and returns its whole and fractional parts as outputs. Like 6.44 would return int 6 and float 0.44. My trouble: On #1: I understand how reference passing is working, that the variables values in main's variables can be changed by the function. What I cant figure out is how to write something that will find the greatest common factor of any two numbers entered. I understand the math behind it but I cant translate that math to the program, and get it the correct place in main or the function and have it work. On #2: This problem came from the chapter on reference parameters also. So is there a way to do this problem with the user entering one single float? Or is the trick to cin what the user enters in a format that allows to be updated invidually by reference from a function? If my code is needed I can post it. If there is another forum I need to be at, please let me know. Thank you sincerely for any help. JB

    P D 2 Replies Last reply
    0
    • J j4express

      Hello I'm not sure if this is a beginners forum, but I will start here. I am a beginning C++ student and have a couple of mini-projects invloving fractions that I need some guidance on. First the assignments: 1. Write a Fraction-Reduction program that takes in any fraction (maybe with denominator not equal to 0 or 1), and reduces it lowest terms. The function prototype will take two integer variables passed by reference. 2. Write a function that takes in any number and returns its whole and fractional parts as outputs. Like 6.44 would return int 6 and float 0.44. My trouble: On #1: I understand how reference passing is working, that the variables values in main's variables can be changed by the function. What I cant figure out is how to write something that will find the greatest common factor of any two numbers entered. I understand the math behind it but I cant translate that math to the program, and get it the correct place in main or the function and have it work. On #2: This problem came from the chapter on reference parameters also. So is there a way to do this problem with the user entering one single float? Or is the trick to cin what the user enters in a format that allows to be updated invidually by reference from a function? If my code is needed I can post it. If there is another forum I need to be at, please let me know. Thank you sincerely for any help. JB

      P Offline
      P Offline
      Prakash Nadar
      wrote on last edited by
      #2

      j4express wrote:

      2. Write a function that takes in any number and returns its whole and fractional parts as outputs. Like 6.44 would return int 6 and float 0.44.

      find the ceil or floor of the no depending on less or greater than zero val, Now you get the integer part, now subtract the original no with the integer part and you get the decimal part.


      -prakash Learning Symbian | Personal views

      1 Reply Last reply
      0
      • J j4express

        Hello I'm not sure if this is a beginners forum, but I will start here. I am a beginning C++ student and have a couple of mini-projects invloving fractions that I need some guidance on. First the assignments: 1. Write a Fraction-Reduction program that takes in any fraction (maybe with denominator not equal to 0 or 1), and reduces it lowest terms. The function prototype will take two integer variables passed by reference. 2. Write a function that takes in any number and returns its whole and fractional parts as outputs. Like 6.44 would return int 6 and float 0.44. My trouble: On #1: I understand how reference passing is working, that the variables values in main's variables can be changed by the function. What I cant figure out is how to write something that will find the greatest common factor of any two numbers entered. I understand the math behind it but I cant translate that math to the program, and get it the correct place in main or the function and have it work. On #2: This problem came from the chapter on reference parameters also. So is there a way to do this problem with the user entering one single float? Or is the trick to cin what the user enters in a format that allows to be updated invidually by reference from a function? If my code is needed I can post it. If there is another forum I need to be at, please let me know. Thank you sincerely for any help. JB

        D Offline
        D Offline
        ddupre
        wrote on last edited by
        #3

        #1> This is some really old code laying around in the depths of my harddrive, but it should be exactly what you are looking for. It's not really a programming problem as much as a math problem, luckily Euclidean did all the work for us many years ago. Euclidean algorithm for finding the Greatest Common Denominator: void GCD(int &Nume, int &Denom) { int a= Nume; int b= Denom int temp = a % b; while (temp > 0) //Keep searching till a % b evenly { a = b; //Cycle Values b = temp; temp = a % b; } Nume= Nume / b; Denom= Denom /b; } #2> Ok, this one is easier than it seems. So lets say we the float 6.44 from the user. Now the easy part, casting a floating point into an integer will automaticall truncate the number, therefore leaving us with only the whole number, simple subtraction does the rest.

        float f1 = 6.44f;
        int Int1= (int) f1; //Truncate 6.44 to int 6
        f1= f1 - Int1;      // 6.44 -6 = 0.44
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups