Homework - PowerPoint PPT Presentation

About This Presentation
Title:

Homework

Description:

... line character just as in 'Hello World!' Second Argument to printf = fahr ... Quoted string 'Value: . . .' is just like 'Hello, world', but = and %6.1f are ... – PowerPoint PPT presentation

Number of Views:161
Avg rating:3.0/5.0
Slides: 21
Provided by: bobwi9
Learn more at: https://www.cs.umb.edu
Category:
Tags: hello | homework

less

Transcript and Presenter's Notes

Title: Homework


1
Homework
  • Done the reading?
  • KR 1.1-1.9
  • Glass Chapters 1 and 2
  • Applied for cs240? (If not, keep at it!)
  • Gotten a UNIX account? (If not, keep at it!)
  • Making progress on HW1? Due next class!
  • HW2 will be on line by next class.

2
gcc
  • At UNIX prompt, type gcc hello.c o hello
  • If you get any compiler error messages
  • Figure out what they mean
  • Study and correct your source code
  • Repeat gcc hello.c o hello until you get no
    messages
  • If you get no output except a new prompt
  • The compiler has accepted your source code
  • You should now have a file named hello

3
Running your program hello
  • At UNIX prompt, type hello
  • If you get the printout Hello World! and a new
    prompt, your program ran successfully
  • If not,
  • Study any UNIX error messages for clues
  • Study your source code for logical errors
  • Probably logical errors - compiler didnt catch
  • Fix your source code and recompile / rerun

4
Handing in Assignments
  • Turn in assignments as hard copy of typescript
    file
  • script (Start recording typescript file)
  • Script started, file is typescript
  • ls l (list directory entries)
  • cat hello.c (display source file)
  • gcc hello.c o hello (compile source file)
  • hello (run executable file)
  • exit
  • script done on Thu Aug 22 143002 2002
  • lpr typescript (printout typescript file)

5
Debugging a C program error
  • There is a big difference between
  • The program compiling correctly
  • The program doing what you want it to do
  • You hope the compiler will catch your errors
  • These errors will be easier to find
  • If the compiler does not catch your errors
  • These errors will be harder to find

6
Compiler Error Messages
  • A compiler error message may direct you to a
    specific error in your program
  • A compiler error message may be vague about what
    the error is and why it is an error
  • Some compilers are better than others at
    providing useful error messages!

7
Compiler Error Messages
  • Introduced error is a missing closing brace
  • include ltstdio.hgt
  • int main ( )
  • printf("Hello, World!")
  • return 0
  • / missing /
  • gcc hello.c o hello
  • hello.c In function main'
  • hello.c6 parse error at end of input
  • Not a very helpful message Gotta figure it out!

8
Variables
  • Defined Data Type, Name, and ( value)
  • int lower 0 / Note and /
  • lower case by convention for readability
  • An executable statement
  • Memory location assigned to hold the value
  • Value can be changed as program executes
  • lower 20 / Legal /

9
Symbolic Constants
  • Defined Name and Value
  • define LOWER 0 / Note No or /
  • UPPER CASE by convention for readability
  • Not an executable statement
  • No memory location assigned to hold value
  • Value cant be changed as program executes
  • LOWER 20 / NOT Legal /

10
Example Program (KR, P 15)
  • include ltstdio.hgt
  • define LOWER 0 / Symbolic Constants /
  • define UPPER 300
  • define STEP 20
  • / Print out Fahrenheit Celsius Conversion
    Table /
  • int main ( )
  • int fahr / Variable /
  • for (fahr LOWER fahr lt UPPER fahr fahr
    STEP)
  • printf(3d,6.1f\n, fahr, (5.0/9.0)(fahr
    32))
  • return 0

11
For Statement
  • for (fahr LOWER fahr ltUPPER fahr fahr
    STEP)
  • statements within the loop
  • next statement after the loop terminates
  • for (A B C) - controls executing statement(s)
    within the loop
  • A is initialization (executed once when loop is
    started)
  • B is the loop test statement (when to stop
    looping)
  • C is a statement to execute at end of each loop

12
Printf statement
  • printf (3d, 6.1f\n, fahr, (5.0/9.0) (fahr -
    32))
  • First Argument to printf 3d, 6.1f\n
  • 3d integer format with 3 digits
  • 6.1f floating point format with 6 digits and
    1 decimal
  • \n end of line character just as in Hello
    World!
  • Second Argument to printf fahr
  • variable to print in 3d format
  • Third Argument to printf (5.0/9.0)(fahr
    32.0)
  • expression to calculate and print in 6.1f
    format
  • Note For open book tests, remember KR, Page 154!

13
printf formats
  • printf ("Values?3d,?6.1f\n", fahr,
    (5.0/9.0)(fahr-32))
  • Where the characters specified by "?" are what we
    write to show a space explicitly. Quoted string
    "Value . . ." is just like "Hello, world", but
    3d and 6.1f are special placeholders, called
    conversion specifications.
  • This means that the two expressions following the
    quoted string, fahr, and (5.0/9.0)(fahr-32), are
    to be printed according to the prescription
    given.
  • The table here would look like
  • Values???0,??-17.8
  • Values??20,???-6.7
  • Values??40,????4.4

14
printf formats
  • Other characters in "Values . . .", such as ","
    and "?" are printed literally.
  • The 3d means that an integer is printed so as to
    take up 3 spaces, right adjusted -- "?40", but
    no initial space for "100" -- still have space
    before 100 because came after "Values?".
  • The 6.1f means to print a float number (floating
    point or double by default, represented with a
    fractional part), with a total of 6 spaces used
    up and 1 digit after the decimal point thus
    "?-17.8" uses 6 spaces.

15
Functions
  • A function is a separate block of code that you
    can call as part of your program
  • A function executes and returns to next line
    after you call it in your program
  • Arguments within parentheses may be passed in
  • Arguments are passed by value!
  • function_name (arguments)
  • A return value may be passed back
  • return_value function_name (arguments)

16
Character I/O getchar( )
  • A standard function/macro defined in ltstdio.hgt
  • No arguments are passed to getchar ( )
  • getchar ( ) returns an int value from stdin
  • int c
  • c getchar ( )
  • Next character input is returned as value of c

17
Character I/O putchar( )
  • A standard function/macro defined in ltstdio.hgt
  • Character to print to stdout is passed as
    argument
  • There is no return value
  • int c
  • putchar (c)
  • Next character output is based on value of c

18
File Copying
  • include ltstdio.hgt
  • int main ( )
  • int c
  • c getchar( )
  • while (c ! EOF)
  • putchar (c)
  • c getchar ( )
  • return 0

19
File Copying - Alternative
  • include ltstdio.hgt
  • int main ( )
  • int c
  • while ( (c getchar( ) ) ! EOF)
  • putchar (c)
  • return 0

20
Redirecting stdin and stdout
  • copy lthello.c gtjunk.c
  • Copies
  • FROM
  • the contents of existing file hello.c as stdin
  • TO
  • the contents of a new file junk.c as stdout
  • (Note will overwrite any existing file junk.c)
Write a Comment
User Comments (0)
About PowerShow.com