# Tag Info

42

Let me quote the wonderful book Numerical Recipes in C++ (but also applicable): We assume that you know enough never to evaluate a polynomial this way: p=c[0]+c[1]*x+c[2]*x*x+c[3]*x*x*x+c[4]*x*x*x*x; or (even worse!), p=c[0]+c[1]*x+c[2]*pow(x,2.0)+c[3]*pow(x,3.0)+c[4]*pow(x,4.0); Come the (computer) revolution, all persons found guilty of such criminal ...

41

Small issues before I get into the big one: Please make those setters private. The caller of this code has no business changing any of those values. Don't use ulong unless you are interoperating with unmanaged code. long has plenty of range. .NET uses signed quantities even for quantities that are logically always positive. It makes it easier to do things ...

38

The problem with an approach like that is that you you don't take into account that switching processors/cores is taking some time which will fake the results. This can be avoided by setting the ProcessorAffinity to use only a specific processor/core like so Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2); // Use only the second core To ...

31

Learn to love rustfmt. For example, the Rust standard is 4-space indents. Learn to love Clippy, which can show you the more idiomatic way to iterate over a collection: warning: it is more idiomatic to loop over references to containers instead of using explicit iteration methods --> src/main.rs:43:18 | 43 | for x in times.iter() { | ...

28

There's a lot that can be improved here, so I hope that these suggestions are useful to you. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Make sure you have all required #includes The code uses rand() but doesn't #include <cstdlib>. It's important to make sure ...

23

As it turns out, a similar question was asked recently on Math.SE. Rather than reinventing-the-wheel, take advantage of built-in functionality in Python. Your norm_cdf(z) is just a numerical approximation for P(z) = \frac{1}{\sqrt{2\pi}}\int_{-\infty}^{z} e^{-t^2/2}\ dt = \int_{-\infty}^{z} Z(t)\ dt = \frac{1}{2} \left( 1 + \mathrm{erf}\left( \frac{z}{\...

22

I have found a couple of things that could help you improve your code. Don't abuse using namespace std Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. Avoid the use of global variables It may make some sense to have SIZE_OF_GENERIC_ARRAY as a global variable, but not arithmeticAverage. It's generally ...

18

I'll only address the so-called "magic numbers" that several reviewers have mentioned. Sometimes, when you're working in pure mathematics, what seems at first glance to be a "magic number" really isn't. It may be that the numbers themselves are just part of the problem statement. I think the question boils down to this: can you come up with a name that is ...

16

Instead of math.pow, use the builtin ** operator. You don't need the \s at EOL, because the parentheses surrounding the expression allow it to implicitly span multiple lines. So after making both of those changes, I wind up with: def norm_cdf(z): """ Use the norm distribution functions as of Gale-Church (1993) srcfile. """ # Equation 26.2.17 from ...

16

Correctness of the solution On reflection, I'm not sure either the C++ or the Rust code solves the problem as stated. I'm not completely sure I understand the shoe shine shop model so I may be wrong. Here's what it looks like the code does: you generate a bunch of random events of all kinds, and order them in time. Then you process the events one by one ...

15

Not sure if this helps but you could easily define a function to evaluate the value of a polynom at a given position def evaluate_polynom(pol, x): return sum(a * math.pow(x, i) for i, a in enumerate(pol)) Then (0.319381530 * t) + (-0.356563782* math.pow(t,2)) + (1.781477937 * math.pow(t,3)) + (-1.821255978* math.pow(t,4)) + (1.330274429 * math.pow(t,5)...

15

The program cannot be made totally unbreakable. for(int i = 0; i < N; i++){ average += nums[i] / N; } for(int i = 0; i < N; i++){ total += nums[i]; } average = total / N; There are two ways to compute the average of a list of numbers, both of which can be broken: The method you use: divide each number by the size of the list, then add it to ...

15

Your original Python code translates line-for-line into C++11 as: #include <iostream> #include <map> #include <tuple> #include <vector> int main() { std::vector<int> lst = {1,2,4,3,1,3,1,4,5,15,5,2,3,5,4}; std::map<int, int> mydict = {}; int cnt = 0; int itm = 0; // in Python you made this a string '', ...

15

chooseNextWord distorts the probabilities. For example, consider a list of 3 words with the inherent probabilities $\frac{1}{3}$, $\frac{1}{3}$, $\frac{1}{3}$. The first word is selected with the probability $\frac{1}{3}$. The second, however is selected with probability $\frac{2}{9}$ ($\frac{2}{3}$ that the first word was not selected, times $\... 14 Bug This calculation looks suspiciously buggy: int tzm = ((double) avg * 3 + TEZA) / 4; Why are you taking avg, which is already a double, and casting it to a double? Furthermore, why are you taking the right-hand side, which is a double, and coercing it into an int variable? Note that you had declared double tzm earlier in the code; this int tzm ... 14 I see a number of things that may help you improve your program. Don't violate const The calculate_median routine takes a const array, makes a duplicate and then sorts the original! I think you meant to sort the duplicated array instead. Understand pointers In main there's this line: int numbers[] = { 1,7,3,4,5,6,7,8,9 }; // 9 elements // other stuff ... 13 What you want is a histogram of your values. If you want it to be flexible in the future with different bucket sizes, then you can use a std::vector and calculate how many buckets you need. If the values are in range [0, 1] and 0 < bucket_size < 1, then the number of buckets you need obviously is ceil(1 / bucket_size). So what I'd do would probably ... 13 I would try to use existing pandas features where possible to keep this code minimal - this aids readability and reduces the possibility of bugs being introduced in complicated loop structures. import pandas from scipy.stats import chi2_contingency def chisq_of_df_cols(df, c1, c2): groupsizes = df.groupby([c1, c2]).size() ctsum = groupsizes.unstack(... 13 public static double userInput(String userInput){ // ... if(userInput.matches("-?\\d+(\\.\\d+)?")){ // ... } else { System.err.println("ERROR: Non numeric input.Please check your file and try again."); System.exit(-1); } // ... } I was prompted to enter a number! I may have entered it wrongly, but why does it ask ... 13 You've got some pretty poor variable names there. For example, what's m2 supposed to mean? I don't like the fact, that result generates itself. I would rather have two separate classes: one to run a benchmark and one to represent benchmark result. Also calling some Benchmark.Run method makes much more sense semantically than calling BenchmarkResult.... 13 The docstrings are vague. Documentation needs to be precise and complete. It should answer questions like "what arguments should I pass?" and "what does it return?" For example, "go through the AST and count lines" should become something like, "Return the number of nodes in the AST rooted at node, and update the set line_nos with the line numbers of these ... 13 1. Design I think that using the @mean_of_experiments decorator is not the best approach to this problem: There might be cases where you want to run the underlying function by itself, for example in order to test it, but the decorator prevents this. You have to choose the number of experiments, N, when you define the function. This is restrictive: it means ... 13 On top of Thomas Ward answer, here is a short and important comment: More beautiful functions Your functions are performing some mathematical computations and handling the formatting to show the result to the 2 users. These correspond to 2 concerns that could be separated. Also, your functions rely on the list_one global variable. It would be clearer to ... 12 I prefer to use explicit access modifiers -- it keeps everything uniform: you have internal int[][] JaggedArray so I'd make the others like _minV explicitly private. Every field that is outside-world accessible, should be a property. This means internal, protected internal and public fields. The benefit of this is that you can always decide later to add ... 12 You are doing way too much work to validate the input; the Scanner already has a method for reading doubles: java.util.Scanner.nextDouble. Rather than doing all this crazy regex work, you should just use this along side java.util.Scanner.hasNextDouble to receive input. Using this will ensure that all input received is valid and that it is received ... 12 Useless code On the first line of code, I already saw a problem: double[] nums = new double[4]; nums = inputHandler(); Why do you allocate an array for nums and then immediately reassign it to something else? The first line is completely rendered useless. It should be: double[] nums = inputHandler(); You made the same mistake in inputHandler(... 12 Glaring Deficiencies Let me venture here that your getMedian method is slower than your getMode method. Thanks to your sort. You use a naive bubble-sort, which is$O(n^2)$. It can be done using$O(n\log(n))\\$ using quicksort (for practical input), mergesort, heapsort, etc. However, you can get the best of all worlds by just using the static method sort() ...

11

Yes, the code's terrible and yes you've used overloading incorrectly, but it's hard for me to see how you were supposed to fill your project spec without using it incorrectly. Your lecturer must be awful if this stuff is what he teaches. using namespace std; Don't, ever. std is full of an ever-increasing number of unconstrained template algorithms with ...

11

Please name your variables appropriately. Single-letter variables are in most cases sub-optimal and make it needlessly hard for the you-in-six-weeks to figure out what it does. I'm fairly sure your algorithm is sub-par as well, although I'm not that proficient in algorithms myself. I suspect a function like this already exists in the Python library, I'll ...

Only top voted, non community-wiki answers of a minimum length are eligible