// Lab 2 Lecture #include using namespace std; int main() { // ------------------------------ Console I/O --------------------------------- // So far we have learned that we can print to the screen using cout // identifier (console out). cout << "print this to the console" << endl; // However, what if we want to take something from the user? // We can accomplish this by using the "cin" identifier (console in). // this will take a value from the user and put it inside a variable. // Note you must be careful to enter the correct data type. int usersNumber; cout << endl << "Enter a number" << endl; // take a number from the user cin >> usersNumber; // print the user's number back to the console cout << "You entered the number " << usersNumber << endl << endl; // ---------------- Arithmetic Operators - + - * / % ++ ----------------------- // Operator Description // ---------------------- // + addition // - subtraction // * multiplication // / division // % modulo // I am sure you are very familiar with each of the operators except // possibly the modulo operator (%), which gives the remainder of the // division of two values. // For example : int x = 11 % 3; cout << "11 % 3 = " << x << endl << endl; // x is 2 because 11 / 3 = 3r2 // Example 2 : 8 % 5 = 3 because 8 / 5 = 1r3 // Example 3 : 6 % 9 = 6 because 6 / 9 = 0r6 // Example 4 : 0 % 4 = 0 because 0 / 4 = 0r0 // Example 5 : 10042 % 10 = 2 because 10042 / 10 = 1004r2 // ----------------------- Compound Assignment Operators ---------------------- // Are equivalent to assigning the result of an operation to the // first operand. // Operator Expression Equivalent To // ----------------------------------------- // += x += 5; x = x + 5; // -= y -= x; y = y - x; // *= z *= 7; z = z * 7; // /= w *= y; w = w * y; int y = 2; cout << "y = " << y << endl; y += 5; cout << "y += 5 => y = " << y << endl << endl; // ----------------------- Increment and Decrement ---------------------------- // Operator Expression Equivalent To // ----------------------------------------- // ++ x++; or ++x; x = x + 1; // -- x--; or --x; x = x - 1; // As you can see, you are able to use this operator as a prefix and as a // suffix. In a simple expression, as seen above, they have exactly the // same outcome. However, when the operator is used as a prefix the value // is incremented or decremented before evaluating the containing // expression. Whereas, when the operator is a suffix the value is // incremented or decremented after the containing expression. // For example int p = 3; int s = 3; cout << "p = " << p << " s = " << s << endl; cout << "++p is " << ++p << " s++ is " << s++ << endl; cout << "p = " << p << " s = " << s << endl << endl; // output: // p = 3 s = 3 // ++p is 4 s++ is 3 // p = 4 s = 4 // Example 2 p = 3; s = 3; int a; int b; a = ++p + 5; // add 1 to p before evaluating expression b = s++ + 5; // add 1 to p after evaluating expression cout << "a = " << a << ", b = " << b << ", p = " << p << ", s = " << s << endl << endl; // output : a = 9, b = 8, p = 4, s = 4 // ------------------------- Expression Evaluation ---------------------------- // The compiler will follow the order of operations (PMDAS). Note that // C++ does not have a basic built in method of dealing with exponents. // Example: cout << "5 + 6 * 3 = " << (5 + 6 * 3) << endl; cout << "(5 + 6) * 3 = " << ((5 + 6) * 3) << endl << endl; // Integer division // when two integers are divided the result is the quotient with any // fractional part discarded (truncated). This is really important to // remember! // Example: cout << "Integer Division" << endl; cout << "4 / 3 = " << (4 / 3) << endl; cout << "3 / 4 = " << (3 / 4) << endl; cout << "12 / 5 = " << (12 / 5) << endl; // The division of two integer variables will also result in integer division int intA = 3; int intB = 4; cout << "intA / intB = " << (intA / intB) << endl; // However, if either the divisor or dividend is a floating point value // then the result will be a floating point number. double doubleA = 3.0; cout << "4 / 3.0 = " << (4 / 3.0) << endl; cout << "intB / doubleA = " << (intB / doubleA) << endl << endl; // ------------------------- Example 1 ---------------------------------------- // Programs work sequentially from top to bottom. This is important to // make sure you do things in a logically correct order. The following // program will not work if any of the following steps are reordered. // declare variables float tempF; float tempC; // Take a value from the user and assign it to 'tempF' cout << "Please enter a temperature in fahrenheit:" << endl; cin >> tempF; // do math tempC = (tempF-32)*(5.0/9); // print result to screen cout << tempF <<"F = " << tempC << "C"<< endl << endl; // ---------------------------------------------------------------------------- return 0; }