week 1 part ii

63
Week 1 Part II Kyle Dewey

Upload: duy

Post on 13-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Week 1 Part II. Kyle Dewey. Overview. Lab The art of programming Basic data types Variable declaration and initialization Data representation. Lab 1. The Art of Programming. General Advice. Top-down programming: Start with the big picture and get more specific - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 1 Part II

Week 1 Part IIKyle Dewey

Page 2: Week 1 Part II

Overview

•Lab

•The art of programming

•Basic data types

•Variable declaration and initialization

•Data representation

Page 3: Week 1 Part II

Lab 1

Page 4: Week 1 Part II

The Art of Programming

Page 5: Week 1 Part II

General Advice•Top-down programming: Start with

the big picture and get more specific

•Bottom-up programming: work up to the big picture by glueing together smaller, existing pieces

•Divide and conquer: break a problem up into individual piecs

Page 6: Week 1 Part II

Top-Down Programming

•Start general and gradually become specific, filling in the details as you go along

Page 7: Week 1 Part II

Top-DownMake Make

SandwichSandwich

Page 8: Week 1 Part II

Top-DownMake Make

SandwichSandwich

Put Meat Put Meat on Breadon Bread

Put Put Sides Sides

TogetherTogether

Page 9: Week 1 Part II

Top-DownMake Make

SandwichSandwich

Put Put Sides Sides

TogetherTogether

Put Meat Put Meat on Breadon Bread

Get Meat Get Meat from from

FridgeFridge

Get Bread Get Bread from Cabinetfrom Cabinet

Page 10: Week 1 Part II

Top-DownMake Make

SandwichSandwich

Put Put Sides Sides

TogetherTogether

Put Meat Put Meat on Breadon Bread

Get Meat Get Meat from from

FridgeFridge

Get Bread Get Bread from Cabinetfrom Cabinet

Open Open Fridge...Fridge...

Open Open Cabinet...Cabinet...

Page 11: Week 1 Part II

Bottom-Up Programming

•Glue preexisting pieces together to derive a solution

Page 12: Week 1 Part II

Bottom-Up

HandlebarsHandlebars

FrameFrame

WheelsWheels

ChainChain

GearsGears

Page 13: Week 1 Part II

Bottom-Up

HandlebarsHandlebars

FrameFrame

WheelsWheels

ChainChain

GearsGears

Bolt

Page 14: Week 1 Part II

Bottom-Up

HandlebarsHandlebars

FrameFrame

WheelsWheels

ChainChain

GearsGears

Bolt

Bolt

Page 15: Week 1 Part II

Bottom-Up

HandlebarsHandlebars

FrameFrame

WheelsWheels

ChainChain

GearsGears

Bolt

Bolt

Oil

Page 16: Week 1 Part II

Bottom-Up

HandlebarsHandlebars

FrameFrame

WheelsWheels

ChainChain

GearsGears

Bolt

Bolt

Oil

ChainTool

Page 17: Week 1 Part II

Combination

•printf / scanf are generally written once (preexisting)

•A middle ground

Page 18: Week 1 Part II

Divide and Conquer

•Really part of top-down and bottom-up

•Break a problem down into distinct subproblems, solve them individually, and finally combine them

Page 19: Week 1 Part II

Divide and Conquer

ProgramProgram

Input

Output

Page 20: Week 1 Part II

Divide and Conquer

SubprograSubprogram 1m 1

Input

OutputSubprograSubprogra

m 2m 2

Output / Input

Page 21: Week 1 Part II

Divide and Conquer

Input

OutputSubprograSubprogra

m 2m 2

Subprogram Subprogram AA

Subprogram Subprogram BB Output / Input

Page 22: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 23: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 24: Week 1 Part II

Problem Statement

•What are we trying to solve?

•(Believe it or not, real software often goes astray here arguably more than anywhere else)

Page 25: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 26: Week 1 Part II

Input / Output Description

•What are the program inputs and what are the outputs?

ProgramProgram

Input I

Input 2

(A miracle occurs)

Output 1

Output 2

Page 27: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 28: Week 1 Part II

Hand Example

•Do the problem by hand

•See if you actually understand what must be done

Page 29: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 30: Week 1 Part II

Algorithm Development

•Steps needed to solve the problem

•Ultimately involves writing code

•“Days of coding can save you hours of planning”

Page 31: Week 1 Part II

General Five Step Process

1.Problem Statement

2.Input / Output Description

3.Hand Example

4.Algorithm Development

