Presentation is loading. Please wait.

Presentation is loading. Please wait.

ASEE / GradSWE C++ Workshop

Similar presentations


Presentation on theme: "ASEE / GradSWE C++ Workshop"— Presentation transcript:

1 ASEE / GradSWE C++ Workshop
While we’re waiting… Follow the “Getting Started” directions at Open Visual Studio from the Start menu: Start / All Programs / Software Development Tools / Visual Studio 2013 / Visual Studio 2013

2 ASEE C++ Workshop Aaron Bevill

3 Common Programming Languages
Compiled Fortran C C++ C# Java Interpreted bash C shell Perl make Python MatLab R SPSS scientific calculation www

4 Overview Syntax, arithmetic, and variables Blocks and scope
Flow control: if, else, and for Vectors and arrays Functions Comprehensive example Further reading

5 Compiling and Running C++
Visual Studio Create an empty Visual C++ project Add a new source file *.cpp Write source Build / run Linux Terminal Create and edit source file *.cpp Compile: g++ source.cpp -o executable Execute: ./executable

6 Basic Syntax // import std::cout as cout: #include <iostream>
using namespace std; int main() { cout << "hello world" << endl; return 0; }

7 Arithmetic Operators #include <math.h> // pow // in main:
cout << << endl; cout << << endl; cout << 4 * 3 << endl; cout << 24 / 5 << endl; cout << 24 % 5 << endl; cout << pow(2,3) << endl;

8 Variables // in main: //declare and assign
//variables before using them int x; x = 2; double y = 2.; cout << x / 2 << endl; cout << y / 2 << endl;

9 Variable Types int 7 11 float 0.4 2e6 1. double 0.4 2e6 1.
bool true false char 'a' 'f'

10 C++ vs MatLab Use pow(x,y) for an exponent
A decimal or scientific notation (1E4) indicates a floating point constant Use ^ for an exponent All numbers are assumed to be floating point

11 Blocks and Scope int x = 2; { int y = 3;
cout << x << endl; cout << y << endl; } // y is no longer in scope

12 Flow Control---conditionals
int x = 5; if(x < 0) { cout << "x is small" << endl; } else cout << "x is large" << endl;

13 Example 0: Impact Distance
in a new project: define constants G = m/s2 VX, VY0 (guess) calculate impact print impact state is 100 < x < 120? use if, else if, else GOAL: Find initial velocities such that 100 < x < 120 5 minutes

14 Vectors and Arrays // preamble for vector: #include<vector>
using namespace std; // array, length 3: int x[3] = {1, 2, 3}; cout << x[0] << x[1] << x[2]; // vector, length 3: vector<int> y(3, 4); cout << y[0] << y[1] << y[2];

15 Arrays vs Vectors An array is a direct allocation of memory for a fixed number of objects cannot be resized possible to “seg fault” from indexing error A vector is a coder-friendly wrapper on an array, implemented using templating it can be resized also possible to seg fault

16 Flow Control---for loops
vector<double> x(4,8); // initialize; condition; increment for(int i = 0; i < x.size(); i++) { cout << x[i] << endl; }

17 Example 1: Multiple Projectiles
define constants G, VX list of VY0 for each value of VY0 calculate impact print impact state is 100 < x < 120? GOAL: Find initial velocities such that 100 < x < 120 5 minutes

18 Functions // first, define or declare the function
double abs1(double x) { if(x < 0.) return -1 * x; } return x; // then use it in main() : cout << abs1(-5) << endl;

19 Example 2: Projectile Function
create a function range(vx, vy0) that returns the impact x call that function inside the for loop GOAL: Find initial velocities such that 100 < x < 120 3 minutes

20 Further Reading---Pointers
// // a pointer is the memory address of another variable int x = 4; // declare with a * int * x_pointer; // reference x to get a pointer to it x_pointer = & x; // de-reference x_pointer to retrieve the value cout << *x_pointer << endl; // or view the address cout << x_pointer << endl;

21 Further Reading---File I/O
// #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }

22 Further Reading---Classes (OOP)
A class brings together data and functions that act on that data. For example, you could create a class ChessPiece with integers x and y (the position on the board, or -1 if captured), kind (a flag to indicate pawn, rook, etc) and color (a flag for black or white). The ChessPiece class could have functions move_to(x, y) to move it and capture() to capture it. These functions use and manipulate the data. A “constructor” function is implemented to create ChessPiece objects. We can declare and initialize ChessPiece “objects” using the constructor. For example, the white rook at position (0,0) is an object: const int ROOK=4, WHITE=1; // define flag constants ChessPiece white_left_rook(0, 0, ROOK, WHITE); white_left_rook.move_to(4, 0); white_left_rook.capture();

23 Example 3: Estimating Pi
See create function: bool inside(x, y) returns true if (x,y) is in the unit circle sample 105 points (x, y) call inside(x, y) to count how many are inside a unit circle compare fraction inside circle to pi / 4 GOAL: Find the fraction of random x,y points 0 < x < 1 0 < y < 1 that fall in a unit circle 8 minutes

24 Advanced Syntax Exercises
Guess the output! Questions?

25 inline operations int x = 509; x += 6; cout << x << endl;

26 matrices // 2D, 3D, etc. data structures can be created using arrys, vectors, or classes double eye[2][2] = { {1., 0.}, {0., 1.} }; cout << eye[0][0] << endl; vector< vector<double> > mat3(2, vector<double>(2,3.)); cout << mat3[0][0] << endl; // class example omitted for brevity

27 passing by reference void increment_both(int x, int & y) { x++; y++; }
// void increment_both(int x, int & y) { x++; y++; } // in main(): int x=0, y=0; increment_both(x, y); cout << x << endl; cout << y << endl;

28 for-continue-break char words[] = "wrcl cmbqsa";
for(int i = 0; i < 11; i++) { if(words[i] == 'c') continue; } if(words[i] == 'b') break; cout << words[i] << endl;

29 formatted I/O // see http://www.cprogramming.com/tutorial/iomanip.html
std::cout << setprecision(3) << ;

30 inline if-else int x = 4; int y = (x < 7) ? 1 : -1;
cout << y << endl;

31 multi-line comments /* comments can span multiple lines
using the slash-asterisk and asterisk-slash */ cout << "this is outside the comment" << endl;

32 throw and try-catch // try { throw 20; } catch (int e) cout << "caught excetpion number " << e << endl; try { throw 30; } if(e != 20) { throw; }


Download ppt "ASEE / GradSWE C++ Workshop"

Similar presentations


Ads by Google