week 1 part ii
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 PresentationTRANSCRIPT
![Page 1: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/1.jpg)
Week 1 Part IIKyle Dewey
![Page 2: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/2.jpg)
Overview
•Lab
•The art of programming
•Basic data types
•Variable declaration and initialization
•Data representation
![Page 3: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/3.jpg)
Lab 1
![Page 4: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/4.jpg)
The Art of Programming
![Page 5: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/6.jpg)
Top-Down Programming
•Start general and gradually become specific, filling in the details as you go along
![Page 7: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/7.jpg)
Top-DownMake Make
SandwichSandwich
![Page 8: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/8.jpg)
Top-DownMake Make
SandwichSandwich
Put Meat Put Meat on Breadon Bread
Put Put Sides Sides
TogetherTogether
![Page 9: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/11.jpg)
Bottom-Up Programming
•Glue preexisting pieces together to derive a solution
![Page 12: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/12.jpg)
Bottom-Up
HandlebarsHandlebars
FrameFrame
WheelsWheels
ChainChain
GearsGears
![Page 13: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/13.jpg)
Bottom-Up
HandlebarsHandlebars
FrameFrame
WheelsWheels
ChainChain
GearsGears
Bolt
![Page 14: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/14.jpg)
Bottom-Up
HandlebarsHandlebars
FrameFrame
WheelsWheels
ChainChain
GearsGears
Bolt
Bolt
![Page 15: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/15.jpg)
Bottom-Up
HandlebarsHandlebars
FrameFrame
WheelsWheels
ChainChain
GearsGears
Bolt
Bolt
Oil
![Page 16: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/16.jpg)
Bottom-Up
HandlebarsHandlebars
FrameFrame
WheelsWheels
ChainChain
GearsGears
Bolt
Bolt
Oil
ChainTool
![Page 17: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/17.jpg)
Combination
•printf / scanf are generally written once (preexisting)
•A middle ground
![Page 18: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/19.jpg)
Divide and Conquer
ProgramProgram
Input
Output
![Page 20: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/20.jpg)
Divide and Conquer
SubprograSubprogram 1m 1
Input
OutputSubprograSubprogra
m 2m 2
Output / Input
![Page 21: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/21.jpg)
Divide and Conquer
Input
OutputSubprograSubprogra
m 2m 2
Subprogram Subprogram AA
Subprogram Subprogram BB Output / Input
![Page 22: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/28.jpg)
Hand Example
•Do the problem by hand
•See if you actually understand what must be done
![Page 29: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/32.jpg)
Testing
•See if solution actually works
•Important step!
•“50% of our code is tests”
![Page 33: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/33.jpg)
TDD
•Test-driven development
•Write tests before code
•Wildly popular right now
•The point: testing is important!
![Page 34: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/34.jpg)
C
![Page 35: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/35.jpg)
hello.c
![Page 36: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/36.jpg)
Running Code
•Via make / gcc / cc (compilation)
•Via ch (interpretation)
![Page 37: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/38.jpg)
Object Files
1: somethingFromHere();2: somethingFromElsewhere();3: somethingElseFromHere();
somethingFromHere
somethingElseFromHere
somethingFromElsewhere
![Page 39: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/39.jpg)
Linking
•Technically separate from compilation, but the two often get conflated
•Connects different pieces of code together
![Page 40: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/40.jpg)
Linking
somethingFromHere
somethingElseFromHere
somethingFromElsewhere
![Page 41: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/41.jpg)
Linking
somethingFromHeresomethingFromHeresomethingFromElsewhersomethingFromElsewher
eesomethingElseFromHeresomethingElseFromHere
![Page 42: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/42.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/43.jpg)
Question
•Why separate compilation and linking?
![Page 44: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/44.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/45.jpg)
Questions
•Why compile versus interpret?
•Why interpret versus compile?
![Page 46: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/46.jpg)
Types
![Page 47: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/47.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/48.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/49.jpg)
Variables
•A container for a value
•Must have a type
![Page 50: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/50.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/51.jpg)
Question
int myVariable;int x = myVariable + 1;// what does x equal?
![Page 52: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/52.jpg)
Answer?
•ch: 1
•gcc: 1
![Page 53: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/53.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/54.jpg)
Variable Assignment
•The values of variables can be initialized...
int myVariable = 0;-or-
int myVariable;myVariable = 0;
![Page 55: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/55.jpg)
Variable Assignment
•...or changed on the fly...
int myVariable = 0;myVariable = 5 + 2;
![Page 56: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/56.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/57.jpg)
Data Representation
![Page 58: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/58.jpg)
Data Representation
•Many programming languages abstract away data representation
•C is not (exactly) one of those languages
![Page 59: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/59.jpg)
Some Definitions
•bit: binary digit (true/false, 0/1, yes/no)
•byte: 8 bits
•kilobyte: 1024 bytes
![Page 60: Week 1 Part II](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/60.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/61.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/62.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062301/56814590550346895db27e2d/html5/thumbnails/63.jpg)
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?