Comments, Style, Printing - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

Comments, Style, Printing

Description:

Run commands on remote machines. From Unix/Linux/Mac: Open a terminal - type: ... Combine 3 cups flour, 1 tsp. salt, tsp of yeast and cup of water in a large bowl ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 65
Provided by: csInd
Category:

less

Transcript and Presenter's Notes

Title: Comments, Style, Printing


1
Comments, Style, Printing
  • Jan. 15, 2008

2
Hackerz Tip of the Day
  • SSH Secure Shell Protocol
  • Run commands on remote machines
  • From Unix/Linux/Mac
  • Open a terminal -gt type
  • ssh username_at_hostname
  • Then password when prompted
  • From Windows
  • http//ftp.ssh.com/pub/ssh/SSHSecureShellClient-3
    .2.9.exe
  • A graphical SSH client
  • Also does file transfer

3
(No Transcript)
4
Quiz!
  • Given RAM, hard drive, CPU cache, CPU registers
  • Order in terms of access speed (fastest to
    slowest)
  • Order in terms of cost per byte (most expensive
    to least expensive)
  • Give an example of two input devices and two
    output devices
  • What does \n in a printf statement do?
  • What symbol is used for division?

5
30-Second Survey
  • Finished Lab 1 without any problems?
  • How did Lab 1 go?
  • Had more trouble working with Cygwin than
    understanding the programming?
  • Darn you, Cygwin!
  • Have Cygwin/Ubuntu/Mac Developer tools installed
    on personal computer?
  • Feels like they understand last week's material
    completely or almost completely?

6
News
  • Office hours have been updated
  • Monday, 10am-Noon Vivek (I313)?
  • Tuesday, 530pm-730pm Eugene (I313)?
  • Wednesday, 2pm-4pm Matt (I231)?
  • Wednesday, 4pm-6pm Huijun (I313)?
  • Thursday, 1pm-3pm John (I313)?
  • Friday, 230pm-430pm Zhuofeng (I313)

7
What is a Program?
  • Set of computer instructions that define the
    solution to a specific, well-defined problem
  • The general name for this is algorithm
  • A finite set of steps that solve a well-defined
    problem
  • Real world example a cooking recipe

8
Bread Recipe
  • Combine 3 cups flour, 1 tsp. salt, ¼ tsp of yeast
    and ¾ cup of water in a large bowl
  • Mix thoroughly with a wooden spoon
  • Cover with plastic wrap and let rise for 12-18
    hours
  • Fold dough over twice and then put into loaf pan
  • Let rise 3-4 hours
  • Cook at 400 F for 45 minutes

9
Recipe as Algorithm
  • Instructions are in sequential order
  • Don't do step 2 before doing step 1!
  • Instructions are exact
  • Exact enough to be easily reproducible by others

10
Recipe as Algorithm
  • Recipe solves a well-defined problem
  • Lack of yummy bread
  • Recipe is finite, it has an end
  • We will get bread sometime tomorrow
  • Bread has been proven to be not as tasty when it
    takes an infinite number of years to bake it

11
Computer Programs as Algorithms
  • Should perform a well-defined task
  • Calculations, communications, games, etc.
  • Should finish in a finite amount of time
  • Should have explicit instructions
  • The computer has to be told what to do
  • We have to tell it what to printf
  • We have to tell it which library printf comes
    from stdio.h
  • Instructions are executed in order

