Simple C Programs - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Simple C Programs

Description:

Identify the data requirements of the problem. How the data ... ceil(x), floor(x) exp(x), log(x), log10(x) 206_C2. 40. Basic Functions. Trigonometric Functions ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 52
Provided by: drron6
Category:
Tags: ceil | programs | simple

less

Transcript and Presenter's Notes

Title: Simple C Programs


1
Simple C Programs
  • ELEC 206
  • Computer Applications for Electrical Engineers
  • Dr. Ron Hayne

2
Program Structure
  • Object-Based Programming
  • Program Structure
  • Dev-C

3
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

4
C Program
  • /-----------------------------------------------
    /
  • / Program chapter1_1
    /
  • /
    /
  • / This program computes the distance between
    /
  • / two points
    /
  • include ltiostreamgt
  • include ltcmathgt
  • using namespace std

5
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)

6
C Program
  • // Print distance
  • cout ltlt "The distance between the two points is
    "
  • ltlt distance ltlt endl
  • // Windows friendly exit
  • system("PAUSE")
  • return 0
  • /----------------------------------------------/

7
Program Structure
  • Comments
  • /-----------------------------------------------
    /
  • / Program chapter1_1
    /
  • // Declare and initialize objects
  • Preprocessor Directives
  • include ltiostreamgt
  • include ltcmathgt
  • Using Directive
  • using namespace std

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

9
Program Structure
  • Statements
  • // Compute sides of right triangle
  • side1 x2 - x1
  • side2 y2 - y1
  • distance sqrt(side1 side1 side2 side2)
  • // Print distance
  • cout ltlt "The distance between the two points is
    "
  • ltlt distance ltlt endl
  • Return
  • return 0

10
General Program Structure
  • preprocessing directives
  • int main()
  • declarations
  • statements

11
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

12
Dev-C
13
Windows Friendly Exit
  • // Windows friendly exit
  • system("PAUSE")
  • return 0

14
Summary
  • Object-Based Programming
  • Program Structure
  • Dev-C

15
Simple C
  • Constants and Variables
  • C Operators
  • Standard Input and Output

16
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

17
Scientific Notation
  • Floating-Point
  • 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

18
Numeric Data Types
  • Integers
  • short
  • int
  • long
  • Floating-Point
  • float
  • double
  • long double

19
Boolean Data Type
  • Example
  • bool error(false), status(true)
  • cout ltlt error ltlt endl ltlt status
  • Program Output

0 1
20
Character Data Type
  • ASCII
  • Appendix B
  • 7-bit binary
  • Character Constant
  • Single quotes
  • 'A', 'b', '3'
  • Can be interpreted as character or integer
  • '3' ! 3

21
String Data
  • String Constant
  • Sequence of characters
  • Double quotes
  • "Fred", "C17"
  • String Objects
  • string class

22
String Class
  • /-----------------------------------------------
    /
  • / This program prints a greeting
    /
  • / using the string class.
    /
  • include ltiostreamgt
  • include ltstringgt // Required for string class
  • using namespace std

23
String Class
  • int main()
  • // Declare and initialize two string objects.
  • string salutation("Hello"), name("Jane Doe")
  • // Output greeting.
  • cout ltlt salutation ltlt ' '
  • ltlt name ltlt '!' ltlt endl
  • // Exit program.
  • return 0

Hello Jane Doe!
24
Symbolic Constants
  • Const
  • Declared and initialized
  • Cannot be changed within the program
  • const double PI acos(-1.0)
  • const double LightSpeed 2.99792e08

25
C Opeartors
  • Assignment Operator
  • identifier expression
  • 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'
26
Assignment Operator
  • Multiple Assignments
  • x y z 0
  • Type Conversion
  • long double
  • double
  • float
  • long integer
  • integer
  • short integer

27
Arithmetic Operators
  • Unary Operators
  • Positive
  • Negative -
  • Binary Operators
  • Multiplication
  • Division /
  • Modulus
  • Addition
  • Subtraction -

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

