ASEE / GradSWE C++ Workshop

Slides:



Advertisements
Similar presentations
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Advertisements

© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Chapter 2: Introduction to C++.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization Introduction to C.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Chapter 9 Scope, Lifetime, and More on Functions.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Pointers OVERVIEW.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Chapter 9 Scope, Lifetime, and More on Functions.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
Chapter 1.2 Introduction to C++ Programming
Chapter 13 Introduction to C++ Language
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Basic Elements of C++.
C++ in 90 minutes.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Chapter 9 Scope, Lifetime, and More on Functions
Chapter 2 Elementary Programming
CS 2308 Exam I Review.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
2.1 Parts of a C++ Program.
Sridhar Narayan Java Basics Sridhar Narayan
Lab 1 Introduction to C++.
When your program crashes
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Seoul National University
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Arrays Arrays A few types Structures of related data items
Subject:Object oriented programming
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Seoul National University
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Presentation transcript:

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

ASEE C++ Workshop Aaron Bevill 2015-03-31

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

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

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

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

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

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;

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

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

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

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

Example 0: Impact Distance in a new project: define constants G = -9.81 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

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];

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

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

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

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;

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

Further Reading---Pointers // http://www.cplusplus.com/doc/tutorial/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;

Further Reading---File I/O // http://www.cplusplus.com/doc/tutorial/files/ #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; }

Further Reading---Classes (OOP) http://www.cplusplus.com/doc/tutorial/classes/ 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();

Example 3: Estimating Pi See http://goo.gl/OdOfbK 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

Advanced Syntax Exercises Guess the output! Questions? ambevill@umich.edu

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

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

passing by reference void increment_both(int x, int & y) { x++; y++; } // http://www.cplusplus.com/doc/tutorial/functions/#reference 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;

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;

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

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

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

throw and try-catch // http://www.cplusplus.com/doc/tutorial/exceptions/ try { throw 20; } catch (int e) cout << "caught excetpion number " << e << endl; try { throw 30; } if(e != 20) { throw; }