CS 1400 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS 1400

Description:

All variables must be established or declared prior to use usually at the ... Input and output statements can be cascaded; cin height width depth; ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 15
Provided by: scottc50
Category:
Tags: cascaded

less

Transcript and Presenter's Notes

Title: CS 1400


1
CS 1400
  • 1 Sept 2006

2
Your first C program
  • Boilerplate
  • // Cannon, demo program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • // program goes here
  • return 0

3
Output
  • Pseudocode
  • Print Enter a number
  • C
  • cout ltlt Enter a number
  • Pseudocode
  • Print square
  • C
  • cout ltlt square

4
Input
  • Pseudocode
  • Get value from user
  • C
  • cin gtgt value

5
Assignments
  • Pseudocode
  • Square n n
  • C
  • square n n
  • Pseudocode
  • Pay hours 40 (hours-40) Overtime_pay
  • C
  • pay hours 40 (hours-40) overtime_pay

6
Declarations
  • All variables must be established or declared
    prior to use usually at the top of the program
  • Integers are declared using int. For example
  • int value
  • int width, height, depth
  • A variable name must begin with a letter,
    followed by letters, digits, or underscores.

7
Pseudocode Example
  • Write a program to calculate the volume of a
    box, using dimensions provided by the user.
  • Print Enter height, width, and depth of box
  • Get height from user
  • Get width from user
  • Get depth from user
  • volume height width depth
  • Print Volume is
  • Print volume

8
C boilerplate
  • // Cannon, demo program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • return 0

9
Adding C instructions
  • // Cannon, demo program
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int height, width, depth, volume
  • cout ltlt Enter height, width, and depth
  • cin gtgt height gtgt width gtgt depth
  • volume height width depth
  • cout ltlt Volume is ltlt volume
  • return 0

10
C is case sensitive!
  • Use lower case for simplicity
  • Upper case or underscore may be used to separate
    multi-word variable names
  • int phone_number // easiest to read
  • int phoneNumber // easier to read
  • int phonenumber // not as easy to read

11
Extended syntax
  • Input and output statements can be cascaded
  • cin gtgt height gtgt width gtgt depth
  • cout ltlt the value of width is ltlt width
  • Declarations can be a list
  • int height, width, depth, volume
  • Output messages can include control characters
    such as \n (new line) and \a (beep)
  • cout ltlt This appears on line 1 \n This is on
    line 2
  • Output messages may output endl to force a new
    line
  • cout ltlt this is line1 ltlt endl ltlt This is line
    2

12
Chapter 2 issues --
  • Key words Table 2.4 (4th edition)
  • Gloss over (for now)
  • Section 2.3
  • Section 2.7
  • Section 2.9
  • Section 2.10
  • Section 2.16

13
Integer variables
  • Declaring variables as type int
  • Only allows them to hold an integer ( or -)
  • Range -2,147,483,648 to 2,147,483,647
  • Variations
  • unsigned int
  • long
  • short etcetera

14
Floating point variables
  • Declaring variables to be type float
  • Allows them to hold an approximate floating-point
    value (precision to 6 digits)
  • Range /- 3.4 x 10-38 to /- 3.4 x 1038
  • Constants may be expressed in floating-point
    notation or E-notation
  • E-notation is like scientific notation
  • 3.14 x 105 is the same as 3.14E5
Write a Comment
User Comments (0)
About PowerShow.com