40
Training Of Trainers (TOT) program on Embedded Software Engineer Job role Session 2 Developing Software Module 1 www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Develop Embedded Software Module-Session 2

Embed Size (px)

Citation preview

Page 1: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Session 2

Developing Software Module

1www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Page 2: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

AgendaSession 1

History of C Fundamentals of C

Data Types Variables, Constants and Arrays Keywords Decision Control (if-else)

Session 2 Loops Functions (Overview) Arrays

Session 3 Pointers & String

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Page 3: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Loops

3www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Page 4: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntaxfor Loop

expression1 initializes a loop count variable once at start of loop (e.g. i = 0) expression2 is the test condition – the loop will continue while this is true (e.g. i <= 10) expression3 is executed at the end of each iteration – usually to modify the loop count variable (e.g. i++)

for (expression1; expression2; expression3) statement

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 4

Page 5: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax for Loop - Flow Diagram

for (expression1; expression2; expression3) statement

expression 2?

START

END

expression 1

statement

expression 3

TRUE

FALSE

i = 0

i < n

i++

Initialize loop variable

Test loop variable for exit condition

Modify loop variable

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 5

Page 6: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example (Code Fragment)

for Loop

int i;

for (i = 0; i < 5; i++) { printf("Loop iteration #%d\n", i); }

Loop iteration 0 Loop iteration 1 Loop iteration 2 Loop iteration 3 Loop iteration 4

Expected Output:

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 6

Page 7: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

while Loop

If expression is true, statement will be executed and then expression will be re-evaluated to determine whether or not to execute statement again It is possible that statement will never execute if expression is false when it is first evaluated

while (expression) statement

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 7

Page 8: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

while Loop- Flow Diagram

while (expression) statement

expression?

START

END

statementTRUE

FALSE

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 8

Page 9: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example (Code Fragment)

while Loop- Example

int i = 0;

while (i < 5) { printf("Loop iteration #%d\n", i++); }

Loop iteration 0 Loop iteration 1 Loop iteration 2 Loop iteration 3 Loop iteration 4

Expected Output:

Loop counter initialized outside of loop

Loop counter incremented manually

inside loopCondition checked at start of loop iterations

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 9

Page 10: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Note

while LoopThe expression must always be there, unlike with a for loop while is used more often than for when implementing an infinite loop, though it is only a matter of personal taste Frequently used for main loop of program

while (1) { … }

Infinite Loops A w h i l e l o o p w i t h expression = 1 will execute indefinitely (can leave loop via break statement)

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 10

Page 11: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

do-while Loop

statement is executed and then expression is evaluated to determine whether or not to execute statement again statement will always execute at least once, even if the expression is false when the loop starts

do statement while (expression);

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 11

Page 12: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntaxdo-while Loop - Flow Diagram

do statement while (expression);

expression?

START

END

statement

TRUE

FALSE

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 12

Page 13: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example (Code Fragment) do-while Loop - Example

int i = 0;

do { printf("Loop iteration #%d\n", i++); } while (i < 5);

Loop iteration 0 Loop iteration 1 Loop iteration 2 Loop iteration 3 Loop iteration 4

Expected Output:

Loop counter initialized outside of loop

Loop counter incremented manually

inside loop

Condition checked at end of loop iterations

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 13

Page 14: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Functions

14www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Page 15: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Definition

Functions

Functions are self contained program segments designed to perform a specific, well defined task.

All C programs have one or more functions The main() function is required Functions can accept parameters from the code that calls them Functions usually return a single value Functions help to organize a program into logical, manageable segments

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 15

Page 16: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

drink() { ... be_merry(); return; }

be_merry() { ... return; }

eat() { ... return; }

main() { ... eat(); ... drink(); ... }

Program Structure

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 16

Page 17: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax Definitions

type identifier(type1 arg1,…,typen argn) { declarations statements return expression; }

Data type of return expression

NameParameter List

(optional)

Return Value (optional)Header

Body

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 17

Page 18: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

Function Definitions: Return Data Type

A function's type must match the type of data in the return expression

type identifier(type1 arg1,…,typen argn) { declarations statements return expression; }

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 18

Page 19: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

Function Definitions: Return Data Type

A function may have multiple return statements, but only one will be executed and they must all be of the same type

int bigger(int a, int b) { if (a > b) return 1; else return 0; }

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 19

Page 20: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

void identifier(type1 arg1,…,typen argn) { declarations statements return; }

return; may be omitted if nothing is being returned

Function Definitions: Return Data Type

The function type is void if: The return statement has no expression The return statement is not present at all

This is sometimes called a procedure function since nothing is returned

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 20

Page 21: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Function Definitions: Parameters

Parameter list may mix data types int foo(int x, float y, char z)

Parameters of the same type must be declared separately – in other words:

int maximum(int x, y) will not work int maximum(int x, int y) is correct

Exampleint maximum(int x, int y) { return ((x >= y) ? x : y); }

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 21

Page 22: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Function Call Syntax

How to Call / Invoke a Function

No parameters and no return value foo();

No parameters, but with a return value x = foo();

With parameters, but no return value foo(a, b);

With parameters and a return valuex = foo(a, b);

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 22

Page 23: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Function prototypes may be take on two different formats:

An exact copy of the function header:

Like the function header, but without the parameter names – only the types need be present for each parameter:

Function Prototypes

Example – Function Prototype 1

int maximum(int x, int y);

