Week 1 part ii
This presentation is the property of its rightful owner.
Sponsored Links
1 / 63

Week 1 Part II PowerPoint PPT Presentation


  • 41 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Week 1 Part II

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Week 1 part ii

Week 1 Part II

  • Kyle Dewey


Overview

Overview

  • Lab

  • The art of programming

  • Basic data types

  • Variable declaration and initialization

  • Data representation


Lab 1

Lab 1


The art of programming

The Art of Programming


General advice

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


Top down programming

Top-Down Programming

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


Top down

Top-Down

Make Sandwich


Top down1

Top-Down

Make Sandwich

Put Meat on Bread

Put Sides Together


Top down2

Top-Down

Make Sandwich

Put Meat on Bread

Put Sides Together

Get Meat from Fridge

Get Bread from Cabinet


Top down3

Top-Down

Make Sandwich

Put Meat on Bread

Put Sides Together

Get Meat from Fridge

Get Bread from Cabinet

Open Fridge...

Open Cabinet...


Bottom up programming

Bottom-Up Programming

  • Glue preexisting pieces together to derive a solution


Bottom up

Bottom-Up

Handlebars

Chain

Frame

Gears

Wheels


Bottom up1

Bottom-Up

Handlebars

Chain

Bolt

Frame

Gears

Wheels


Bottom up2

Bottom-Up

Handlebars

Chain

Bolt

Frame

Gears

Bolt

Wheels


Bottom up3

Bottom-Up

Handlebars

Chain

Oil

Bolt

Frame

Gears

Bolt

Wheels


Bottom up4

Bottom-Up

Handlebars

Chain

Chain

Tool

Oil

Bolt

Frame

Gears

Bolt

Wheels


Combination

Combination

  • printf / scanf are generally written once (preexisting)

  • A middle ground


Divide and conquer

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


Divide and conquer1

Divide and Conquer

Program

Input

Output


Divide and conquer2

Divide and Conquer

Subprogram 1

Input

Output / Input

Subprogram 2

Output


Divide and conquer3

Divide and Conquer

Subprogram A

Input

Subprogram B

Output / Input

Subprogram 2

Output


General five step process

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


General five step process1

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


Problem statement

Problem Statement

  • What are we trying to solve?

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


General five step process2

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


Input output description

Input / Output Description

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

Output 1

Input I

Program

Output 2

Input 2

(A miracle occurs)


General five step process3

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


Hand example

Hand Example

  • Do the problem by hand

  • See if you actually understand what must be done


General five step process4

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


Algorithm development

Algorithm Development

  • Steps needed to solve the problem

  • Ultimately involves writing code

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


General five step process5

General Five Step Process

  • Problem Statement

  • Input / Output Description

  • Hand Example

  • Algorithm Development

  • Testing


Testing

Testing

  • See if solution actually works

  • Important step!

  • “50% of our code is tests”


Week 1 part ii

TDD

  • Test-driven development

  • Write tests before code

  • Wildly popular right now

  • The point: testing is important!


Week 1 part ii

C


Hello c

hello.c


Running code

Running Code

  • Via make / gcc / cc (compilation)

  • Via ch (interpretation)


Compilation

Compilation

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

  • The result runs as fast as the machine

  • Certain interdependencies not handled


Object files

Object Files

1: somethingFromHere();

2: somethingFromElsewhere();

3: somethingElseFromHere();

somethingFromHere

somethingFromElsewhere

somethingElseFromHere


Linking

Linking

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

  • Connects different pieces of code together


Linking1

Linking

somethingFromElsewhere

somethingFromHere

somethingElseFromHere


Linking2

Linking

somethingFromHere

somethingFromElsewhere

somethingElseFromHere


Overall process

Overall Process

Linking

Compilation

Source Code File #1

Object File #1

Executable

Source Code File #2

Object File #2


Question

Question

  • Why separate compilation and linking?


Interpretation

Interpretation

  • As with ch

  • A program that understands the language runs it directly

  • Runs as fast as the interpreter


Questions

Questions

  • Why compile versus interpret?

  • Why interpret versus compile?


Types

Types


Types1

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)


Basic types

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,...)


Variables

Variables

  • A container for a value

  • Must have a type


Variable declaration

Variable Declaration

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

int myInteger;

double myFloatingPoint;

char myCharacter;


Question1

Question

int myVariable;

int x = myVariable + 1;

// what does x equal?


Answer

Answer?

  • ch: 1

  • gcc: 1


Uninitialized variables

Uninitialized Variables

  • Officially, the initial value is “undefined”

  • Undefined means “anything goes”

  • Can be a source of tricky bugs


Variable assignment

Variable Assignment

  • The values of variables can be initialized...

int myVariable = 0;

-or-

int myVariable;

myVariable = 0;


Variable assignment1

Variable Assignment

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

int myVariable = 0;

myVariable = 5 + 2;


Variable assignment2

Variable Assignment

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

int myVariable = 0;

myVariable = 5 + 2;

myVariable = 10 - myVariable;


Data representation

Data Representation


Data representation1

Data Representation

  • Many programming languages abstract away data representation

  • C is not (exactly) one of those languages


Some definitions

Some Definitions

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

  • byte: 8 bits

  • kilobyte: 1024 bytes


Sizes

Sizes

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

  • And a byte holds 8 bits...

  • How many possible values in a byte?

  • A kilobyte?


Sizes and types

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)


Questions1

Questions

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

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


Questions2

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?


  • Login