Professional Documents
Culture Documents
• Example Algorithms
• Flowcharts and C Code
• Flexibility
– For Example
• Any For Loop can be expressed as a While Loop
• Any Case Switch structure can be replaced by a
series of IF/ELSE structures
Rule 1
Rule 3
Rule 3
Rule 2
sum = 0
i=i+1
Calculate the Sum of
the Numbers
Calculate the
Average of the 6 sum = sum + ni
Numbers
Divide the Sum by the
Number of Entries Does No
i = 6?
End
Yes
End
Average = sum / 6
sum = 0
i=0
i=i+1
sum = sum + ni
Does No
i = 6?
Yes
Average = sum / 6
End
ECET 209 Purdue University 19
Relational Operators
( number_of_dogs > 3 )
( value != 0 )
( counter > 10 )
• For example…
Problem:
Light the upper nibble of the LEDs
when the counter is above 10.
What is
this??
Is the counter Yes
variable greater
than 10 ?
if
Is the counter Yes
variable greater
than 10 ?
What??
if
Is the counter Yes
variable greater
than 10 ?
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{
Turn on the upper
No four LEDs
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{
Turn on the upper
No four LEDs
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{ PORTC = 0xF0;
Turn on the upper
No four LEDs
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{ PORTC = 0xF0;
Turn on the upper
No four LEDs
if ( counter > 10 )
Is the counter Yes
variable greater
than 10 ?
{ PORTC = ~0xF0;
Turn on the upper
No four LEDs
if ( counter > 10 )
{
PORTC = ~ 0xF0; // turn on LEDs
}
Problem:
Light the upper nibble of the LEDs
when the counter is above 10. When the
counter is equal to or below 10, light
only the least significant bit of the
LEDs.
else
No Is the counter Yes
variable greater
{ than 10 ?
else
No Is the counter Yes
variable greater
{ than 10 ?
}
PORTC = ~0x01;
ECET 209 Purdue University 40
Transformation to C Code
} }
PORTC = ~0x01; PORTC = ~0xF0;
ECET 209 Purdue University 41
Updated C Code
if ( counter > 10 )
{
PORTC = ~ 0xF0; // turn on LEDs
}
else
{
PORTC = ~ 0x01; // turn on LSB
}209
ECET Purdue University 42
Updated C Code
if ( counter > 10 )
{
PORTC = ~ 0xF0; // turn on LEDs
}
else
{
PORTC = ~ 0x01; // turn on LSB
}209
ECET Purdue University 43
Updated C Code
if ( counter > 10 )
{
PORTC = ~ 0xF0; // turn on LEDs
}
else
{
PORTC = ~ 0x01; // turn on LSB
}209
ECET Purdue University 44