CMPUT 201: Lab 2 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CMPUT 201: Lab 2

Description:

In C the special objects cin', cout', and cerr' are used for console I/O ... Arrays are 'ordinal' since every element has a clear predecessor and successor ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 16
Provided by: nla5
Category:
Tags: cmput | lab | ordinal

less

Transcript and Presenter's Notes

Title: CMPUT 201: Lab 2


1
CMPUT 201 Lab 2
I/O and Arrays
September 26th, 2002
2
C Input / Output Objects
  • In C the special objects cin, cout, and
    cerr are used for console I/O
  • cin is used for reading data from the standard
    input (the keyboard)
  • cout is used for sending program output to the
    standard output (the screen)
  • cerr is used for sending error messages to the
    standard output

3
cout Usage
  • Text strings and variable values are output to
    the screen with cout
  • cout statements have the following form
  • cout ltlt item1 ltlt item2 ltlt item3
  • The ltlt operator separates output items
  • Here an item is a text string or variable name
  • cout ltlt Number of files ltlt fileTotal
    ltlt endl

4
cout Escape Sequences
  • endl means a newline character. You can also
    use \n inside double quotes to specify a new
    line.
  • This is one of several escape sequences
  • Sequence Meaning
  • \n new line
  • \t horizontal tab
  • \\ display backslash
  • \ display single quote
  • \ display double quote

5
cout Number Formatting
  • For floating point (decimal number) outputs, it
    is desirable to display a select number of digits
  • Use setf function with flags to format output
  • Use precision to specify number of decimal
    digits to show
  • cout.setf(iosfixed)cout.setf(iosshowpoint)
    cout.precision(4)
  • This tells cout to show the decimal point
    followed by a fixed number of digits, in this
    case 4.

6
cerr Usage
  • Same object type as cout, therefore you use the
    exact same functions
  • cerr sends output data to the standard error
    stream (the screen by default)
  • Useful for distinguishing between normal
    program output and error messages
  • Can redirect cout so that a file contains
    program output and the monitor displays errors
    sent by cerr

7
cin Usage
  • Variable values are read in from the keyboard
    with cin
  • cin statements have the following form
  • cin gtgt studentID gtgt lastName
  • This reads input from two lines and places
    that input in the variables studentID and
    lastName.
  • The gtgt operator separates input items.

8
cin Functions
  • cin.get(char) reads a single character from
    input and stores it in char
  • Returns true when successful, false when end of
    file is reached
  • cin.putback(char) is useful when youve read a
    character but do not want to process it yet
  • This function places the value in char at the
    beginning of the input stream so that it may be
    processed later

9
Arrays in C
  • Arrays are collections of objects of the same
    type stored in consecutive memory
  • Arrays are declared with a specific data type
    and maximum size
  • int marks10
  • This creates an array of 10 integers.
  • Arrays are ordinal since every element has a
    clear predecessor and successor (e.g. element 2
    is after element 1 and before element 3)

10
Arrays in C
  • Arrays provide a common name for collections of
    variables. Individual variables are accessed
    with indexes (or subscripts)
  • Indexes range from 0 to arraySize 1, where
    arraySize represents the amount of memory
    assigned to the array
  • This means element 0 is actually the first
    element and element arraySize 1 is the last

11
Arrays in C
  • So the marks10 arrays elements are marks0,
    marks1, marks9
  • maxSoFar marks0
  • This accesses the first element in the marks
    array and stores it in the variable maxSoFar.
  • marks5 tempMark
  • This sets the sixth element in the marks array
    to the value in tempMark.

12
Structures in C
  • Structures are record types consisting of data
    items of different types
  • Individual items are referenced by field names
  • A structure is declared by listing its name and
    then all the fields it contains
  • struct Account String userId float
    quotaSize int monthlyHours

13
Structures in C
  • Elements are accessed by writing
    structureName.fieldName
  • Account customer1customer1.monthlyHours 50
  • This creates an Account record type named
    customer1 and stores the value 50 in its
    monthlyHours field.
  • noHours customer1.monthlyHours
  • This retrieves the value in the monthlyHours
    field and stores it in the variable noHours.

14
Command Line Arguments
  • Often its necessary to provide a program
    parameters (such as option flags) from the
    command line
  • This is done with command line arguments
  • programName arg1 arg2 arg3 ...
  • The main function in a C program can use
    two special variables argc and argv to access
    any command line arguments

15
Command Line Arguments
  • int main( int argc, char argv ) ... if
    (argc gt 1)
  • ...
  • argc contains the number of parameters, which
    includes the program name (so in a program called
    with one option, argc 2)
  • argv contains an array of strings that holds
    the arguments (so argv0 is the program name,
    argv1 the first parameter and so on)
Write a Comment
User Comments (0)
About PowerShow.com