chapter 2 primitive data types and operations
DESCRIPTION
Chapter 2 Primitive Data Types and Operations. Primitive Data types: Integer data types: int byte short long Floating-point types: double float Character type: char. Identifiers Variables must start with a letter or underscore consists of letters, digits & underscores - PowerPoint PPT PresentationTRANSCRIPT
Chapter 2Primitive Data Types and
Operations
• Primitive Data types:– Integer data types:
•int•byte•short•long
- Floating-point types:•double•float
- Character type: •char
• Identifiers– Variables
• must start with a letter or underscore
• consists of letters, digits & underscores
• cannot be a reserved word
• Cannot be true, false, or null
firstScore score1 score
3scores score-1 myScore
• Capitalize “middle” words
• Usually start variables with lowercase, classes with uppercase
• C++ is case sensitive!!!!
Score score
Declaring Variables – C++ is Strongly Typed
Syntax:type identifier1, identifier2, …;
int num1;
float num2, num3;
float num4;
Assignment Statement
variable = expression;
We should initialize our variables.
int num1 = 0;
double num2 = 0.0, num3 = 0.0;
Numeric Operators
Arithmetic + - * / %
Consider m * x + b• Is it equivalent to
(m * x) + b• Is it equivalent to
m * (x + b)
Expression evaluation rules
Use operator precedence!
Standard precedence order
( ) Evaluated first, if nested then the innermost pair is evaluated,
and so on
* / % Evaluated second, from left
to right
+ - Evaluated third, from left to right
-1 * 2 + 3 / 4 - 5
50 / 3 + 22 % 4
(1 + 3) * (2 + (4 * 6) * 3) / 2 + 2
-2 / 10 * (15 + 2)
-2.0 / 10.0 * (15.0 + 2.0)
• Trace the program fragment belowint total=0, newTotal=0;
total = 0;total = 5;newTotal = 10;total = newTotal + 8;newTotal = total + 3;newTotal = newTotal + 1;total = total + newTotal;
cout << newTotal << endl;cout << total << endl;
• Write C++ expressions from arithmetic expressions
celsius = (fahrenheit - 32) * 5 / 9;
#include <iostream>using namespace std;
int main(){ // receive the amount cout << "Enter an amount in double, for example 11.56: "; double amount; cin >> amount;
int remainingAmount = static_cast<int>(amount * 100); // find the number of dollars int numberOfOneDollars= remainingAmount / 100; remainingAmount = remainingAmount % 100; // find the number of quarters int numberOfQuarters = remainingAmount/25; remainingAmount = remainingAmount % 25; // find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; // find the number of nickels in the remaining amount int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; // find the number of pennies int numberOfPennies = remainingAmount;
// Display the results cout << "Your amount " << amount << " constists of \n" << "\t" << numberOfOneDollars << " dollars \n" << "\t" << numberOfQuarters << " quarters \n" << "\t" << numberOfDimes << " dimes \n“ << "\t" << numberOfNickels << " nickels \n" << "\t" << numberOfPennies << " pennies\n"; return 0;}
• A literal is a constant value that appears directly in a program.int number = 34;double distance = 12.35;
• Character Data Type: represent a single character char firstLetter = 'A';//initialize a character data type named “firstLetter” with value ‘A’
• A character literal is enclosed in single quotation marks. Which of the following is a character literal?'a' 'A' ' ' '$' '7'
'ab' ' a ' “a”
•A string literal must be enclosed in double quotation marks.
Assignment Compatibilities
int -> float -> double
Be careful when doing assignments!int sum;double average;
sum = 50;
sum = 10.5;
average = 50;
average = 50 / 4;
average = 50.4;
Type Casting: temporarily changing a value’s typeSome type casting is automatic
int num1;double num2;num1 = 7;num2 = num1;
However, we have to be careful:double distance;distance = 9.0;int points;points = distance;
We must cast the value:points = static_cast<int>distance;Assigns the integer part of distance to points.This DOES NOT CHANGE distance!
Consider the code:char letter = 7;letter = ‘A’ + 5;
• ASCII code table: computers use a unique numeric code to represent each character
• Each printable character (letter, number, punctuation mark, space, and tab) is assigned a different integer code
• See Appendix B on page 654 for the ASCII character codes
Integer Ascii Value Character32 Space33 !48 049 150 265 A66 B97 a98 b
• Simple Output must include <iostream>– cout – << is the extraction operator
cout << “One” << endl;
cout << “Two” << endl;
cout << “Three” << endl;
cout << “One”;
cout << “Two”;
cout << “Three”;
• How could we keep the words separated?
• We can use the extraction operator to output more than one value.
cout << num << “ Cokes”<< endl;
• Simple Input
– cin
– >> is the insertion operator
int num1, num2, num3
cin >> num1;
cin >> num2;
cin >> num1 >> num2;
Remember the operator points in the direction of data flow!
Formatting Output
We’d like to be able to format floats (and doubles) to limit the number of digits after the decimal point.
It’s EASY!
Suppose you have the following:
double amount = 197.55
double tax = amount * .07
cout << “the tax is $”
<< static_cast<int>(tax * 100)/100.0;
This will drop the decimal amounts beyond the two decimals that we want. Change the multiplier and divisor to change the number of decimal places required.
• Naming Conventions: (1) variable names should begin with a lowercase letter, capitalize the first letter of subsequent words; (2) capitalize the first letter of each word in a class name.
• Programming Errors:
– We have discussed compiler errors, which are usually syntax errors or omissions. The compiler discovers these errors for us.
– When our program compiles successfully, it means that it is free of syntax errors. It does not mean that the program will necessarily run correctly!
– Programs which do not run correctly contain runtime errors (which cause the program to terminate abnormally) and/or logic errors (which cause the program to produce incorrect results). We discover these errors by testing the program.
• Seven steps to good programming habits– Analyze the problem
– Develop the algorithm (design!)
– Write code for the program (implement)
– Compile the code
– Run the program
– Test the results
– Document the program!
READ CHAPTER 2 – to really understand it all, punch in all sample programs.
• Chapter 2 review questions
• 2.1 - 2.5
• 2.9 - 2.10
• 2.12 – 2.13
• 2.27