# Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.

## Presentation on theme: "Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264."— Presentation transcript:

Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264

An example…. Describe how to tie a shoe… How to text message someone… Locate a human on a help line… They are all instructions that have "repeats" and "retries" (called iterations) and "if this" and "do that" (called conditionals) top to bottom left to right each step depends on the steps before (called “concatenation”)

Instructions have to be explicit A programmer is told: "Go to the store and buy a loaf of bread. If they have eggs, buy a dozen". He returns with 12 loaves of bread. "Please peel half the potatoes that are in the bag and cook them."

4 algorithmic tools concatination - one step at a time iteration - repeat until a condition is met conditional - make a decision transfer of control - end and start something new

Programs: Complicated, Concatenated Algorithms integer Total, LoopCounter Total = 0 LoopCounter = 0 Start: Add 2 to Total Add 1 to LoopCounter Go Back to Start Until Total = 12 What is LoopCounter? done Total: 2 4 6 8 10 12 LoopCounter: 1 2 3 4 5 6 test for Total=12, done here

Written in “Code” int LoopCounter = 0; int Total = 0; while (Total < 12) { Total = Total + 2; LoopCounter = LoopCounter + 1; } semicolon open bracket odd-looking algebra no semicolon (because brackets follow) close bracket

Basic C++ Program Framework #include using namespace std; int main( ) { } VERY confusing: - where to put semicolons everywhere but on "conditionals" if and while - where to use brackets to start and end a program or functional paragraph brackets always paired your code here

C++ Commands go in the brackets tells the computer what to do, one step at a time mathematical, algorithmic follows a strict “syntax” in C++, brackets { } always surround "functional paragraphs"

int main( ) memorize it… it means something. the computer - when asked to run your program - looks for the main to start. int main( ) { … put your program here } in C++, brackets always surround "functional paragraphs"

Step 1 - The info at the top The computer views the top few lines to set up a basic execution environment #include using namespace std ; no ; enables keyboard & monitor uses ; what keyboard and monitor are called: cout and cin no ; additional “libraries”

Step 2 - main( ) After setting up the environment, the computer looks for "int main( )" to begin execution of your program int main( ) { } computer finds this then it runs, from here to here never ; before brackets

step 3 - any variables? int main( ) { int x = 0; double y = 1.0; char z = 'Q'; string a = "hello"; } note: x, y, z, and a will "hold" changeable values a counting variable a measuring variable a keyboard character a word or phrase

step 4 - provide a way to EXIT Easy: int main( ) { return 0; } Note: computer exits at last } anyways, but programmers like to be explicit computer finds this then it runs, from here to here

step 5 - write to the monitor #include using namespace std ; int main( ) { cout << "hello" << endl ; return 0 ; }

cout can do “strings” or variables #include using namespace std ; int main( ) { double pi = 3.14159 ; cout << pi << endl ; return 0 ; }

step 6 - accept keyboard input #include using namespace std; int main( ) { int y = 0; cout << "Enter an integer: "; cin >> y; cout << "The integer is "; cout << y << endl; return 0; } // end of program

cout and cin cout << "Enter an integer: "; cin >> y; cout << "The integer is "; cout << y << endl; or a combination of outputs: cout << "The integer is " << y << endl;

Scientific notation Try this: double x; x = 35.62 e 6; cout.scientific; cout << x << endl; cout.fixed; //eliminates scientific notation cout.precision(12); cout << x << endl;

cout - decimal precision cout only displays 6 decimal characters cout attempts to reduce output to save characters use "cout. precision( n ); " to set displayable values to n decimal characters

count to a million #include using namespace std; int main( ) { int x = 0; while ( x <= 1000000 ) { x = x+1; // what? cout << x << endl; } } // end program

step 7 - Comments included in your “program” read by humans, not the computer used to explain what you intend the code to do

Commenting code /* anything between “delimiters” */ // anything after two slashes Header - comments at the beginning of a program // one line only /* EAS230 Author: Harry Truman Date: Feb. 4, 2015 */

Readability Indentation Skipped lines Brackets Comments Pages & Functional paragraphs Meaningful variable names

The Assignment Statement “=” int main ( ) { int A; A = 4; A = A + 3; } NOT “A equals 4” instead: “A is assigned the value 4” semicolon, brackets, main… must be C++

Keywords vs. myWords Keywords are C++ defined: –int –main –char –double –while –many more variables, file names are your choice Convention: –lowerUpper

Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions using namespace std; // enables cout >

Operators The usual: + - / * Precedence: use ( ) e.g. (y + b) * c not the same as y + b * c Negative numbers: - 8