1 / 28

Algorithms

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…

joanna
Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. 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. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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"

  10. 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"

  11. 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

  12. 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

  13. 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

  14. 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

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

  16. 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

  17. 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;

  18. 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

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

  20. 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 */

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

  22. 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++

  23. the Assignment int x; x = 14; x = x + 1; what is x? C++

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

  25. 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>>

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

  27. reading Read all of chapter 1 in the text

More Related