Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engr 0012 (04-1) LecNotes 18-01. Engr 0012 (04-1) LecNotes 18-02 Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.

Similar presentations


Presentation on theme: "Engr 0012 (04-1) LecNotes 18-01. Engr 0012 (04-1) LecNotes 18-02 Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real."— Presentation transcript:

1 Engr 0012 (04-1) LecNotes 18-01

2 Engr 0012 (04-1) LecNotes 18-02 Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real equivalent script - program control algorithm main - program control function/algorithm functions - perform specific computation or task

3 Engr 0012 (04-1) LecNotes 18-03 Contrasting variables MATLABC language Two variable types You must decide type based upon need and use int (+/-32,000) log int (+/- 2 billion) float (8 digits ~ 10 +/-37 ) double (16 digits ~ 10 +/-307) char (ASCII character) Use meaningful names double precision, complex string (text)

4 Engr 0012 (04-1) LecNotes 18-04 I/O Commands MATLABC language % comment delimiter // comment delimiter input get info from keyboard scanf get info from keyboard fprintf display info to screen printf display info to screen load read file into memory no equivalent command - must define a function to do this

5 Engr 0012 (04-1) LecNotes 18-05 decision (branching) structures - if/elseif MATLABC language if ( ) action1; action2; elseif ( ) action3; else action4; action5; end %if if ( ) { action1; action2; } else if ( ) { action3; } else { action4; action5; } suppresses display ends statement end terminates structure { } used to enclose actions that belong together

6 Engr 0012 (04-1) LecNotes 18-06 decision (branching) structures - switch/case MATLABC language switch (value) case {1, 3} action1; action2; case {4} action3; otherwise action4; action5; end %switch/case switch (value) { // begin switch case 1: case 3: action1; action2; break; case 4: action3; break; default: action4; action5; } //end switch end terminates structure { } used to enclose actions that belong together

7 % initialize loop sum sum = 0; % add first N numbers for (k=1:1:N) sum = sum+k; end % for // initialize loop sum sum = 0; // add first N numbers for (k=1;i<N+1;k=k+1) { sum = sum+k; } //end for Engr 0012 (04-1) LecNotes 18-07 repetition (looping) structures - for loop MATLABC language end terminates structure { } used to enclose actions that belong together

8 Engr 0012 (04-1) LecNotes 18-08 repetition (looping) structures - while loop MATLABC language % initialize loop sum sum = 0; k = 1; % add first N numbers while (k<N+1) sum = sum+k; k = k+1; end % while // initialize loop sum sum = 0; k = 1; // add first N numbers while (k<N+1) { sum = sum+k; k = k+1; } //end while end terminates structure { } used to enclose actions that belong together

9 Engr 0012 (04-1) LecNotes 18-09 repetition (looping) structures - do..while loop MATLABC language no do..while // initialize loop sum sum = 0; k = 0; // add first N numbers do { k = k+1; sum = sum+k; } while (k<N)

10 Engr 0012 (04-1) LecNotes 18-10 sample C-program /* Your Name(s) Engineering 12, Fall Term 2002 Class Activity 18-1 */ // include libraries #include // prototypes void displayheader( void ); //******************************************************** main() declaration section preprocessor commands

11 Engr 0012 (04-1) LecNotes 18-11 sample C-program //******************************************************** main() { // begin main // variable declarations int first; double second; char letter; displayheader(); // algorithm printf( "\nPlease enter an integer ==> " ); scanf( "%d", &first ); printf( "\nPlease enter a real number ==> " ); scanf( "%lf", &second ); printf( "\nPlease enter a character ==> " ); fflush( stdin ); scanf( "%c", &letter ); fflush( stdin ); printf( "\n\nYou entered the integer %d " " \n the real number %lf " " \n and the character %c \n\n", first, second, letter ); return(0); } // end main //******************************************************** void displayheader( void ) use easily seen marker to separate functions function begin/end enclosed by { } variable declaration section for main command to display header placeholders - correspond to variable type & address of operator

12 Engr 0012 (04-1) LecNotes 18-12 sample C-program //******************************************************** void displayheader( void ) /* purpose: display header info to screen goal state: header info on screen */ { // begin displayheader printf( "Your name(s)" ); printf( "\nEngineering 12, Fall Term 2003" ); printf( "\nClass Activity 18-1 \n" ); printf( "\n\nThis program performs simple I/O operations" ); printf( "\n" ); } // end displayheader returns nothing needs nothing


Download ppt "Engr 0012 (04-1) LecNotes 18-01. Engr 0012 (04-1) LecNotes 18-02 Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real."

Similar presentations


Ads by Google