Physics and Astronomy |

Back to top
On this page
Contents ## More about variables, expressions and assignment## Arithmetic expressionsArithmetic expressions use the symbols "
Having calculated an expression we can use it as the argument
to a function such as: No prizes for guessing what the ## Example: height of a planeIf we are writing a program dealing with a plane of gradient 1 in both the x and y directions (i.e. an absolute gradient of sqrt(2)), and we want to know the distance of a point on the plane from the origin, then our code might look like this (where we have added a couple of "interesting" lines at the end): // Calculate the height of a plane of gradient 1 // in both the x and y directions. #include <stdio.h> #include <math.h> int main() { double x, y, z, distance; x = 2.1; y = 1.3; z = x + y; distance = sqrt(x*x + y*y + z*z); printf("Distance; %g\n", distance); // See what happens now x = 10.0; y = y + x; printf("Distance; %g\n", distance); // More code here... return 0; } Remember: An Mathematical operators have the same precedence (bind as closely) as normal arithmetic. - Step through the above "Key example".
- To demonstrate assignment
- Step through the above "Key example" in a new window.
- Press "Start program" and see the variables
spring into existence with random value.
- Step through the code one step at a time stopping just before the second assignment of x (x = 10.0)
- Notice the extremely simplistic way the computer performs
calculations, and stores and retrieves the values.
## Arithmetic assignmentIt is essential to realise that the statements above with equals signs such as: z = x + y; are one-off arithmetic
assignments, not lasting mathematical relationships.
(Remember, we are dealing with When it gets to the statement " Of course the compiler doesn't "think" at all
and it certainly doesn't realise the significance of the word
"distance". For all it cared we could have called the four
variables ( - Continue stepping through the above example
- To demonstrate that assignment is an action not a relationship and has no side-effects
- Continue stepping through the above code changing the values of x and y.
- Notice how the value of z does not change.
Statements such as: ## When the value of a variable changes its old value is gone foreverIf we change the value of a variable there is
no way to retrieve it later. For example, in the following
code the original values of the variables #include <stdio.h> int main() { double materials, labour, total_cost; materials = 9.4; labour = 11.3; total_cost = materials + labour; printf("The first cost is %g\n", total_cost); materials = 14.3; // The old value of materials has gone forever labour = 21.2; total_cost = materials + labour; printf("The second cost is %g\n", total_cost); return 0; } When the value of a variable changes its old value is gone forever - Change the value of mass or velocity in your momentum calculation
- To illustrate that changing the value of a variable does not automatically change the values of variables previously calculated from it
- Find your "momentum" exercise from the last lesson.
- Copy-and-Paste the
`printf()`statement that prints out the mass, velocity and momentum so that you now have two copies, on two separate lines. Build & run and check that it does print them out twice. - In between these two lines insert a line that changes the value of
either the mass or velocity (or insert two lines and change hem both).
- What does your program produce now?
- Does the second value of the momentum reflect the new value of mass and/or velocity?
- Do you understand why/why not? If not, step through the example above again.
- Finally, copy the line with the momentum calculation
and paste it into the correct
place for it to update the momentum to the correct new value.
- What is the corrrect place to put this second calculation? Think through the steps the program will take and do the calculation when the mas and/or velocity will have the new values.
## Standard mathematical functionsAs mentioned in the previous lecture, putting the the following compiler directive at the top of our file: #include <math.h> (note the Americanism it's
We shall see below that the include file ## Some useful mathematical functions
All arguments and returned values are ## Note: atan2() The function
- Mathamatical functions
- To practice using built-in mathematical functions.
- Create a new on-line program in a new window with a short comment at the top saying it demonstrates using mathematical functions.
- Immediately after your
program includes <stdio.h> include <math.h>.
You will need to do this on a separate lines:
#include <stdio.h> #include <math.h> - As always,
**type this in**, don't copy and paste. Typing it in will help fix it in your mind.
- As always,
- Declare a
**double**variable called`x`. Give it a positive value less than one. - Print out the value of its sine, square root, and hyperbolic sine of
`x`along with a few other mathematica functions. - Try printing the value of
`sin(asin(x))`. Does it do what you expect? - Try calling the
`asin()`function with suitable values to answer the question:- Does
`asin()`return values in the range 0 to π or -π/2 to π/2?
- Does
## Warning: don't confuse |