280 likes | 412 Views
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…
E N D
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
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". "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? Total: 2 4 6 8 10 12 LoopCounter: 1 2 3 4 5 6 done test for Total=12, done here
Written in “Code” semicolon int LoopCounter = 0; int Total = 0; while (Total < 12) { Total = Total + 2; LoopCounter = LoopCounter + 1; } no semicolon (because brackets follow) open bracket odd-looking algebra close bracket
A computer “Program” • A computer file. • In your Lab work area. • Written in an editor. • filename.cpp #include <iostream> using namespace std; int main( ) { cout<< “Hello” << endln; } Dear John, I think we should just be friends… Ashleigh
Basic C++ Program Framework your code here #include <iostream> using namespace std; int main() { } VERY confusing: - where to put semicolons everywhere but "conditionals" if and while - where to use brackets to start and end a program after conditionals brackets always paired
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. • intmain( ) { … 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 <iostream> #include <string> using namespace std; no ; enables keyboard & monitor uses ; what keyboard and monitor are called: cout and cin
Step 2 - main( ) • After setting up the environment, the computer looks for "int main( )" to begin execution of your program int main( ) { } never ; before brackets computer finds this then it runs, from here to here
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 <iostream> using namespace std; int main() { cout << "hello" << endl; return 0; }
step 6 - accept keyboard input #include <iostream> 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;
count to a million #include <iostream> using namespace std; int main( ){int x = 1;while ( x <= 1000000 ) { cout << x << endl; x = x+1; // what? }}// 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. 5, 2014 */
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++
the Assignment int x; x = 14; x = x + 1; what is x? C++
Keywords are C++ defined: int main char double while many more variables, file names are your choice Convention: lowerUpper Keywords vs. myWords
Libraries Programs that other people write that help you. #include <iostream> // enables C++ #include <string> // enables human-readable text #include <math.h> // enables math functions using namespace std; // enables cout<< and cin>>
Operators The usual: +-/ * Precedence: ( ) e.g. (y + b) * c not the same as y + b * c Negative numbers: -8
reading Read all of chapter 1 in the text