Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

MCH 1003 Programming Notes

Uploaded by

fodemati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

MCH 1003 Programming Notes

Uploaded by

fodemati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

MCH 1003

Introduction to Mechatronics
Fall 2024
Programming - Quick Start
Programming
1. Programming basics
• Style, data types, variables, constants
2. Primitives
• Counters, looping, branching, nesting
3. Side info
• Operations, numbering systems, RGB codes
4. Intermediate
• Functions, I/O, packages, pointers, debugging
5. Algorithms
• Flags, Recursion, swap & sort
Programming - basics
1. Programming style
• Indentation, Commenting, End-of-command operators
• Whitespace & parentheses
2. Data types
• Numbers (integer, float, double, etc.)
• Booleans
• Characters
• Arrays, Strings & Matrices
• Structures
• Casting
3. Variable definitions and constants
Programming - basics
1.Programming style
• Indentation, Commenting, End-of-command operators
• Whitespace & parentheses
2. Data types
• Numbers (integer, float, double, etc.)
• Booleans
• Characters
• Arrays, Strings & Matrices
• Structures
• Casting
3. Variable definitions and constants
Programming - programming style
• Indentation
• Commenting
• End-of-command operators
• Whitespace & parentheses
Programming - indentation

Bad example:

Good example:
Programming - programming style
• Indentation
• Commenting
• End-of-command operators
• Whitespace & parentheses
Programming - commenting
• So that someone else can understand your
code
• Particularly if you did something “clever”
• Do NOT comment every line
• Makes the code less readable
• Only comment where necessary
Programming - programming style
• Indentation
• Commenting
• End-of-command operators
• Whitespace & parentheses
Programming - end-of-line
Programming - programming style
• Indentation
• Commenting
• End-of-command operators
• Whitespace & parentheses
Programming - whitespace & ()
Programming - basics
1. Programming style
• Indentation, Commenting, End-of-command operators
• Whitespace & parentheses
2.Data types
• Numbers (integer, float, double, etc.)
• Booleans
• Characters
• Arrays, Strings & Matrices
• Structures
• Casting
3. Variable definitions and constants
Programming - data types
• Numbers
• Integer, float, double
• Booleans
• Characters
• Arrays
• Strings
• Matrices
• Structures
• Casting
Programming - basics
1. Programming style
• Indentation, Commenting, End-of-command operators
• Whitespace & parentheses
2. Data types
• Numbers (integer, float, double, etc.)
• Booleans
• Characters
• Arrays, Strings & Matrices
• Structures
• Casting
3.Variable definitions and constants
Programming - definitions
• Variables
• Name, type, value, scope, location
• Ex: int class_size = 39;
• Scope: The function it is defined in
(unless made global)
• Location: somewhere in the memory
• Constants
• #define
• const type identifier_name = value;
Programming
1. Programming basics
• Style, data types, variables, constants
2.Primitives
• Counters, looping, branching, nesting
3. Side info
• Operations, numbering systems, RGB codes
4. Intermediate
• Functions, I/O, packages, pointers, debugging
5. Algorithms
• Flags, Recursion, swap & sort
Programming - primitives
1. Counters
2. Looping
• For
• While
• Do-while
3. Branching
• If/else
• Switch case
4. Conditions
5. Nesting
Programming - primitives
1.Counters
2. Looping
• For
• While
• Do-while
3. Branching
• If/else
• Switch case
4. Conditions
5. Nesting
Programming - counters
for (int i = 0; i<10; i++) {
print(i);
}

int i = 0;
while (i<10) {
print(i);
i++;
}
Programming - primitives
1. Counters
2.Looping
• For
• While
• Do-while
3. Branching
• If/else
• Switch case
4. Conditions
5. Nesting
Programming - looping
• For

• While
• Do-while
• do { statements; } while (condition);
• Happens at least once
Programming - primitives
1. Counters
2. Looping
• For
• While
• Do-while
3.Branching
• If/else
• Switch case
4. Conditions
5. Nesting
Programming - branching
• If/else

