ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 2: Basic Elements of C++
Copyright © 2003 Pearson Education, Inc. Slide 1.
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
CS 240 Computer Programming 1
C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 4 Functions for All Subtasks Slides by David B. Teague, Western Carolina University.
Vectors, lists and queues
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
CSE202: Lecture 3The Ohio State University1 Assignment.
Engineering Drawing ELEC 106 Fundamentals of Electrical Engineering
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the.
Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Exercise 2.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
General Computer Science for Engineers CISC 106 Lecture 26 Dr. John Cavazos Computer and Information Sciences 04/24/2009.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
EE Lab Intro ELEC 106 Fundamentals of Electrical Engineering Dr. Ron Hayne.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Timer & Op Amp Circuits ELEC 106 Fundamentals of Electrical Engineering Dr. Ron Hayne Images from Timer, Op Amp & Optoelectronic Circuits & Projects by.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 9.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Introduction ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Data Files ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
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.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
Classes (Chapter ) Classes (Chapter ) Data with natural operations Classes Using Classes.
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Chapter 4 Loops Case Studies
CS149D Elements of Computer Science
Chapter 2 Elementary Programming
Starting Out with C++: From Control Structures through Objects
Anatomy of a Function Part 1
Programming with Data Files
Chapter 7 – Special Right Triangles Review
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Anatomy of a Function Part 1
Introduction to Functions
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Introduction to Algorithms and Programming COMP151
Presentation transcript:

ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Simple C++ Programs ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne ELEC 330

Program Structure Object-Based Programming Program Structure Dev-C++

Object-Based Programming Object-Oriented Programming Identify the data requirements of the problem How the data will be used in the program Abstract Data Types 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 sides of right triangle side1 = x2 - x1; side2 = y2 - y1; distance = 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 Preprocessor Directives Using Directive /*-----------------------------------------------*/ /* Program chapter1_1 */ // Declare and initialize objects Preprocessor Directives #include <iostream> #include <cmath> Using Directive using namespace std; 206_C2

Program Structure Main Function Declarations object types int main() { } Declarations object types initial values // Declare and initialize objects double x1(1), y1(3), x2(4), y2(7), side1, side2, distance; 206_C2

Program Structure Statements Return // Compute sides of right triangle side1 = x2 - x1; side2 = y2 - y1; distance = sqrt(side1 * side1 + side2 * side2); // Print distance cout << "The distance between the two points is " << distance << endl; Return return 0; 206_C2

General Program Structure preprocessing directives int main() { declarations; statements; } 206_C2

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

Dev-C++ New Source File Compile Run 206_C2

Windows Friendly Exit // Windows friendly exit system("PAUSE"); return 0; 206_C2

Summary Object-Based Programming Program Structure Dev-C++ 206_C2

Simple C++ Constants and Variables C++ Operators Standard Input and Output 206_C2

Constants and Variables Objects 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

Scientific Notation Floating-Point Scientific Notation 2.5, -0.004, 15.0 Scientific Notation 25.6 = 2.56 x 101 -0.004 = -4.0 x 10-3 Exponential Notation 25.6 = 2.56e1 -0.004 = -4.0e-3 Mantissa Precision Example Exponent Range 206_C2

Numeric Data Types Integers Floating-Point short int long float double long double 206_C2

Boolean Data Type Example Program Output bool error(false), status(true); cout << error << endl << status; Program Output 1 206_C2

Character Data Type ASCII Character Constant Appendix B 7-bit binary Single quotes 'A', 'b', '3' Can be interpreted as character or integer '3' != 3 206_C2

String Data String Constant String Objects Sequence of characters Double quotes "Fred", "C17" String Objects string class 206_C2

String Class /*-----------------------------------------------*/ /* This program prints a greeting */ /* using the string class. */ #include <iostream> #include <string> // Required for string class using namespace std; 206_C2