12
File system Organization
13
Organizing Directories
  • It's important to keep things organized
  • We're going to use two directories
  • src stores all our source code
  • bin stores all our executable applications
  • (Why do you think it's called bin?)?

14
Home Directory
i210
src
bin
hello.exe
add.exe
add.c
hello.c
15
Compiling From src to bin
  • gcc -Wall -o ../bin/hello.exe hello.c

16
Compiling From src to bin
Relative path to bin folder
  • gcc -Wall -o ../bin/hello.c hello.c

17
How are variables stored in memory?
18
Starting Memory (idealized)?
19
Printing Multiple Values
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 10
  • return( 0 )

20
Memory During Execution
21
Memory with Realistic Addresses
22
Memory Over Time
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 0
  • y x 5
  • return( 0 )

23
Memory Over Time
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 0
  • y x 5
  • return( 0 )

Snapshot 1
Snapshot 2
24
Memory at Snapshot 1
25
Memory at Snapshot 2
26
Starting Memory (Realistic)?
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x
  • printf( x is d\n, x )
  • return( 0 )

27
Starting Memory (Realistic)?
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x
  • printf( x is d\n, x )
  • return( 0 )

No initial value given to the variable, x
28
Starting Memory (Realistic)?
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x
  • printf( x is d\n, x )
  • return( 0 )

Can we assume this value is 0? No.
29
Starting Memory (Realistic)?
Garbage Values
Garbage Values
30
Printing Multiple Values
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 10
  • printf( "x is d, y is d\n", x, y )
  • return( 0 )

31
Review Assignment Operator
  • Use to assign new values to variables

x y 10
32
The Assignment Operator
  • Use to assign new values to variables

x y 10
x is getting a new value
33
The Assignment Operator
  • Use to assign new values to variables

x y 10
x is getting a new value
Right-hand side is evaluated first, by looking up
y's value and multiplying by 10
34
The Assignment Operator
  • Right-hand side can even have the old value of
    the variable

x x 8
35
The Assignment Operator
  • Right-hand side can even have the old value of
    the variable

x x 8
Look up old value of x and add 8.
36
The Assignment Operator
  • Right-hand side can even have the old value of
    the variable

x x 8
Look up old value of x and add 8.
Assign the result back into x's memory slot
37
Making your Programs Human Understandable
38
Programming code can be cryptic
include ltstdio.hgt main(t,_,a)char
areturn!0ltt?tlt3?main(-79,-13,amain(-87,1-_, ma
in(-86,0,a1)a))1,tlt_?main(t1,_,a)3,main(-94,-
27t,a)t2?_lt13? main(2,_1,"s d
d\n")916tlt0?tlt-72?main(_,t, "_at_n','/w/wc
dnr/,r/de,/,/w,/wqn,/l,,/nn,/
n,/\ qn,/k,/'r 'd'3,wK
w'K'e'dq'l \ q'd'K!/kq'reKKw'reKK
nl'/qn'))w'))nl'/n'drw' i
\ )nl!/nn' rw'r ncnl'/l,'K rw'
iKnl'/wqn'wk nw' \ iwkKKnl!/w'lw'
i nl'/q'ldr'nlwb!/de'c
\ nl'-rw'/,'nc,',nw'/kd'e'rdq
w! nr'/ ') rl'n' ') \ '(!!/")? tlt-50
?_a?putchar(31a)main(-65,_,a1)main((a'/
')t,_,a1)? 0ltt?main(2,2,"s")a'/'main(0
,main(-61,a, "!ekdc i_at_bK'(q)-wnr3l,\nuw
loca-Om .vpbks,fxntdCeghiry"),a1)
39
Clarity Helper 1 Comments
  • Comments help programmers figure out what
    different instructions are doing
  • They're basically human-readable notes that don't
    affect the program's execution
  • Comments are completely ignored by the compiler
    when building an executable

40
Single Line Comment
  • A comment that ends at the end of the line
  • Denoted in the code by the use of //
  • Everything that follows // is part of the comment
    and doesn't do anything when the program is
    executed

41
Single Line Comment Example
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 10
  • // Print out the values of x and y
  • printf( "x is d, y is d\n", x, y )
  • return( 0 )

42
Multi-line Comment
  • Sometimes we need to write comments that cover
    several lines
  • Option 1
  • Put single line comment markers, //, at the
    beginning of each line of the multi-line comment
  • What if we have several paragraphs of description
    in a very complicated program?

43
Multi-line Comments using Single Line syntax
  • int main( int argc, char argv )?
  • int x 5
  • // It is now vital to show the user
  • // the value of x.
  • // In order to do this we will use
  • // the printf function to display the
  • // value on the monitor of the user.
  • printf( "x is d\n", x )
  • return( 0 )

44
Option 2 Real Multi-line Comments
  • Use the multi-line comment syntax
  • / is the starting marker
  • / is the ending marker
  • Everything in between these markers is part of
    the comment

45
Multi-line Comments using Single Line syntax
  • int main( int argc, char argv )?
  • int x 5
  • / It is now vital to show the user
  • the value of x.
  • In order to do this we will use
  • the printf function to display the
  • value on the monitor of the user. /
  • printf( "x is d\n", x )
  • return( 0 )

46
When to use Comments
  • Use comments to describe complicated parts of a
    program
  • Makes it easier for others you're working with to
    understand your code
  • It is very easy to forget what your old code was
    supposed to be doing
  • Help out your future self!
  • Use comments to take out parts of your code, so
    they're not executed
  • Example with printf line

47
Header Comments
  • Each program you write should start with a
    multi-line comment that includes essential
    information about the program
  • Your name
  • Date
  • Which assignment/lab/project it is for
  • Any unusual requirements to compile or run your
    program
  • A brief overview of what the program does

48
  • /
  • Matt Whitehead
  • Jan. 15, 2008
  • Lecture Example 6
  • This program shows how to define variables and
    perform simple mathematical operations on them.
  • /
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • ...

49
Clarity Helper 2 Appropriate Variable Names
  • Variable names can be nearly anything
  • xouw15erjfjjj88 is a legal variable name
    (unfortunately)?
  • Variable names should be short and descriptive
  • The name should make it clear what the value
    being stored represents
  • daysUntilSummer
  • booksCheckedOut
  • hatsOwned

50
Case of Variable Names
  • Variable names should be in Camel Case
  • First letter is lowercase
  • Subsequent letters that start words should be
    uppercase
  • daysUntilSummer
  • booksCheckedOut
  • hatsOwned

51
Clarity Helper 3 Program Layout
  • Keep your program nicely organized in terms of
    spacing and indentation
  • These help improve the readability of your code
    and make it far easier to work with

52
Manage Vertical Spacing Adding Blank Lines
  • Add blank lines to separate logical blocks of
    code
  • Add a blank line above the return statement
  • Add a blank line above the main function header
  • Add a blank line after your variable declarations

53
BAD Vertical Space Management
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5 int y 0
  • y x 5
  • return( 0 )

54
GOOD Vertical Space Management
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 0
  • y x 5
  • return( 0 )

55
Manage Horizontal Spacing Adding Spaces/Tabs
  • Code Indentation
  • Indent code inside the main function
  • Indentation should be 4 spaces in
  • Indentation helps us see which code is inside the
    main function (or other block of code, as we'll
    see later on)?
  • Spacing around operators
  • Leave a space around mathematical operators
  • Leave space inside parentheses

56
BAD Horizontal Space Management
  • include ltstdio.hgt
  • int main(int argc,charargv)?
  • int x 5
  • int y0
  • y x 5
  • return(0)

57
GOOD Horizontal Space Management
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • int x 5
  • int y 0
  • y x 5
  • return( 0 )

58
New Datatypes
59
Floating Point Values
  • So far we've just used integers
  • Obviously, sometimes we need the fractional part
    of numbers to perform accurate calculations
  • Just as simple to declare and use as integers

60
Declaring and Using Floats
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • float x 5.55
  • printf( x is f\n, x )
  • return( 0 )

61
Declaring and Using Floats
Use the data type float for numbers with decimal
values
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • float x 5.55
  • printf( x is f\n, x )
  • return( 0 )

62
Declaring and Using Floats
Now you can use a decimal point in your
initializations
Use the data type float for numbers with decimal
values
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • float x 5.55
  • printf( x is f\n, x )
  • return( 0 )

63
Declaring and Using Floats
Now you can use a decimal point in your
initializations
Use the data type float for numbers with decimal
values
  • include ltstdio.hgt
  • int main( int argc, char argv )?
  • float x 5.55
  • printf( x is f\n, x )
  • return( 0 )

Printing a floating point value is similar, but
uses f instead of d
64
Float Examples
Write a Comment
User Comments (0)
About PowerShow.com