![]() Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time. If, like me, you're curious about the rationale for the change to the C standard read this question. Reasons against omitting seem to boil down to "it looks weird". We rely on automatically generated member functions in C++, and few people write explicit return statements at the end of a void function. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0 Īll versions of both standards since then (C99 and C++98) have maintained the same idea. Instead of your existing code for generate, you could use this: bool generate() that terminates the main function returns a value of 0.įor C++, since the first standard in 1998 see ISO/IEC 14882:1998 section 3.6.1: The way you're using rand() could be greatly improved. Similarly runExperiment is a decent function name but generate and getInfo are not (generate what? get what info?). However, the variables in runExperiment are named m, n, a, b, etc. The variables numTrials and numExperiments are good names because they hint at the significance of those variables to this program. For example, the runExperiment function relies on numExperiment and numTrials, so those should be passed parameters. It's generally better to explicitly pass variables your function will need rather than using the vague implicit linkage of a global variable. Avoid the use of global variablesĪs you have already noted, global variables should be avoided in favor of local variables. Only include files that are actually needed. The inverse of the above suggestion is to avoid having extra #includes. It's important to make sure you have all required include files to make sure your program compiles reliably. The code uses rand() but doesn't #include. Make sure you have all required #includes ![]() Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. There's a lot that can be improved here, so I hope that these suggestions are useful to you. Int numExperiments = 1000 //Number of experimentsīool printT = false //Whether to print the results of each trial Int numTrials = 5 //Number of trials per experiment Int choose(int, int) //Performs a combination (nCk)ĭouble probability(int) //Calculates the probability of a coin hitting heads n times (binomial thrm)ĭouble chance = 0.5 //Chance of landing on heads Void getInfo() //Gets the information to perform the experiment Void runExperiment() //Performs and prints the results of the trials Name Lastnameīool generate() //Performs a coin flip with the given chance Right off the bat I know that I should pass things by reference instead of by value and that I shouldn't have used global variables. ![]() I'm pretty new to computer science so I'd like to learn the correct practices and everything. I turned this in already but I really enjoyed doing the assignment and I'd like to know how to make it better, faster, etc. Then, it displays the results, as well as the theoretical and observed probabilities of each event happening. For example, given 5 trials per experiment and 20 experiments, the program will flip a coin 5 times and record the results 20 times. Asks the user for the chance of a coin landing on heads, the number of trials per experiment, and the number of experiments.
0 Comments
Leave a Reply. |