String Class Hello Jane Doe! int main() { // Declare and initialize two string objects. string salutation("Hello"), name("Jane Doe"); // Output greeting. cout << salutation << ' ' << name << '!' << endl; // Exit program. return 0; } Hello Jane Doe! 206_C2

Symbolic Constants Const Declared and initialized Cannot be changed within the program const double PI = acos(-1.0); const double LightSpeed = 2.99792e08; 206_C2

C++ Opeartors Assignment Operator Expression identifier = expression; Constant, Object, Result of an Operation double sum(10.5); int x1(3); char ch('a'); double sum; int x1; char ch; sum = 10.5; x1 = 3; ch = 'a'; 206_C2

Assignment Operator Multiple Assignments Type Conversion x = y = z = 0; Type Conversion long double double float long integer integer short integer info lost no loss 206_C2

Arithmetic Operators Unary Operators Binary Operators Positive + Negative - Binary Operators Multiplication * Division / Modulus % Addition + Subtraction - Precedence 2 3 4 206_C2

Mixed Operations Operation between values of different types Value of lower type promoted Cast Operator (type) Examples 206_C2

Expressions Distance = x0 + v0t + at2 Tension = ? double distance, x0, v0, a, t; distance = x0 + v0*t + a*t*t; Tension = ? 206_C2

Increment and Decrement Unary Operators Increment ++ Decrement -- Prefix ++count Postfix count-- Examples 206_C2

Abbreviated Assignment Abbreviated Assignment Operators x = x + 3; x +=3; y = y * 2; y *=2; Lowest Precedence (evaluate last) a = (b += (c + d)); a = (b = b + (c + d)); b = b + (c + d); a = b; 206_C2

Standard Output Standard Output Stream Manipulators #include <iostream> cout << "Hello " << name; Stream Manipulators #include <iomanip> setprecision(n), fixed, scientific setw(n), left, right, setfill(ch) dec, oct, hex endl 206_C2

Standard Output /*-----------------------------------------------*/ /* Program chapter2_4 */ /* */ /* This program computes area of a circle. */ #include <iostream> #include <iomanip> #include <cmath> using namespace std; 206_C2

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

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

Standard Input Standard Input Whitespace used as delimiters #include <iostream> cin >> var1 >> var2 >> var3; Whitespace used as delimiters blanks, tabs, newlines Values must be compatible with data type of objects Stream Manipulators #include <iomanip> skipws, noskipws 206_C2

Summary Constants and Variables C++ Operators Standard Input and Output 206_C2

Problem Solving Basic Functions Numerical Technique Linear Interpolation Problem Solving Applied Wind-Tunnel Data Analysis 206_C2

Basic Functions Basic Math Functions Elementary Math Functions #include <cmath> Arguments are type double Elementary Math Functions fabs(x), abs(n) sqrt(x), pow(x,y) ceil(x), floor(x) exp(x), log(x), log10(x) 206_C2

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

Practice velocity = sqrt(pow(v0,2) + 2*a*(x - x0)); 206_C2

Other Functions Hyperbolic Functions Character Functions sinh(x), cosh(x), tanh(x) Character Functions #include <cctype> toupper(ch), tolower(ch) isdigit(ch), isupper(ch), islower(ch) isspace(ch), ispunct(ch) 206_C2

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 206_C2

Example Data Set Estimate the temp at 2.6s Time, s Temp, deg F 0.0 1.0 20.0 2.0 60.0 3.0 68.0 4.0 77.0 5.0 110.0 206_C2

Problem Solving Applied Wind-Tunnel Data Analysis 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 Coefficient f(b) New Angle (b) 206_C2

Problem Solving Applied Hand Example Estimate coefficient of lift at 8.7 degrees Algorithm Development Read coordinates of adjacent points Read new angle Compute new coefficient Print new coefficient Angle (degrees) Coefficient of Lift 6 0.479 8 0.654 10 0.792 12 0.924 14 1.035 206_C2

Problem Solving Applied /*-----------------------------------------------*/ /* Program chapter2_5 */ /* */ /* This program uses linear interpolation to */ /* compute the coefficient of lift for an angle.*/ #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { 206_C2

Problem Solving Applied // Declare objects double a, f_a, b, f_b, c, f_c; // Get user input from the keyboard. cout << "Use degrees for all angle measurements. \n"; cout << "Enter first angle and lift coefficient: \n"; cin >> a >> f_a; cout << "Enter second angle and lift coefficient: \n"; cin >> c >> f_c; cout << "Enter new angle: \n"; cin >> b; 206_C2

Problem Solving Applied // Use linear interpolation to compute new lift. f_b = f_a + (b-a)/(c-a)*(f_c - f_a); // Print new lift value. cout << fixed << setprecision(3); cout << "New lift coefficient: " << f_b << endl; // Windows friendly exit system("PAUSE"); return 0; } 206_C2

Problem Solving Applied Testing 206_C2

Summary Basic Functions Numerical Technique Problem Solving Applied Linear Interpolation Problem Solving Applied Wind-Tunnel Data Analysis End of Chapter Summary C++ Statements Style Notes Debugging Notes 206_C2