5.Testing

Page 32: Week 1 Part II

Testing

•See if solution actually works

•Important step!

•“50% of our code is tests”

Page 33: Week 1 Part II

TDD

•Test-driven development

•Write tests before code

•Wildly popular right now

•The point: testing is important!

Page 34: Week 1 Part II

C

Page 35: Week 1 Part II

hello.c

Page 36: Week 1 Part II

Running Code

•Via make / gcc / cc (compilation)

•Via ch (interpretation)

Page 37: Week 1 Part II

Compilation

•Code is ultimately converted to a form the machine understands directly

•The result runs as fast as the machine

•Certain interdependencies not handled

Page 38: Week 1 Part II

Object Files

1: somethingFromHere();2: somethingFromElsewhere();3: somethingElseFromHere();

somethingFromHere

somethingElseFromHere

somethingFromElsewhere

Page 39: Week 1 Part II

Linking

•Technically separate from compilation, but the two often get conflated

•Connects different pieces of code together

Page 40: Week 1 Part II

Linking

somethingFromHere

somethingElseFromHere

somethingFromElsewhere

Page 41: Week 1 Part II

Linking

somethingFromHeresomethingFromHeresomethingFromElsewhersomethingFromElsewher

eesomethingElseFromHeresomethingElseFromHere

Page 42: Week 1 Part II

Overall Process

Source Source Code File Code File

#1#1Source Source

Code File Code File #2#2

Object File Object File #1#1

Object File Object File #2#2

ExecutableExecutable

Compilation Linking

Page 43: Week 1 Part II

Question

•Why separate compilation and linking?

Page 44: Week 1 Part II

Interpretation

•As with ch

•A program that understands the language runs it directly

•Runs as fast as the interpreter

Page 45: Week 1 Part II

Questions

•Why compile versus interpret?

•Why interpret versus compile?

Page 46: Week 1 Part II

Types

Page 47: Week 1 Part II

Types

•All data in C has a type

•Types determine:

•What can be done with the data

•How much space they take up (as with the sizeof operator)

Page 48: Week 1 Part II

Basic Types

•int: Integers (i.e. 1, 2, 3...)

•Come in both signed and unsigned forms

•double: floating point numbers (i.e. 5.1, 7.8, 2.93, ...)

•char: A character (i.e. a, b, c,...)

Page 49: Week 1 Part II

Variables

•A container for a value

•Must have a type

Page 50: Week 1 Part II

Variable Declaration

•Telling the compiler that a variable with a given name and type can be used

int myInteger;double myFloatingPoint;char myCharacter;

Page 51: Week 1 Part II

Question

int myVariable;int x = myVariable + 1;// what does x equal?

Page 52: Week 1 Part II

Answer?

•ch: 1

•gcc: 1

Page 53: Week 1 Part II

Uninitialized Variables

•Officially, the initial value is “undefined”

•Undefined means “anything goes”

•Can be a source of tricky bugs

Page 54: Week 1 Part II

Variable Assignment

•The values of variables can be initialized...

int myVariable = 0;-or-

int myVariable;myVariable = 0;

Page 55: Week 1 Part II

Variable Assignment

•...or changed on the fly...

int myVariable = 0;myVariable = 5 + 2;

Page 56: Week 1 Part II

Variable Assignment

•...or even be used to update the same variable!

int myVariable = 0;myVariable = 5 + 2;myVariable = 10 - myVariable;

Page 57: Week 1 Part II

Data Representation

Page 58: Week 1 Part II

Data Representation

•Many programming languages abstract away data representation

•C is not (exactly) one of those languages

Page 59: Week 1 Part II

Some Definitions

•bit: binary digit (true/false, 0/1, yes/no)

•byte: 8 bits

•kilobyte: 1024 bytes

Page 60: Week 1 Part II

Sizes

•If a bit can hold two possible values...

•And a byte holds 8 bits...

•How many possible values in a byte?

•A kilobyte?

Page 61: Week 1 Part II

Sizes and Types

•The actual size of variables depends on the compiler and the particular computer

•sizeof is your friend if it’s important, as is limits.h (see typelimits.c)

Page 62: Week 1 Part II

Questions

•Integers are often 4 bytes. How many possible values can this hold?

•Signed versus unsigned integers: Does this make a difference for size?

Page 63: Week 1 Part II

Questions

•What’s the largest number that can be stored in an unsigned 4 byte integer?

•What’s around the largest number that can be stored in a signed 4 byte integer?