170 likes | 332 Views
Today’s Class. Office Hours Questions/problems with Matlab assignment Recap Programming Assignment What is C? How does it compare to Matlab? Simple C Program. Office Hours. Options for Office Hours – Oliver Wednesdays 9:30-11:30 Monday: 10:30-11:30 and 4-5 My office hours
E N D
Today’s Class • Office Hours • Questions/problems with Matlab assignment • Recap • Programming Assignment • What is C? How does it compare to Matlab? • Simple C Program R. Smith - University of St Thomas - Minnesota
Office Hours • Options for Office Hours – Oliver • Wednesdays 9:30-11:30 • Monday: 10:30-11:30 and 4-5 • My office hours • 9am till class starts (9:35 or 9:55) • TTH after class: 3:15-4:15 R. Smith - University of St Thomas - Minnesota
Questions/Problems with Matlab • Sums • Graphing • Strings • Booleans R. Smith - University of St Thomas - Minnesota
Recap • C and Matlab use similar arithmetic notation • Identical in infix format, operator precedence, parentheses • A few minor differences in tokens used for operators • C and Matlab use same assignment statement • Numbers are different from text (even numeric) • ‘123’ in quotes is not the same as the number 123 • You can do arithmetic with both, with different results • C and Matlab have similar Boolean operators • Same six comparisons, uses one different token (!=) • C and Matlab have similar Logical operators • And, Or, Not: different tokens, though • Actually, I tend to lump the Boolean and Logical operators together in a single category myself. R. Smith - University of St Thomas - Minnesota
First C Program Assignment • Build-a-Boat, Phase 1 • Calculate a cost for a simple custom boat • Estimate the space available per person • Due next Wednesday • More about that later R. Smith - University of St Thomas - Minnesota
How is C different from Matlab? • C is compiled • A special program, the compiler, converts the C program from ASCII source code into binary coded computer instructions • In Matlab, the computer has to do that conversion whenever we type in an expression – we say Matlab is interpreted • Errors arise on two different occasions • Syntax errors and some usage errors are found in compilation • In a perfect world, this finds all possible errors • In practice, lots of errors slip through (a trade-off) • Other errors arise when the program actually ‘runs’ R. Smith - University of St Thomas - Minnesota
Why is C popular? • It is portable • Easy to modify to create programs for another computer • The compiler itself is easily ‘hosted’ on other computers • A fairly complete programming language • A rich set of data types and operations on them • Standard control structures, allows structured programming • Only missing object technology • “Close to the machine” • Easy to write highly technical programs for operating systems • Mechanisms map easily to the machine level = efficient • Highly efficient – optimizing compilers R. Smith - University of St Thomas - Minnesota
Problems with C • C programs can be hard to read • Obfuscated C Contest • Data types are not enforced • Compiler doesn’t catch every error it could • Some operations have ‘side effects’ • Expressions don’t evaluate consistently to the same answer • In this class, we note and avoid operations with side effects • Compared to Matlab • Requires more steps, more typing to yield a simple result • Unsophisticated output: no graphing • Rigid handling of input text – programmer must write code to interpret and process typed inputs R. Smith - University of St Thomas - Minnesota
Who uses C today? • Operating systems • Windows is mostly written in C • Linux/Unix is mostly written in C – that’s where C started • Macintosh software is mostly written in C • C++ is an object-oriented version that sees lots of use • Compilers • C and C++ compilers are written in C • Application packages, like Excel • Many are being written in Java these days – GUIs are easier • Embedded Applications • Gas pumps, USB hubs, cell phones, lab equipment (!!!) R. Smith - University of St Thomas - Minnesota
Matlab is Better for… Vector and matrix math When sizes aren’t known ahead of time Graphing – it’s built in Processing standard file formats – built in Images and spreadsheets C is Better for… Group programming Established ways of sharing the work Tight quarters When the computer is too small to run Matlab Little or no OS support Computer doesn’t have graphics, not very interactive Access to I/O hardware Easy to manipulate I/O control registers from C Choosing between C and Matlab R. Smith - University of St Thomas - Minnesota
How We Write C Programs • Start Textpad (the blue “T,” paper, & hand icon) • Type in some C source code • Save it with a .c suffix • Go to the “Tools” menu • Select “External Tools” and then “Compile C Program” • Error messages go into a “Command Window” • If no errors, you can run your program • To run, go to the Tools menu again • Select “External Tools” and then “Run C Program” • Program will run in an MSDOS command window R. Smith - University of St Thomas - Minnesota
First C Program /* hello.c – first C program */ #include <stdio.h> main() { printf(“Hello, world!\n”); } R. Smith - University of St Thomas - Minnesota
How We Write C Programs • Start Textpad (the blue “T,” paper, & hand icon) • Type in some C source code • Save it with a .c suffix • Go to the “Tools” menu • Select “External Tools” and then “Compile C Program” • Error messages go into a “Command Window” • If no errors, you can run your program • To run, go to the Tools menu again • Select “External Tools” and then “Run C Program” • Program will run in an MSDOS command window R. Smith - University of St Thomas - Minnesota
Notes about hello.c • Programs reside in text files • Comments delimited by /* and */ • #include lets us use particular functions • Types, operators, syntax are built in to C • Functions are not built in – they’re added on • Functions handle I/O, trickier math, other common tasks • We are defining a function named main • All functions are followed by parentheses in C • Function definitions are delimited by braces { } • The printf() function prints things • Statements end with a semicolon “;” R. Smith - University of St Thomas - Minnesota
Program Examples in Class • ATTENTION! • For those who didn’t notice – there are COMPUTERS in this room. • When I type up an example on the board, you need to type it up and try it yourself. • KEEP THE OLD EXAMPLES • We will use them for later examples R. Smith - University of St Thomas - Minnesota
First C Program Assignment • Build-a-Boat, Phase 1 • Calculate a cost for a simple custom boat • Declare variables for length and width • Initialize to personally selected values • Cost = length x width x $87 + seats x $53 • Estimate the space available per person • Estimate the area by assuming a simple ‘diamond’ shape • Due next Wednesday R. Smith - University of St Thomas - Minnesota
Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. R. Smith - University of St Thomas - Minnesota