Switch case
Programming - primitives
1. Counters
2. Looping
• For
• While
• Do-while
3. Branching
• If/else
• Switch case
4.Conditions
5. Nesting
Programming - conditions
• ==
• !=
•<
•>
• <=
• >=
• &&
• ||
• Parentheses make these legible
Programming - primitives
1. Counters
2. Looping
• For
• While
• Do-while
3. Branching
• If/else
• Switch case
4. Conditions
5.Nesting
Programming - nesting
Programming
1. Programming basics
• Style, data types, variables, constants
2. Primitives
• Counters, looping, branching, nesting
3.Side info
• Operations, numbering systems, RGB codes
4. Intermediate
• Functions, I/O, packages, pointers, debugging
5. Algorithms
• Flags, Recursion, swap & sort
Programming - side info
1. Operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
2. Numbering systems
3. RGB codes
4. ASCII codes
Programming - side info
1.Operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
2. Numbering systems
3. RGB codes
4. ASCII codes
Programming - operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
Programming - math operators
• +,-,*,/
• Power: a = pow(x,y) (a equals x to the power y)
• Modulo: a = x % y (x divide by y leaves a as remainder)
• Precedence
• Use parentheses!
• MD before AS
• Left to Right when equal
• Math libraries (math.h)
• Sin, cos, max, min, etc.
• Be careful about integer vs. floating point math!
Programming - operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
Programming - logic operators
• AND: &&
• OR: ||
• NOT: !
Programming - operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
Programming - bitwise operators
• Just like logic operators - only multiples
• 0101 OR 0011 EQUALS 0111
• 0101 AND 0011 EQUALS 0001
• NOT 0111 EQUALS 1000
• 0101 XOR 0011 EQUALS 0110
• Shifts
• Arithmetic, logical, circular
• R-shift: 0111-0101 —> ?011-1010
• L-shift: 0111-0101 —> 1110-101?
Programming - operations
• Math operators
• Logic operators
• Bitwise operators
• String operators
Programming - string operators
• Compare
• Concatenate
• Scan
• Copy
• Get length
Programming - side info
1. Operations
• Math operators
• Boolean operators
• Bitwise operators
• Logic operators
• String operators
2.Numbering systems
3. RGB codes
4. ASCII codes
Programming - numbering systems
• Binary —> 010110011100111001
• Octal —> 53672334045
• Decimal —> 97465894
• Hexadecimal (Hex) —> 78A98B0F0E7DC

• Illegal bases
Programming - side info
1. Operations
• Math operators
• Boolean operators
• Bitwise operators
• Logic operators
• String operators
2. Numbering systems
3.RGB codes
4. ASCII codes
Programming - RGB codes
• Every color has three components:
• Red
• Green
• Blue
• Each component:
• 0-255 (Hex)
• 0 => 00
• 255 => FF
Programming - side info
1. Operations
• Math operators
• Boolean operators
• Bitwise operators
• Logic operators
• String operators
2. Numbering systems
3. RGB codes
4.ASCII codes
Programming - ASCII codes
Programming
1. Programming basics
• Style, data types, variables, constants
2. Primitives
• Counters, looping, branching, nesting
3. Side info
• Operations, numbering systems, RGB codes
4.Intermediate
• Functions, I/O, packages, pointers, debugging
5. Algorithms
• Flags, Recursion, swap & sort
Programming - intermediate
1. Functions
2. File & command line I/O
3. Pre-defined packages
4. Pointers & references
5. Debugging
Programming - intermediate
1.Functions
2. File & command line I/O
3. Pre-defined packages
4. Pointers & references
5. Debugging
Programming - functions
• Bits of code separated from main set of commands
• Why?
• Sub-stepping (breaking into pieces)
• Using same code over and over again
• Good housekeeping of variable space
• Easier testing
• 3 parts to each function
• Declaration
• Definition
• Usage (calling)
• NO NESTING IN FUNCTIONS!
Programming - intermediate
1. Functions
2.File & command line I/O
3. Pre-defined packages
4. Pointers & references
5. Debugging
Programming - I/O
• Open file / port / command line
• Define access mode
• Read
• Write
• Append
• Read from / write to file / port / command
line
• character / string / numeral / binary / ASCII
• Close file / port / command line
Programming - intermediate
1. Functions
2. File & command line I/O
3.Pre-defined packages
4. Pointers & references
5. Debugging
Programming - packages
• Header files / libraries
• Collection of functions that you can utilize
• stdio.h —> file and command line I/O fxns
• math.h —> mathematical fxns
• string.h —> string handling fxns
• time.h —> date & time utilities
• stdlib.h —> random numbers, algorithms,
string conversions, memory management
Programming - intermediate
1. Functions
2. File & command line I/O
3. Pre-defined packages
4.Pointers & references
5. Debugging
Programming - pointers
• Each variable is stored in a certain
location (address) on memory
• Referring to the variable only reads the
value
• Passing into a function does not change
value
• Passing the address of a variable into a
function enables changing the value
Programming - pointers
main() { main() {
for (int i = 1; i ≤ 9; i++) { for (int i = 1; i ≤ 9; i++) {
if (i%3) changeCounter(i); if (i%3) changeCounter(&i);
print(i); print(i);
} }
} }
changeCounter(int j) { changeCounter(int* j) {
j = j + 1; *j = *j + 1;
} }

