Download presentation
Presentation is loading. Please wait.
Published byVanessa Terry Modified over 9 years ago
1
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15
2
Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return 0; }
3
Output… Pseudocode Print to screen “Enter a number: “ C++ cout << “Enter a number: “; Pseudocode Print value of variable square C++ cout << square;
4
Input… Pseudocode Get number from user and store in variable value C++ cin >> 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 using namespace std; int main() { return 0; }
9
Adding C++ instructions… // Cannon, demo program #include using namespace std; int main() {int height, width, depth, volume; cout << “Enter height, width, and depth: “; cin >> height >> width >> depth; volume = height * width * depth; cout << “Volume is: “ << 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;// easy to read int phonenumber;// not as easy to read
11
Extended syntax… Input and output statements can be cascaded; cin >> height >> width >> depth; cout << “the value of width is: “ << 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 << “This appears on line 1 \n This is on line 2”; Output messages may output endl to force a new line cout << “this is line1 “ << endl << “This is line 2”;
12
Assignment statements A formula may be used to store a calculation into one variable volume = height * width * height; Operator symbols –Addition and subtraction: +, - –Division and multiplication:/, * –Modulus (remainder):% Precedence: *, /, % are higher than +, -
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 10 38 –Constants may be expressed in floating-point notation or E-notation –E-notation is like scientific notation 3.14 x 10 5 is the same as 3.14E5
15
Summary of variable types… int –exact whole numbers –limited range float –approximate decimal numbers –vast range
16
Literals vs. numbers A literal or string is enclosed in quotes cout << “hello” << “my name is Fred”; Any letter or character may be part of a literal Literals are for humans to read – you cannot do math with a literal or store a literal in a numeric variable! int x; x = “5”;// ouch!!
17
Keywords Certain words are reserved in C++ and cannot be used for variable names Fig 2.4
18
Truncation An int variable can only hold integers int whole; float decimal; decimal = 3.14159; whole = 25; Integer truncation will occur on; –assignment:whole = decimal; –division:decimal = 1 / 3; An integer divided by an integer is always an integer!!
19
Scope A variable may only be used after it is declared: {// age may not be used here … int age; …// age may be used here }
20
Examples… Write a program to calculate the area of a circle. Write a program to calculate the total amount of a sale by adding 7.5% sales tax Write a program to convert Fahrenheit temperatures to Centigrade. Write a program to calculate the sale amount for a company that sells three types of bags (small, medium, and large). They sell for $2.50, $3.25, and $4.00 respectively. The user should enter three values for the count of each bag type. formulas: A = R 2 total = (1.075)sale C = (9/5)(F-32)
21
Tracing tables… A traciing table shows the values of variables after each program statement Stmtbag1bag2bag3costsale 1 2 3 4 …
22
Summary Topics covered –boilerplate –input and output –assignment statements –declarations for int and float –case sensitivity –literals –keywords –truncation –scope
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.