Example – Function Prototype 2

int maximum(int, int);

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 23

Page 24: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example 1

Declaration and Use: Example 1

int a = 5, b = 10, c;

int maximum(int x, int y) { return ((x >= y) ? x : y); }

int main(void) { c = maximum(a, b); printf("The max is %d\n", c) }

Function is declared and defined before it is used in main()

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 24

Page 25: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example 2

Declaration and Use: Example 2

int a = 5, b = 10, c;

int maximum(int x, int y);

int main(void) { c = maximum(a, b); printf("The max is %d\n", c) }

int maximum(int x, int y) { return ((x >= y) ? x : y); }

Function is defined after it is used in main()

Function is declared with prototype before use in main()

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 25

Page 26: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Passing Parameters by Value

Parameters passed to a function are passed by value Values passed to a function are copied into the local parameter variables The original variable that is passed to a function cannot be modified by the function since only a copy of its value was passed

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 26

Page 27: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

ExamplePassing Parameters by Value

The value of a is copied into x. The value of b is copied into y. The function does not change the value of a or b.

int a, b, c;

int foo(int x, int y) { x = x + (++y); return x; }

int main(void) { a = 5; b = 10; c = foo(a, b); }

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 27

Page 28: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Arrays

28www.emtech.in 24x7 Helpline +91 92120 88881 [email protected]

Page 29: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Definition

Arrays

Arrays: May contain any number of elements Elements must be of the same type The index is zero based Array size (number of elements) must be specified at declaration

Arrays are variables that can store many items of the same type. The individual items known as elements, are stored sequentially and are uniquely identified by the array index (sometimes called a subscript).

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 29

Page 30: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

Example

size refers to the number of elements size must be a constant integer

How to Create an Array

int a[10]; // An array that can hold 10 integers

char s[25]; // An array that can hold 25 characters

type arrayName[size];

Arrays are declared much like ordinary variables:

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 30

Page 31: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

Example

How to Initialize an Array at Declaration

int a[5] = {10, 20, 30, 40, 50};

char b[5] = {'a', 'b', 'c', 'd', 'e'};

type arrayName[size] = {item1,…,itemn};

Arrays may be initialized with a list when declared:

The items must all match the type of the array

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 31

Page 32: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

Syntax

How to Use an Array

int i, a[10]; //An array that can hold 10 integers

for(i = 0; i < 10; i++) { a[i] = 0; //Initialize all array elements to 0 } a[4] = 42; //Set fifth element to 42

arrayName[index]

Arrays are accessed like variables, but with an index:

index may be a variable or a constant The first element in the array has an index of 0 C does not provide any bounds checking

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 32

Page 33: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example

Syntax

Creating Multidimensional Arrays

Arrays may have any number of dimensions Three dimensions tend to be the largest used in common practice

int a[10][10]; //10x10 array for 100 integers

float b[10][10][10]; //10x10x10 array for 1000 floats

type arrayName[size1]...[sizen];

Add additional dimensions to an array declaration:

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 33

Page 34: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Syntax

Example

Initializing Multidimensional Arrays at Declaration

char a[3][3] = {{'X', 'O', 'X'}, {'O', 'O', 'X'}, {'X', 'X', 'O'}};

int b[2][2][2] = {{{0, 1},{2, 3}},{{4, 5},{6, 7}}};

type arrayName[size0]…[sizen] = {{item,…,item},

{item,…,item}};

Arrays may be initialized with lists within a list:

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 34

Page 35: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Visualizing 2-Dimensional Arrays

6

a[0][0] = 0; a[0][1] = 1; a[0][2] = 2; a[1][0] = 3; a[1][1] = 4; a[1][2] = 5; a[2][0] = 6; a[2][1] = 7; a[2][2] = 8;

y

x0

0

1

1

0 1

3 4

a[y][x]Row, Column

Column

Row

Row

0R

ow 1

2

5

7 82

2

Row

2

0,0 0,1 0,2

1,0 1,1 1,2

2,0 2,1 2,2

int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 35

Page 36: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

a[0][0][0] = 0; a[0][0][1] = 1; a[0][1][0] = 2; a[0][1][1] = 3; a[1][0][0] = 4; a[1][0][1] = 5; a[1][1][0] = 6; a[1][1][1] = 7;

Visualizing 3-Dimensional Arrays

z

y

x

0

1

0

0

1

1

01

23

45

7

a[z][y][x]Plane, Row, Column

Plane

Column

Row

Plan

e 0

Plan

e 1

0,0,00,0,1

0,1,0

0,1,1

1,1,1

1,0,1

int a[2][2][2] = {{{0, 1},{2, 3}}, {{4, 5},{6, 7}}};

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 36

Page 37: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Example of Array Processing

/************************************************** * Print out 0 to 9 **************************************************/ int main(void) { int i = 0; int a[10] = {0,1,2,3,4,5,6,7,8,9}; while (i < 10) { printf("%d\n", a[i]); i++; }

while (1); }

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 37

Page 38: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

HANDS-ON PRACTICE

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 38

Page 39: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

ConclusionIn this Session, We learn

Loops for loop while loop do-while loop

Functions Declaration Defining Calling

Arrays

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 39

Page 40: Develop Embedded Software Module-Session 2

Training Of Trainers (TOT) program on Embedded Software Engineer Job role

Thank you!

www.emtech.in 24x7 Helpline +91 92120 88881 [email protected] 40