Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
+
Internal Memory
Memory Location
totalSales
Total_Sal3s
Total Sales
total.Sales
4thQtrSales
totalSale$
T0taLsales
C++ Keywords
auto
const
double
float
int
short
struct
endl
Data Types
short
int
float
double
bool
char
Examples:
float length;
Int width;
string lastname;
string age;
// Displays 3
String Literals
"Hello"
Sequence
Decision
Repetition
Case
1. Decision Structure
Char vs string literal constants
if (Boolean expression)
action/statement if true;
string name;
cin >> name;
if (expression)
{
statement1;
Making Decisions /
Conditional Statements
Four Program Structures
statement2;
if (Boolean expression)
action/statement if true;
else
Example if Statements
action/statement if false;
if (number == 7)
if (expression)
if Statement Notes
{
statement set1;
}
else
{
statement set2;
}
How the if/else Works
if/else if Format
if (expression)
statement set 1;
else if (expression)
statement set 2;
else if (expression)
statement set n;
else;
Using a Trailing else
Using = Instead of ==
if (genderCode == M')
Using = instead of ==
else
switch (expression)
{
statement set ;
}
switch Statement Requirements
1) expression must be a char or an
integer variable or an expression that
evaluates to an integer value
display menu
Arrays
1) expression is evaluated
2) The value of expression is compared
against exp1 through expn.
3) If expression matches value expi,
the program branches to the
statement(s) following expi and
continues to the end of the switch
4) If no matching value is found, the
program branches to the statement
after default:
The break Statement
for Loop
Array Initialization
General Syntax:
datatype arrayname [index] ;
Example:
One Dimensional
Array Initialization
One Dimensional
Array Initialization
Sample Program
(without array)
int score[3];
score[0] = 90;
score[1] = 95;
score[2] = 100;
One-Array Initialization
Source Code
int number[4][3];
[4]=Number of rows
[3]=Number of columns
#include <iomanip>
LEVELS OF
DATA ORGANIZATION
What is Data?
setiosflags includes:
ios::fixed
ios::showpoint
setprecision
setw
DATA vs INFORMATION
ios::fixed
ios::showpoint
Some Manipulators
Used in the Program
Bit
Byte
Field
Record
File
Database
Bit
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
setprecision
setw
Byte (8 bits)
It is the smallest unit of data.
It is equal to one character (Ex. A-Z 09 and any symbol).
A character is the most basic element
of data that can be observed and
manipulated.
Operators and
Mathematical
Expressions
Mathematical Expressions
Database
2
height
Telephone book
Student data
Music
Fingerprint database
Dictionaries
Customer data
Hospital/patient data
Inventory
Levels of Data Organization
a+b/c
Using Mathematical Expressions
Examples:
area = 2 * PI * radius;
cout << "border is: " <<
(2*(l+w));
Assignment Statement
y = 6;
z = 4;
x = x + y;
y = y + 4;
z = 5 + x + z;
b=5+4*3
Operators
c = 50 / 10 * 2
d = 60 - 20 + 4 / 2 * 10
Division (/) Operator
Types of Operators:
1. Arithmetic
2. Relational
3. Logical
cout << 2 / 4;
Arithmetic Operators
// displays 2
// displays 0
// displays 0.5
Precedence of Operators
// displays 1
Algebraic Expressions
Associativity of Operators
w;
(2 + 2) * 2 2 = 6
2 + 2 * (2 2) = 2
(2 + 2) * (2 2) = 0
is written as m = (y2-y1)/
(x2-x1);
x == 10
x != 8
x == 8
Logical Operators
Relational Operators
Relational Expressions
Advantages of a RDBMS
Logical Precedence
Highest !
&&
Lowest
||
Example:
Properties of a Relation/Table
More on Precedence
Example:
8 < 2 + 7 || 5 == 6
is true
Relational Database
Candidate Key
Alternate Key
Primary Key
Foreign Key
Candidate Keys
- A minimal set of attributes in a table that
uniquely identifies a record. When there is
more than one attribute in the candidate key,
it is called composite key.
Alternate Keys
- Any candidate key that is not selected to
be a primary key can be an alternate key.
Primary Key
- A candidate key that is chosen to
represent a record uniquely. That is, a table
may consist of many candidate keys, but
ONLY ONE can be selected as a primary key.
Note that a primary key can have more than
one attribute.
One-to-one
Foreign Keys
- is a column or group of columns in a
relational database table that provides a link
between data in two tables.
Table Relationship
A Relationship - is how you tell the
program that a piece of information means
the same thing in more than one table.
Table relationships can be created between
two tables as long as they have a common
attribute.
Relationships are created using a Primary
Key from one table and linking it to a related
field in another table (now called a Foreign
Key).
2. One-to-many
One-to-many
3. Many-to-many
Delete a Relationship
Repetition Structure
Repeating Program Instructions
Performing Loops
x < y?
YES
YES Process A
c) do while
Initialization;
while (expression)
{
statement(s);
Update;
expression is evaluated
// boolean condition
// update
// end while
Produces Output:
5 4 3 2 1 0
Loop Control Variable
x = 5;
while (x > 0)
cout << x;
Sentinel
Sentinel Example
int points;
Sentinel
int points;
Sentinel
Example
cout << "Enter
points earned "
<< "(or -1 to quit): ";
cin
>> points;
No
semicolon
{
cout << number << endl;
number ++;
}
int number = 1;
An accumulator - is a numeric
variable used for accumulating
(adding together) multiple values
Example:
Repetition Structure
(continued)
Counter-Controlled Loops
Solution to for
Square Loop
cout<< \t
Format:
for( initialization; expression ; update )
{
Review of
Pretest vs. Posttest Loops
1 or more statements;
}
Format:
do
{
1 or more statements;
} while (expression);
Answer
Nested Loop