Lecture 2
Lecture 2
programming computers
• The “trick”
– figuring out how to get the computer to do what you want it to do
– text editor
– computer game
– web browser
computer operations
• Remember – they’re very basic
– get/set memory values
– simple arithmetic
– test values
– execute instructions
– steering wheel
– gas pedal
– brake
– clutch
– shifter
– turn signals
– radio
car operations
• So what kinds of operations can we perform?
– ignition key
• turn car on
• turn car off
– steering wheel
• rotate steering wheel clockwise
• rotate steering wheel counterclockwise
– gas pedal
• depress gas pedal (%)
– brake
• depress brake pedal (%)
car operations continued
• What kinds of operations can we perform?
– clutch
• depress clutch pedal (%)
– shifter
• move up
• move down
• move left
• move right
– turn signals
• ignore them
• signal right turn
• signal left turn
and just a couple more…
• Those were all “output” operations
• Input is important too
– your eyes
• look
– your ears
• “loud pipes save lives”
directions?
• None of those directions mentioned any car parts or operations!
• Our brains interpret automatically
– also handle a lot of unmentioned tasks
interpreting directions
• Let’s look at the first direction:
to Shan Hai Gardon
• What does this really mean?
– assumes I know how to get onto Shan Hai Gardon gate
– stay in my lane
– feedback loop
» magnitude of difference between current and desired direction
» perceived centripetal force
stay in my lane
• Instructions:
– repeat the following continuously:
• get lane direction
• get car direction
• desired deflection = difference (lane direction, car direction)
• desired steering wheel rotation = S * desired deflection
details
• Computers are the ultimate detail-oriented things
– they need everything spelled out exactly
– instructions : C program
functions
• So, why not write the instructions generically once, and then invoke
the same set of instructions from different places in the directions?
– this is the C concept of a function
different
• such as the “desired speed” in the last example
so (finally!) let’s talk about C
• What is C?
– One of countless programming languages
• but one of the more popular
• was considered a “programmer’s” language
– K&R C
*/
• This is a comment
1). comments help explain the program to
someone #include <stdio.h>
2).they are ignored by the computer.
return 0;
–
a simple example
• Here’s a very simple C program:
/*
*/
#include <stdio.h>
{
--supplies useful information for the compiler printf (“Hello, world!\n”);
return 0;
–
a simple example
• Here’s a very simple C program:
/*
*/
#include <stdio.h>
return 0;
main is a special function; it is where your }
program starts running
–.
a simple example
• Here’s a very simple C program:
/*
*/
#include <stdio.h>
• This is a C statement. {