Result: 1 2 3 4 5 6 7 8 9 Result: 2 3 5 6 8 9
Programming - intermediate
1. Functions
2. File & command line I/O
3. Pre-defined packages
4. Pointers & references
5.Debugging
Programming - debugging
• Finding where you made a mistake
• NOT ALL MISTAKES CAN BE FOUND BY
DEBUGGING!
• Usually one mistake leads to another
• Start by fixing the first mistake and running
again!
• You can see variables being defined, functions
being utilized, stored data changing, calculations,
branches and loops, etc.
• Breakpoints, stepping (in), stepping out & over
Programming
1. Programming basics
• Style, data types, variables, constants
2. Primitives
• Counters, looping, branching, nesting
3. Side info
• Operations, numbering systems, RGB codes
4. Intermediate
• Functions, I/O, packages, pointers, debugging
5.Algorithms
• Flags, Recursion, swap & sort
Programming - algorithms
1. Flags
2. Recursion
3. Swap & sort
Programming - algorithms
1.Flags
2. Recursion
3. Swap & sort
Programming - flags
• Boolean variable
• Used to track changes in situations
• Wait until a counter is 12
• Alert when an LED turns ON
• Wake up when a sensor senses light
• Warn when the program finishes
Programming - flags
Ex: There are 2 buttons. Wait until buttons are pushed simultaneously.
boolean button1_pressed = false;
boolean button2_pressed = false;
boolean both_pressed = false;
void setup() {
pinMode(3, INPUT_PULLUP); // Define pin 3 as input
pinMode(5, INPUT_PULLUP); // Define pin 5 as input
}
void loop() {
if (digitalRead(3) == LOW) button1_pressed = true;
else button1_pressed = false;
if (digitalRead(5) == LOW) button2_pressed = true;
else button2_pressed = false;
both_pressed = button1_pressed && button2_pressed;
}
Programming - algorithms
1. Flags
2.Recursion
3. Swap & sort
Programming - recursion
• Calling a function within itself
• Defining a bigger problem in terms of a
simpler version of itself
• Example (‘finish class’ function):
• Class finished?
• If yes, stop waiting (exit function)
• Wait another minute
• Call ‘finish class’
Programming - recursion
Example: Factorial
x! = x * (x-1) * (x-2) * … * 4 * 3 * 2 * 1
= x * [(x-1) * (x-2) * … * 4 * 3 * 2 * 1]
= x * (x-1)!

function result = factorial(x) {


if (x == 0) { result = 1; }
else { result = x * factorial(x-1); }
}
Programming - algorithms
1. Flags
2. Recursion
3.Swap & sort
Programming - swap & sort
• Usually, need to sort a list of variables
based on a property
• Ex: ‘Sort grades from highest to lowest’
• Many sort algorithms exist
• Bubble sort & selection sort
• All algorithms include swapping
Programming - swap & sort
main () {
int grades[] = {65, 92, 88, 69, 73};
grades = sort(grades);
}
int * sort(int grades[]) {
int N = sizeof(grades) / sizeof(grades[0]); // length of grades[]
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
if (grades[i]<grades[j]) swap(grades, i, j);
}
} swap (int grades[], int i, int j) {
return grades; int temp = grades[i];
} grades[i] = grades[j];
grades[j] = temp;
}

You might also like