CS156: How to write a program - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS156: How to write a program

Description:

Many programs will do what we want, but few will be good ... Syntax highlighting helps. Logic errors occur when the program does not perform as expected. ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 15
Provided by: csColo
Category:

less

Transcript and Presenter's Notes

Title: CS156: How to write a program


1
CS156 How to write a program
  • Lecture

2
Hello World 1
  • A simple hello world example

/
hello_world.c Purpose Say hello to the
world. /
include ltstdio.hgt int main() // Say
hello printf(Hello, world.\n) return 0
3
Hello World 2
  • An obfuscated hello world example
  • Whaaaa?
  • Many programs will do what we want, but few will
    be good programs.

includeltstdio.hgt main() int
x0,y14,zy(z)0x48(z)yx0x1D
(z)yx0x07(z)yx0x00(z)y
x0x03 (z)yx-0x43(z)yx-0x
0C(z)yx0x57 (z)yx-0x08(z
)yx0x03(z)yx-0x06
(z)yx-0x08(z)yx-0x43(z)yx-
0x21 x(--z)while(yx!NULL)putchar(yx)

4
Signs of a Good Program
  • Effective - The first goal of a program is to be
    correct.
  • Simple The program should be only as complex as
    necessary to solve the problem.
  • Documented A programmer unfamiliar with the
    program should be able to easily understand what
    it does and how.
  • Robust The program should handle unexpected
    events gracefully.

5
Signs of a Bad Program
  • Lack of useful comments and documentation
  • Overly complex, e.g. sorting a list to find the
    maximum element
  • Clever code is hard to interpret and modify.
  • Many things can be done multiple ways in C,
    choosing the shortest way is not always the best
    idea.

6
Why does good programming matter?
  • Programs often outlive their expected lifetime.
  • Programs often outlive the programmers short term
    memory.
  • Most useful code will end up being modified by
    someone who did not write it.

7
A Few Style Tips
  • Break your program into blocks
  • Document the purpose of each block and how it
    works.
  • Use meaningful variable names.
  • Use the simplest effective code.

8
Practical Program Writing What to do.
  • Understand what the program should do.
  • Describe the program in pseudocode.
  • Write the code from the top down.
  • Work in steps. Write/Compile/Test/Repeat

9
Practical Program Writing What not to do.
  • Dont try to write the program without any
    planning of forethought.
  • Dont try to write the program all at once.
  • Dont send me your code and ask me to tell you
    what is wrong with it.
  • Dont wait until the last minute.

10
Bugs and Errors
  • Bug is a programmers euphemism for error
  • Syntax errors occur when a program does not
    follow the rules defined by the C language.
    Syntax highlighting helps.
  • Logic errors occur when the program does not
    perform as expected. Planning and pseudocode
    helps.

11
Correcting Syntax Errors
  • The compiler detects and describes syntax errors
    during compilation.
  • Often, a single error will cause the compiler to
    detect many errors later in the code.
  • It is best to try to correct the errors one at a
    time. This will keep you from spending time
    trying to correct phantom errors.

12
Example
/
example3-1.c a bad example
/ in
clude ltstdio.hgt int main() float
friction foat number // assign some
values friction .04 number friction 3.2
2 // print number printf(The final number
was f\n, number) printf(The value of
\friction\ was f.\n,friction) return 0
13
Example
preisnergt gcc example3-1.c example3-1.c In
function main example3-1.c10 error foat
undeclared (first use in this function) example3-1
.c10 error (Each undeclared identifier is
reported only once example3-1.c10 error for
each function it appears in.) example3-1.c10
error syntax error before number example3-1.c1
4 error number undeclared (first use in this
function) example3-1.c14 error syntax error
before numeric constant example3-1.c17 error
missing terminating " character example3-1.c18
error syntax error before token
  • The error messages give the file, function, and
    line number of the error. A description of the
    error is also given.
  • Sometimes, an error is not detected until several
    lines after it actually occurred. The line
    number can be misleading.

14
Finding and Correcting Logical Errors
  • Writing a program in stages help in avoiding
    logical errors.
  • To correct logical errors, try to localize the
    error to a small portion of code.
  • Test just this piece by commenting out other
    parts of the code or making a separate test
    program.
  • Use printf to give you the values of variables at
    various points in the program.
  • Use your brain, dont work at random.
Write a Comment
User Comments (0)
About PowerShow.com