Title: Lessons from CST exercise
1Engr 0012 (04-1) LecNotes 18-01
2Contrasting MATLAB with C
MATLAB
C language
No real equivalent
Workspace - interactive computation
main - program control function/algorithm
script - program control algorithm
functions - perform specific computation or task
functions - perform specific computation or task
Engr 0012 (04-1) LecNotes 18-02
3Contrasting variables
MATLAB
C language
You must decide type based upon need and use
Two variable types
double precision, complex string (text)
int (/-32,000) log int (/- 2 billion) float
(8 digits 10/-37) double (16 digits
10/-307) char (ASCII character)
Use meaningful names
Use meaningful names
Engr 0012 (04-1) LecNotes 18-03
4I/O Commands
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-04
5decision (branching) structures - if/elseif
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-05
6decision (branching) structures - switch/case
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-06
7repetition (looping) structures - for loop
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-07
8repetition (looping) structures - while loop
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-08
9repetition (looping) structures - do..while loop
MATLAB
C language
Engr 0012 (04-1) LecNotes 18-09
10sample C-program
/ Your Name(s) Engineering 12, Fall Term
2002 Class Activity 18-1 / // include
libraries include ltstdio.hgt // prototypes
void displayheader( void ) //
main()
preprocessor commands
Engr 0012 (04-1) LecNotes 18-10
11sample C-program
//
main() // begin main // variable
declarations int first double second
char letter displayheader() //
algorithm printf( "\nPlease enter an integer
gt " ) scanf( "d", first )
printf( "\nPlease enter a real number gt " )
scanf( "lf", second ) printf( "\nPlease
enter a character gt " ) 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 )
variable declaration section for main
command to display header
Engr 0012 (04-1) LecNotes 18-11
12sample 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
Engr 0012 (04-1) LecNotes 18-12