29
Expressions
  • Distance x0 v0t at2
  • double distance, x0, v0, a, t
  • distance x0 v0t att
  • Tension ?

30
Increment and Decrement
  • Unary Operators
  • Increment
  • Decrement --
  • Prefix count
  • Postfix count--
  • Examples

31
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

32
Standard Output
  • Standard Output
  • include ltiostreamgt
  • cout ltlt "Hello " ltlt name
  • Stream Manipulators
  • include ltiomanipgt
  • setprecision(n), fixed, scientific
  • setw(n), left, right, setfill(ch)
  • dec, oct, hex
  • endl

33
Standard Output
  • /-----------------------------------------------
    /
  • / Program chapter2_4
    /
  • /
    /
  • / This program computes area of a circle.
    /
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltcmathgt
  • using namespace std

34
Standard Output
  • const double PIacos(-1.0)
  • int main()
  • // Declare and initialize objects.
  • double radius(4.6777), area
  • // Compute area
  • area PIradiusradius

35
Standard Output
  • // Output results
  • cout ltlt setprecision(4)
  • ltlt "The radius of the circle is "
  • ltlt setw(10) ltlt radius ltlt " centimeters"
  • ltlt endl
  • cout ltlt scientific
  • ltlt "The area of the circle is " ltlt
    setw(12)
  • ltlt area ltlt " square centimeters" ltlt endl

36
Standard Input
  • Standard Input
  • include ltiostreamgt
  • cin gtgt var1 gtgt var2 gtgt var3
  • Whitespace used as delimiters
  • blanks, tabs, newlines
  • Values must be compatible with data type of
    objects
  • Stream Manipulators
  • include ltiomanipgt
  • skipws, noskipws

37
Summary
  • Constants and Variables
  • C Operators
  • Standard Input and Output

38
Problem Solving
  • Basic Functions
  • Numerical Technique
  • Linear Interpolation
  • Problem Solving Applied
  • Wind-Tunnel Data Analysis

39
Basic Functions
  • Basic Math Functions
  • include ltcmathgt
  • 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)

40
Basic Functions
  • Trigonometric Functions
  • Angles in radians (180 degrees p 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)

41
Practice
  • velocity sqrt(pow(v0,2) 2a(x - x0))

42
Other Functions
  • Hyperbolic Functions
  • sinh(x), cosh(x), tanh(x)
  • Character Functions
  • include ltcctypegt
  • toupper(ch), tolower(ch)
  • isdigit(ch), isupper(ch), islower(ch)
  • isspace(ch), ispunct(ch)

43
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 lt b lt c

44
Example
  • Data Set
  • Estimate the tempat 2.6s

45
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))
New Coefficient f(b)
Data Point (c, f(c))
New Angle (b)
46
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

47
Problem Solving Applied
  • /-----------------------------------------------
    /
  • / Program chapter2_5
    /
  • /
    /
  • / This program uses linear interpolation to
    /
  • / compute the coefficient of lift for an
    angle./
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltcmathgt
  • using namespace std
  • int main()

48
Problem Solving Applied
  • // Declare objects
  • double a, f_a, b, f_b, c, f_c
  • // Get user input from the keyboard.
  • cout ltlt "Use degrees for all angle measurements.
    \n"
  • cout ltlt "Enter first angle and lift coefficient
    \n"
  • cin gtgt a gtgt f_a
  • cout ltlt "Enter second angle and lift coefficient
    \n"
  • cin gtgt c gtgt f_c
  • cout ltlt "Enter new angle \n"
  • cin gtgt b

49
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 ltlt fixed ltlt setprecision(3)
  • cout ltlt "New lift coefficient " ltlt f_b ltlt endl
  • // Windows friendly exit
  • system("PAUSE")
  • return 0

50
Problem Solving Applied
  • Testing

51
Summary
  • Basic Functions
  • Numerical Technique
  • Linear Interpolation
  • Problem Solving Applied
  • Wind-Tunnel Data Analysis
  • End of Chapter Summary
  • C Statements
  • Style Notes
  • Debugging Notes
Write a Comment
User Comments (0)
About PowerShow.com