Exam 1 Results 30 Percent of Scores 25 20 Mean Median Mode St Dev Minimum Maximum 62 63 72 14 24 90 15 10 5 0 <40 40-49 50-59 60-69 Score Range Engr 0012 (04-1) LecNotes 18-01 70-79 80-90 Contrasting MATLAB with C MATLAB C language Workspace - interactive computation No real equivalent script - program control algorithm main - program control function/algorithm functions - perform specific computation or task functions - perform specific computation or task Engr 0012 (04-1) LecNotes 18-02 Contrasting variables MATLAB C language Two variable types You must decide type based upon need and use double precision, complex string (text) Use meaningful names Engr 0012 (04-1) LecNotes 18-03 int (+/-32,000) log int (+/- 2 billion) float (8 digits ~ 10+/-37) double (16 digits ~ 10+/-307) char (ASCII character) Use meaningful names I/O Commands MATLAB C 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 Engr 0012 (04-1) LecNotes 18-04 decision (branching) structures - if/elseif MATLAB if ( ) action1; action2; elseif ( ) action3; else action4; action5; end %if end terminates structure Engr 0012 (04-1) LecNotes 18-05 C language suppresses display if ( { ) action1; action2; } else if ( ) { action3; } else { { } used to enclose action4; actions that belong action5; together } ends statement decision (branching) structures - switch/case MATLAB C 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 Engr 0012 (04-1) LecNotes 18-06 repetition (looping) structures - for loop MATLAB C language % initialize loop sum sum = 0; // initialize loop sum sum = 0; % add first N numbers for (k=1:1:N) sum = sum+k; end % for // add first N numbers for (k=1;i<N+1;k=k+1) { sum = sum+k; } //end for end terminates structure { } used to enclose actions that belong together Engr 0012 (04-1) LecNotes 18-07 repetition (looping) structures - while loop MATLAB C language % initialize loop sum sum = 0; k = 1; // initialize loop sum sum = 0; k = 1; % add first N numbers while (k<N+1) sum = sum+k; k = k+1; end % while // 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 Engr 0012 (04-1) LecNotes 18-08 repetition (looping) structures - do..while loop MATLAB C 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) Engr 0012 (04-1) LecNotes 18-09 sample C-program /* Your Name(s) Engineering 12, Fall Term 2002 Class Activity 18-1 */ // include libraries #include <stdio.h> preprocessor commands declaration section // prototypes void displayheader( void ); //******************************************************** main() Engr 0012 (04-1) LecNotes 18-10 sample C-program use easily seen marker to separate functions function begin/end enclosed by { } placeholders correspond to variable type //******************************************************** main() { // begin main // variable declarations int first; double second; char letter; variable declaration section for main displayheader(); command to display header // 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 ); & address of operator 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 Engr 0012 (04-1) LecNotes 18-11 //******************************************************** void displayheader( void ) sample C-program needs nothing returns nothing //******************************************************** void displayheader( void ) /* purpose: display header info to screen goal state: header info on screen */ { // begin displayheader printf( printf( printf( printf( printf( "Your name(s)" ); "\nEngineering 12, Fall Term 2003" ); "\nClass Activity 18-1 \n" ); "\n\nThis program performs simple I/O operations" ); "\n" ); } // end displayheader Engr 0012 (04-1) LecNotes 18-12