Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

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 (called “concatenation”)

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". He returns with 12 loaves of bread. "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? done Total: LoopCounter: test for Total=12, done here

6 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

7 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

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

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

10 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”

11 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

12 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

13 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

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

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

16 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

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 Scientific notation Try this: double x; x = e 6; cout.scientific; cout << x << endl; cout.fixed; //eliminates scientific notation cout.precision(12); cout << x << endl;

19 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

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

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

22 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 */

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

24 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++

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

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

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

28 reading Read all of chapter 1 in the text


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

Similar presentations


Ads by Google