C++ Linear Equation

Talk about any languages right here. Share and discuss source, but don't expect your homework to be done for you.
Post Reply
User avatar
Thor
htd0rg lieutenant
Posts: 440
Joined: Tue Dec 18, 2007 9:39 am
Location: Location Location

C++ Linear Equation

Post by Thor » Fri Apr 04, 2014 12:32 pm

Alright, im trying to wrap my head around something here, perhaps I need more coffee. I've recently completed a simple programming challenge, but upon completion I see that some other entries are different from what I submitted.

The challenge description is here: http://www.talentbuddy.co/challenge/528 ... 0af383a409.

So, given the values for 'a' and 'b' , we need to complete a function that solves for x in this equation: a*x + b = 0. If any value of x satisfies the equation please print "INF" (infinium).

A lot of solutions are like so:

Code: Select all

#include <iostream>

using namespace std;

void solve_equation(double a, double b) {
    if(a==0)
        cout << "INF";
    else
        cout << -b/a;
}
My solution will print "INF" is either 'a' or 'b' input is 0. Which is more correct? What aren't some people checking for what 'b' is then making 'b' negative? My code covers more situations, but apparently just a few lines is all that is needed.
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.

Omnis Vestri Substructio Es Servus Ad Nobis.
- All Your Base Are Belong To Us

User avatar
Cool_Fire
Not a sandwich
Posts: 1912
Joined: Fri May 09, 2003 1:20 pm
Location: 41 6d 73 74 65 72 64 61 6d
Contact:

Re: C++ Linear Equation

Post by Cool_Fire » Sat Apr 05, 2014 6:57 am

If a = 0 then b must = 0 too for the whole thing to = 0 so you can skip the check since you're sure this is a condition that cannot happen. This is something you can do in math but I wouldn't recommend it in real world programming.
If we're breaking the rules, then how come you can't catch us? You can't find us? I know why. Cause, it's ... MAGIC!
Hackerthreads chat, where the party is going 24/7.

User avatar
Thor
htd0rg lieutenant
Posts: 440
Joined: Tue Dec 18, 2007 9:39 am
Location: Location Location

Re: C++ Linear Equation

Post by Thor » Sun Apr 06, 2014 9:24 pm

Cool_Fire wrote:If a = 0 then b must = 0 too for the whole thing to = 0 so you can skip the check since you're sure this is a condition that cannot happen. This is something you can do in math but I wouldn't recommend it in real world programming.
I see what you are saying. For whatever reason, this wasn't apparent to me at first. What the other coders were doing was coding for this specific situation where the equation equaled 0 every time, allowing for the 'shortcut'. I went ahead and coded to solve every possible situation I could think of whether the equation ended in zero or not. Perhaps too much coffee was what the real issue actually was.
Quidquid latine dictum sit, altum sonatur.
- Whatever is said in Latin sounds profound.

Omnis Vestri Substructio Es Servus Ad Nobis.
- All Your Base Are Belong To Us

Post Reply