CMIS 102 arithmetic operators, int and float numbers, sqrt function, output formatting The arithmetic operators have the same precedence relationships as they do in math (it would be weird if they didn't). All the operators (45 or so of them) are in some precedence group. We'll learn only some of them in this course. Higher precedence means the operator is done before an operator of lower precedence. So, in this expression: x + y * z the multiplication is done before the addition because * has higher precedence than +. Parentheses override the precedence of operators, just like in math. So, in: (x + y) * z the + in the parens is done before the *. You need to be able to read and write arithmetic expressions. The chapter exercises provide lots of practice. You also need to be able to take a math or science formula and turn it into a C++ expression. You need to be aware that ints and floats are stored ("represented") in the machine in completely different formats. The bits that make up int 2 are completely different from the bit pattern that makes float 2. But we can freely mix ints and floats in expressions and the compiler will make everything the same (basically by automatically converting the int values to float format). This automatic conversion is called coercion, but the term isn't important to remember. If in an expression there's at least one float, the result will be float. Only if all values are ints will the result be int. The only dubious thing to do is to assign a float value to an int variable because the decimal part gets chopped off ("truncated") and so some information is lost. The only place you'll need to do casting in this course is if you have two int variables and you want to divide them and get the real result. An int divided by an int results in integer division: 7 / 2 is 3 One or both of the operands must be float for "real" division to occur. So if you have: int i=7, j=2; cout << i / j; //3 is output cout << float(i) / j; //3.5 is output. the value of i is "cast" to float 7.0. i is still int 7, it can't change. There are a bunch of built-in functions that you can use in your programs. You have to include the appropriate header file. Here's how the sqrt function might be used: float x; cout << "Enter a number" ; cin >> x; //input value from user for x cout << "Square root of that number is " << sqrt(x) << endl; float y; y = sqrt(x); The value of x is the value that will be square rooted. The "call" of the function evaluates to (sort of becomes) the square root of x, which is then output, in the first example, or assigned to be the value of y in the second example. Formatting the output. endl is a "manipulator" that is like a carriage return or Enter being output, causing the cursor to go to the beginning of the next line on the screen. You should separate output values with spaces. setw (set width) is useful for aligning columns of data: cout << setw(5) << x << setw(6) << y << endl; cout << setw(5) << z << setw(6) << w << endl; cout << setw(5) << a << setw(6) << b << endl; will produce: 376 49 8593 6273 2 385 assuming those are the values of the 6 variables. For float values, if you want the decimal point to appear and to not have scientific notation and for the precision specification to work, you have to issue this "magic incantation": cout << setprecision(2); cout << setiosflags(ios::fixed|ios::showpoint); //OR cout << fixed << showpoint; // floats will be output xxx.yy wxyz.dd Useful for $$$.cents