-1

so I'm currently writing an assignment for my data structures class that's giving me a headache in the most basic place possible. We're working on the Hungtington-Hill method in conjunction with priority queues, and when I try and do my simple division to get the "priority" which is equal to the population of a state divided by the geometric mean, I'm getting really large numbers. The vars and method inside my state class look like this:

class State{
 private:
    string state;
    int pop;
    int reps;
    double priority;
};
void State::calculatePriority(){
    double temp = (double)reps * ((double)reps + 1.0);
    priority = (double)pop/sqrt(temp);
}

The method calculatePriority() gives me crazy output, where it'll read in like this:

State: California

2010 Census Population: 37253956

Representatives: 1

Priority: 0

And post calculatePriority() I get this:

State: California

2010 Census Population: 37253956

Representatives: 1

Priority: 2.63425e+07

Is there a logical mistake I'm making here? I'm not sure what the issue is. It may be with the sqrt() method I am not sure. I at first thought it was because of the (double) cast, so I changed all the vars to doubles, but I encountered the same issue.

  • If you do the math in a calculator that's the correct answer by your formula: `(37253956 / sqrt(1 * (1 + 1))` – tadman Oct 22 '17 at 19:08
  • @tambre [Square root.](http://en.cppreference.com/w/cpp/numeric/math/sqrt) – tadman Oct 22 '17 at 19:08
  • @tambre": Square Root. `sqrt(temp)` is approximately reps. – MSalters Oct 22 '17 at 19:09
  • @tadman the constructor being used initializes all the variables used to default values. I'll try finessing that a little more. – Dan Mallory Oct 22 '17 at 19:09
  • @tadman Probably yes, but what's his implementation? If he was using the one from STL, he'd do `std::sqrt`. – tambre Oct 22 '17 at 19:09
  • @tambre We have a lot of people here that insist on `using namespace std;` because they don't know better. – tadman Oct 22 '17 at 19:09
  • 1
    @DanMallory Just so you know, [`using namespace std;` is a bad practice](https://stackoverflow.com/q/1452721/2176813), never use it. – tambre Oct 22 '17 at 19:10
  • @tambre yes, using the one from STL with standard namespace. – Dan Mallory Oct 22 '17 at 19:10
  • @DanMallory At first I thought this was a classic uninitialized variable problem where you get wacky, unpredictable numbers, but these numbers look absolutely correct based on your formula. If anything your formula is wrong. – tadman Oct 22 '17 at 19:10
  • You get exactly 37 million divided by the square root of 2. And what were you hoping to get? – bipll Oct 22 '17 at 19:11
  • 1
    @tadman You're right - I hadn't accounted for the fact that the initial priority would be ridiculously high of a number – Dan Mallory Oct 22 '17 at 19:13
  • @tambre If we're going to be picky about things like `using namespace std;`, which is important to call out, it's called the ["Standard Library"](https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library) now, where the Standard Library is based on but *not identical to* the [historical STL](https://en.wikipedia.org/wiki/Standard_Template_Library) unless you consider "STL" to stand for "STandard Library". – tadman Oct 22 '17 at 19:13
  • There's nothing wrong with "using namespace std;". All the carrying-on about it is very silly; there's no reason to avoid using such a labour-saving feature. – Hammerite Oct 22 '17 at 19:15
  • I'm not really here to learn about "proper design practice". I'm aware of the faults of the std namespace, but it's required for this assignment, so regardless of whether or not its implementation is optimal I must use it. – Dan Mallory Oct 22 '17 at 19:15
  • FWIW, the code doesn't need any of those casts. In `(reps + 1.0)`, the `1.0` is a double, so `reps is promoted to double. And since that part of the expression is a double, the left-hand `reps` in `reps * (reps + 1.0)` is also promoted to double. And, finally, the function `sqrt` returns double, so `pop` in `pop/sqrt(temp)` gets promoted to double. – Pete Becker Oct 22 '17 at 19:26

1 Answers1

2

The result is not that large, it's actually 2.63425e+07 ≈ 26342524, which is the exact result of your calculation:

double temp = (double)reps * ((double)reps + 1.0); // = 2
priority = (double)pop/sqrt(temp); // = (37253956 / 1.41421) = 26342524

There is nothing wrong with your program :)

Daniel Trugman
  • 8,186
  • 20
  • 41