Can someone check my code and help me with improving it please

Does the code below look right? Also how do I do testing for this code.
I need to :

Include testing of the various calculations and also of the overall behaviour of the pendulum: does it swing as You would expect? For example, you could try a start angle close to 3.1 or a large initial velocity…

What is the best way to document this testing?
This is for a "modelling a pendulum" experiment.

Last edited on
anyone? really struggling with this
not sure how to test something that doesn't require user input.
Line 18 should be for(i=1 ; i<steps ; i++). Otherwise the array accesses using i-1 in the loop are invalid.

To test it, try changing the initial values of omega, theta[0] and l. Look at the output values and see if they make sense.
HI,

I had these compile errors:

4:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wpedantic] In function 'int main()': 
31:21: error: 'system' was not declared in this scope


The first one is obvious: it's always int main()


I would change the float type to double everywhere, the precision of float is easily exceeded. Remember to change the printf to reflect that.

With the steps variable, I would do this:

6
7
8
9
10
     int i = 0;                      // step counter always initialise
     const std::size_t steps = 10000;  // could use unsigned int if you want
     double theta [steps];        // initial value for angle Is this so you can test 10000 different values?
     const double omega = 0.2;          // initial value for angular speed
     double time [steps];         // initial time 


This avoids having "magic" numbers like 10000 throughout the code.

I would also consider better variable names, if it is an initial value consider putting the word Initial in the variable name.

With the testing, it sounds to me that you teacher wants you to write more code to show the testing. Get some input from the user for all the variables, show all the results for each one. If using one of the scanf functions, make sure you use the return value to see that it worked.

Good Luck !!

Last edited on
Topic archived. No new replies allowed.