Lab 1
“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Cover Topics
Console I/O
Expression Evaluation
Arithmetic Operators +, -, *, /
Compound Assignment Operators +=, -=, *=, /=
Increment and Decrement Operators ++, --
Lab Materials
Prelab Assignment
// Pre-Lab 1 Assignment //Copy and Paste this code into your IDE and complete the requested items. #include <iostream> using namespace std; int main() { // 1. // There are a several errors with the below code // Fix the syntax and logical errors. // The Program should take two integers from the user and assign them to x and y. // The program should then print the square of x and y + 10 int x; int y; x * x = x; y += 10; cout << "Enter the value for X" << endl; cin << x; cout << "Enter the value for Y" << endl; cin << y; cout << "X squared = " << x << endl << "Y + 10 = " << y << endl; // 2. // Write code that takes the number of pounds the user // can lift (use cin), divides that number by 2.20462, and then // prints it to the screen. This will convert pounds to kg. // (200 pounds is equal to 90.7186 kilograms) cout << "Many pounds can you lift?" << endl; // write your code here return 0; }
Prelab Reading
Review
#include <iostream> using namespace std; int main() { // variable declaration int myinteger; // variable assignment myinteger = 10; // both at the same time (variable initialization) string mystr = "hello world!"; // constant variable that cannot be changed. // note we usually write the name in all capitals // to help remind us that it is a constant. const double PI = 3.14159; return 0; }
It is best review your understanding for the above items, most concepts will build on each other over the semester. If you need assistance, you are encouraged to visit your faculty instructor or lab instructor during office hours. Additionally, free tutoring is available in the MCS 590 Lab Suite.
Console I/O
#include <iostream> using namespace std; int main() { int user_num; cout << "Please enter a number: "; // take a number from the user cin >> user_num; // print the user's number back to the console cout << "You entered the number " << user_num << endl; return 0; }
So far we learned how to diplay letters and sentences to the screen console using the cout statement.
examlple:
cout << "Print this to the console." << endl;
However, what if the program needs user input? To gather user input use the cin (console in) statement. The cin statement will take a value from the user and store the value in a specified variable.
Caution: The user input value must match the needed data type. In our example code, if the user entered "four" the program would not work properly. The value "four" is a string and would not work with our data type of integer.
Arithmetic Operators
#include <iostream> using namespace std; int main() { int a, b; a = 5 + 3; b = a * 4; cout << " a = " << a << " b = " << b << endl; a = a / 2; b = b - a; cout << " a = " << a << " b = " << b << endl; // modulo example int x = 11 % 3; cout << "11 % 3 = " << x << endl; // x is 2 because 11 / 3 = 3r2 return 0; }
+ : addition
- : subtraction
* : multiplication
/ : division
% : modulo
Several of these operators are probably familiar, expect for the modulo operator (%), which gives the remainder of the division of two numbers.
For example:
8 % 5 = 3
Because 8 / 5 = 1r3
Other examples:
6 % 9 = 6 because 6 / 9 = 0r6
0 % 4 = 0 because 0 / 4 = 0r0
10042 % 10 = 2 because 10042 / 10 = 1004r2
Compound Assignment Operators
#include <iostream> using namespace std; int main() { /* 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 = 3; cout << "y = " << y << endl; y += 5; cout << "y += 5 => y = " << y << endl << endl; y *= 3; cout << "y *= 3 => y = " << y << endl << endl; y /= 2; cout << "y /= 2 => y = " << y << endl << endl; return 0; }
Compound assignment operators are equivalent to assignment the result of an operation to the first operand.
Examples:
Operator | Expression | Equivalent To |
+= | x += 5; | x = x + 5; |
-= | y -= x; | y = y - x; |
*= | z *= 7; | z = z * 7; |
/= | w /= 2; | w = w / 2; |
Increment and Decrement
#include <iostream> using namespace std; int main() { 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 s after evaluating expression cout << "a = " << a << ", b = " << b << ", p = " << p << ", s = " << s << endl << endl; // output : a = 9, b = 8, p = 4, s = 4 return 0; }
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. For this class you should ONLY use the increment and decrement operator this way.
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.
Expression Evaluation
#include <iostream> using namespace std; int main() { // Try to figure out the answers before running this code in your IDE. cout << "4 + 6 % 3 = " << 4 + 6 % 3 << endl; cout << "5 + 6 * 3 = " << 5 + 6 * 3 << endl; cout << "(5 + 6) * 3 = " << (5 + 6) * 3 << endl << endl; int a = 5; // what is a? 15 or 35? why? a *= 2 + 5; cout << "a = " << a << endl; return 0; }
The compiler will follow the order of operations (PMDAS). Note: C++ does not have a basic built-in method of dealing with exponents.
Operator precedence
1. ++ --
2. ()
3. * / %
4. + -
5. = += -= *= /=
Integer Division
#include <iostream> using namespace std; int main() { // Integer division 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; return 0; }
When two integers are divided, the result is the quotient with any fractional part discarded (truncated). This is really important to remember!
Its Getting Hot In Here
#include <iostream> using namespace std; int main() { // 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; }