0% found this document useful (0 votes)
22 views18 pages

Lecture 2

The document provides an analogy comparing programming to driving a car. It explains that just as driving a car requires controlling basic operations like steering, braking, and accelerating, programming requires combining basic computer instructions to achieve desired tasks. It notes that programming, like driving, involves translating high-level goals into sequences of specific, unambiguous steps that can be executed automatically.

Uploaded by

shuvroo khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views18 pages

Lecture 2

The document provides an analogy comparing programming to driving a car. It explains that just as driving a car requires controlling basic operations like steering, braking, and accelerating, programming requires combining basic computer instructions to achieve desired tasks. It notes that programming, like driving, involves translating high-level goals into sequences of specific, unambiguous steps that can be executed automatically.

Uploaded by

shuvroo khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture #2: How is programming like driving a car?

programming computers
• The “trick”
– figuring out how to get the computer to do what you want it to do

– combining basic “atomic” operations to do interesting things

things we might want to do


• Useful programs
– presentation software (i.e., this program)

– text editor

– computer game

– web browser
computer operations
• Remember – they’re very basic
– get/set memory values

– simple arithmetic

– test values

– execute instructions

getting from operations to programs


• This is “it”
– it’s not easy

– it’s not obvious

– you can usually do something many different ways


• some ways are good
• some suck
so what’s with this driving thing?
• Analogy: driving a car
– why do we drive cars?
• transportation: get from here to there
• fun: drive fast, look good
• race: drive real fast, don’t crash

– how do we drive a car?


• do we just tell the car where we want to go?

– only in science fiction novels


• we have to control the car
car controls
• What controls do we have?
– ignition key

– 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”

so what’s yer point?


• There is no obvious relationship between car operations and what you
want to do with a car
• Likewise, there is no obvious relationship between computer
instructions and what you want to do with a computer
let’s go a little deeper into this analogy
• Driving directions: how to get from point A to point B
• Example: how did I just get here?
– Shan Hai Gardon

– Turn left out of SHG gate

– turn left at second stop sign

– turn right at second stop sign

directions?
• None of those directions mentioned any car parts or operations!
• Our brains interpret automatically
– also handle a lot of unmentioned tasks

• This is where the analogy breaks down


– computers can’t make assumptions
– computers can’t interpolate (unless programmed explicitly to do so)

– computers can’t make judgment calls

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

– go the speed limit

– use the brake as necessary

– flip other drivers the bird


but even these aren’t useful!
• Stay in my lane
– what does that mean?

– how could we express this with numbers?


• measure:

– local average of lane direction

– car velocity vector


• compute:

– difference between lane direction and car velocity vector

– desired deflection of velocity vector


• adjust steering wheel

– 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

– S (“Steering constant”) is a constant that is unique to my

car and my driving style


• turn wheel to desired rotation
• if perceived g-force > Gmax, turn wheel back a bit

details
• Computers are the ultimate detail-oriented things
– they need everything spelled out exactly

– they will follow instructions literally

– they will follow stupid instructions just as diligently as sensible ones


so where does C come in?
• In our analogy, this is how things match up:
– “to get places” : “to balance my checkbook” (or whatever)

– car operations : computer instructions

– driving directions : software design

– instructions : C program

• So when you write a C program, you will be doing things in baby


steps.
doesn’t that get boring?
• It sure can
– but there are ways to make things less tedious

• In terms of the driving analogy…


– in every step, you are regulating the car’s speed
– that means that everywhere you’re going to have the same set of instructions
• adjust gas pedal and brake to make the current speed equal to the desired speed

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

– functions are groups of statements that can be invoked repeatedly

– functions can take parameters, which allow each invocation to be slightly

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

– ANSI/ISO 9899-1990 (R1997)


• “ANSI-standard” C
a simple example
• Here’s a very simple C program:
/*

* HelloWorld: A simple C program

*/
• This is a comment
1). comments help explain the program to
someone #include <stdio.h>
2).they are ignored by the computer.

int main (void)

printf (“Hello, world!\n”);

return 0;


a simple example
• Here’s a very simple C program:
/*

* HelloWorld: A simple C program

*/

#include <stdio.h>

• This is an instruction to the compiler to insert the


contents of a file named “stdio.h” into the program int main (void)

{
--supplies useful information for the compiler printf (“Hello, world!\n”);

return 0;


a simple example
• Here’s a very simple C program:

/*

* HelloWorld: A simple C program

*/

#include <stdio.h>

int main (void)

• This tells the compiler we’re about to define {

a function, named main. printf (“Hello, world!\n”);

return 0;
main is a special function; it is where your }
program starts running

–.
a simple example
• Here’s a very simple C program:

/*

* HelloWorld: A simple C program

*/

#include <stdio.h>

int main (void)

• This is a C statement. {

– This statement calls a function named printf printf (“Hello, world!\n”);

– It causes the text to be printed on the screen return 0;

OK! Now let’s run the example…

You might also like