Thread: help to write program

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    help to write program

    I'm new to c programing and I want to write following program
    Teacher ask student to solve 26/65 he simply cancelled 6 from numerator and denominator and answer found correct.
    Write program to determine all fractions with 2 digit numerators and denominators for which this cancellation technique works correctly
    Thank you

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Teacher wants students to do the work on their own without asking someone on the Net to do this work for them.

    What do you mean by "cancelling" 6 from the numerator and denominator in the example given?

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    Cancelling 6 means canceling same digit from numerator n denominator
    I'm new I don't know to write code for this program

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Do you mean subtracting 6 from each, "20/59" or removing "6" from each, "2/5"? If the latter, it does not make sense to me mathematically!

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    Removing 6 from right digit of numerator n left digit of denominator

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by san12345 View Post
    I'm new I don't know to write code for this program
    Writing code is only one part of programming. Another is figuring out how to break down a problem and solve it. This is a skill you must develop through practice.

    Also, as rstanley mentions, make sure you completely understand the problem before trying to devise a solution.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Please explain the logic of this math!

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic:

    26/65 = 0.4
    2/5 = 0.4

    @ OP: You need to sit down with a pencil and paper, and start trying to figure out this algorithm by hand. If you cannot solve a problem yourself step by step, you will not be able to program a computer to solve it for you.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    @matticus, I agree with you. IMHO, yet another example of a bad instructor! ;^)

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,863
    Quote Originally Posted by Matticus
    It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic:
    Quote Originally Posted by rstanley
    @matticus, I agree with you. IMHO, yet another example of a bad instructor! ;^)
    Err... not necessarily. The scenario is that a hypothetical teacher gives a student a maths problem, but the student incorrectly solved it by applying a nonsensical rule ("cancelling" digits that happen to be the same in the numerator and denominator), yet the answer turned out to be correct by coincidence. The required program is supposed to find all such coincidences for 2 digit numerators and denominators. The instructor himself/herself is not saying that these coincidences form an "obscure mathematical rule", but rather the scenario is merely how the programming problem is presented for context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by laserlight View Post
    The required program is supposed to find all such coincidences for 2 digit numerators and denominators.
    Yes, clearly. That is why I suggested they start developing an algorithm to write this code, rather than try to "correct" their requirements.

    I said the problem "seems to rely more on coincidence", which is also what you are saying.

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by san12345 View Post
    Teacher ask student to solve 26/65 he simply cancelled 6 from numerator and denominator and answer found correct.
    That's just because of a chance occurrence. It was not a 6 that was cancelled; a 13 was cancelled from both numerator and denominator:
    Code:
    26   2⋅13   2
    ── = ──── = ─ 
    65   5⋅13   5
    A computer program typically finds the greatest common divisor of both numerator and denominator -- here, gcd(26,65)=13 -- and divides both by it, to find the reduced form.

    The computer program should obviously look at all pairs of two-digit numerators and denominators that have at least one common digit, and check if removing one of those common digits from both numerator and denominator actually yields the same fraction as the original number.

    So, no need to implement gcd() at all. Just use a single loop for each digit (so four nested loops in total), and construct the numbers using A = N1 + 10⋅N2 and B = N3 + 10⋅N4. It is then very easy to check if the two have digits in common.

    I'm pretty sure the teacher thought that floating-point results suffice for the comparison, but I'm not convinced. Instead, you should check the two ratios using integer variables and cross multiplication,
    Code:
    A   C
    ─ = ─  if and only if A⋅D = B⋅C and B≠0, D≠0.  
    B   D
    where C/D is the original ratio, and A/B is the ratio with one common digit removed from both A and B.

    As long as your (unsigned) integer type is large enough to hold the products A⋅D and B⋅C, and int and unsigned int certainly are for two-digit numbers, the above check is exact and robust.

    (Lots of edits during first 20 minutes this post was up.)
    Last edited by Nominal Animal; 08-22-2015 at 11:55 AM.

  13. #13
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Matticus View Post
    It seems to rely more on coincidence (or some obscure mathematical rule that I am unaware of) than mathematical logic
    Well, the rule is pretty obscure for sure. If we define

    Let
    N = sum(Ni ⋅ 10i, i = 0..LN-1)
    where
    1 ≤ Ni ≤ 9, i = LN-1
    0 ≤ Ni ≤ 9, 0 ≤ i < LN-1
    and
    D = sum(Di ⋅ 10i, i = 0..LD-1)
    where
    1 ≤ Di ≤ 9, i = LD-1
    0 ≤ Di ≤ 9, 0 ≤ i < LD-1
    so that N is an LN-digit positive decimal number,
    10LN-1N < 10LN
    and D is an LD-digit positive decimal number,
    10LD-1D < 10LD

    the rule is

    There is a pair (n, d),
    0 ≤ n < LN
    0 ≤ d < LD
    Nn = Dd
    that fulfills the following rule:
    U = sum(Ni ⋅ 10i, i = 0..n-1) + sum(Ni ⋅ 10i-1, i = n+1..LN-1)
    V = sum(Di ⋅ 10i, i = 0..d-1) + sum(Di ⋅ 10i-1, i = d+1..LD-1)
    N V = D U ≠ 0

    I don't think there is a simpler way to state the rule (anything like summing the digits, or such tricks); I think this was what Matticus was saying, too.

    The OP's problem to be solved is to find all that fulfill the above, when LN = LD = 2. My awk scriptlet says there are 242 of those if you count 89/89 = 8/8 and 89/89 = 9/9 as two separate cases, or 170 if you count 89/89 = 8/8 = 9/9 only once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-09-2014, 08:23 AM
  2. Need help to write a program in c
    By cooljack08 in forum C Programming
    Replies: 15
    Last Post: 11-17-2011, 08:13 PM
  3. How do i write a program that does this?
    By rondel in forum C Programming
    Replies: 1
    Last Post: 10-24-2009, 09:56 AM
  4. Is there another way to write this program?
    By agentxx04 in forum C Programming
    Replies: 1
    Last Post: 11-23-2004, 09:28 PM
  5. Help Write This Program Plz
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 01:08 AM