Beruflich Dokumente
Kultur Dokumente
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Program Structure
Object-Based Programming Program Structure Dev-C++
206_C2
Object-Based Programming
Object-Oriented Programming
Identify the data requirements of the problem How the data will be used in the program
Class Inheritance
206_C2
C++ Program
/*-----------------------------------------------*/ /* Program chapter1_1 */ /* */ /* This program computes the distance between */ /* two points */ #include <iostream> #include <cmath> using namespace std;
206_C2
C++ Program
int main() { // Declare and initialize objects double x1(1), y1(3), x2(4), y2(7), side1, side2, distance; // Compute side1 = x2 side2 = y2 distance = sides of right triangle - x1; - y1; sqrt(side1 * side1 + side2 * side2);
206_C2
C++ Program
// Print distance cout << "The distance between the two points is " << distance << endl; // Windows friendly exit system("PAUSE"); return 0; } /*----------------------------------------------*/
206_C2
Program Structure
Comments
/*-----------------------------------------------*/ /* Program chapter1_1 */
// Declare and initialize objects
Preprocessor Directives
#include <iostream> #include <cmath>
Using Directive
using namespace std;
206_C2 7
Program Structure
Main Function
int main() {
}
Declarations
// Declare and initialize objects double x1(1), y1(3), x2(4), y2(7), side1, side2, distance;
206_C2 8
Program Structure
Statements
// Compute side1 = x2 side2 = y2 distance = sides of right triangle - x1; - y1; sqrt(side1 * side1 + side2 * side2);
// Print distance cout << "The distance between the two points is " << distance << endl;
Return
return 0;
206_C2 9
206_C2
10
Dev-C++
Bloodshed Software
http://www.bloodshed.net/dev/devcpp.html Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2 Download from:
SourceForge
206_C2
11
Dev-C++
New Source File
Compile
Run
206_C2
12
206_C2
13
Summary
Object-Based Programming Program Structure Dev-C++
206_C2
14
Simple C++
Constants and Variables C++ Operators Standard Input and Output
206_C2
15
Constants
Specific values
Variables
Memory locations
Identifiers
Begin with alphabetic character Lowercase or uppercase letters (case sensitive) Can contain digits (not first character) Cannot be a keyword
206_C2 16
Scientific Notation
Floating-Point
Mantissa
Precision
Scientific Notation
Example
Exponent
Range
Exponential Notation
206_C2
17
Floating-Point
206_C2
18
Program Output
0 1
206_C2
19
Character Constant
206_C2
20
String Data
String Constant
"Fred", "C17"
String Objects
string class
206_C2
21
String Class
/*-----------------------------------------------*/ /* This program prints a greeting */ /* using the string class. */ #include <iostream> #include <string> // Required for string class using namespace std;
206_C2
22
String Class
int main() { // Declare and initialize two string objects. string salutation("Hello"), name("Jane Doe"); // Output greeting. cout << salutation << ' ' << name << '!' << endl; // Exit program. return 0;
206_C2
23
Symbolic Constants
Const
206_C2
24
C++ Opeartors
Assignment Operator
identifier = expression;
Constant, Object, Result of an Operation
double sum; int x1; char ch; sum = 10.5; x1 = 3; ch = 'a';
206_C2 25
Expression
Assignment Operator
Multiple Assignments
x = y = z = 0;
Type Conversion
info lost
no loss
206_C2
26
Arithmetic Operators
Unary Operators
Precedence
+ * / % + 206_C2
Binary Operators
27
Mixed Operations
Operation between values of different types
(type)
Examples
206_C2
28
Expressions
Distance = x0 + v0t + at2
double distance, x0, v0, a, t; distance = x0 + v0*t + a*t*t;
Tension = ?
206_C2
29
Increment Decrement
++ --
++count count--
206_C2
30
Abbreviated Assignment
Abbreviated Assignment Operators
x = x + 3; y = y * 2;
x +=3; y *=2;
Standard Output
Standard Output
#include <iostream> cout << "Hello " << name; #include <iomanip> setprecision(n), fixed, scientific setw(n), left, right, setfill(ch) dec, oct, hex endl
Stream Manipulators
206_C2
32
Standard Output
/*-----------------------------------------------*/ /* Program chapter2_4 */ /* */ /* This program computes area of a circle. */ #include <iostream> #include <iomanip> #include <cmath> using namespace std;
206_C2
33
Standard Output
const double PI=acos(-1.0); int main() { // Declare and initialize objects. double radius(4.6777), area; // Compute area area = PI*radius*radius;
206_C2
34
Standard Output
// Output results cout << setprecision(4) << "The radius of the circle is: " << setw(10) << radius << " centimeters" << endl; cout << scientific << "The area of the circle is: " << setw(12) << area << " square centimeters" << endl;
206_C2
35
Standard Input
Standard Input
#include <iostream> cin >> var1 >> var2 >> var3; blanks, tabs, newlines
206_C2
36
Summary
Constants and Variables C++ Operators Standard Input and Output
206_C2
37
Problem Solving
Basic Functions Numerical Technique
206_C2
38
Basic Functions
Basic Math Functions
#include <cmath>
Arguments are type double
206_C2
39
Basic Functions
Trigonometric Functions
Angles in radians (180 degrees = radians) sin(x), cos(x), tan(x) asin(x), acos(x), atan(x), atan2(y,x)
const double PI = acos(-1.0); double angle_deg, angle_rad; angle_deg = angle_rad*(180/PI); angle_rad = angle_deg*(PI/180);
206_C2 40
Practice
2 Velocity v0 2a( x x0 )
206_C2
41
Other Functions
Hyperbolic Functions
Character Functions
206_C2
42
Interpolation
Use data points to determine estimates of a function f(x) for values of x that were not part of the original set of data
Cubic-spline Interpolation
Third-degree polynomial
Linear Interpolation
Straight line a<b<c ba [ f (c) f (a)] f (b) f (a ) ca
206_C2 43
Example
Data Set
Time, s Temp, deg F 0.0 0.0 1.0 20.0 2.0 3.0 4.0 5.0 60.0 68.0 77.0 110.0
206_C2
44
Problem Statement
Use linear interpolation to compute a new coefficient of lift for a specified flight-path angle
Input/Output Description
Data Point (a, f(a)) Data Point (c, f(c)) New Angle (b) New Coefficient f(b)
206_C2
45
Hand Example
Algorithm Development
Read coordinates of adjacent points Read new angle Compute new coefficient Print new coefficient
206_C2
46
206_C2
48
206_C2
49
Testing
206_C2
50
Summary
Basic Functions Numerical Technique
Linear Interpolation
206_C2
51