A1256654067XtcnG - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

A1256654067XtcnG

Description:

How do you make a verbatim copy of a file, including all the whitespaces ? ... For a verbatim copy. read and record every character, including whitespace. ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 35
Provided by: ericse
Category:

less

Transcript and Presenter's Notes

Title: A1256654067XtcnG


1

CS 215 Introduction to Programming with
C Instructor Eric Sedgwick
2
This Time
  • Program design and testing
  • Input and Output streams
  • file I/O
  • Functions calling functions
  • recursion

3
This Time
  • I/O streams (continued)
  • character I/O
  • Introduction to Arrays (Chapter 9)

4
Question
  • How do you make a verbatim copy of a file,
    including all the whitespaces ?
  • Can you do this with ltlt and gtgt ?

5
Recall the purpose of I/O
  • To convert between characters and other data
    types
  • gtgt does this automatically
  • gtgt forgets whitespace in the input stream
  • Spaces
  • Newlines (\n)
  • Tabs (\t)

6
For a verbatim copy
  • read and record every character, including
    whitespace.
  • To read every character, use the input stream
    member function get
  • char symbol
  • cin.get(symbol) // call-by-ref
  • There is a corresponding output stream function
    put, but we dont really need it.

7
What are c1,c2,c3,c4?
  • Input stream in_stream
  • Aa
  • Bb
  • Execute this code
  • char c1,c2,c3,c4
  • in_stream.get(c1)
  • in_stream.get(c2)
  • in_stream.get(c3)
  • in_stream.get(c4)

8
Pitfall
  • Forgetting about the newline character, \n.
  • It can be used to your advantage to detect the
    end of the line. You will need this for your hw!

9
The eof() function
  • Returns true when you have tried to read past the
    last thing in the file. If true you have already
    one too many characters.
  • To copy from in_stream to out_stream
  • in_stream.get(symbol)
  • while (!in_stream.eof())
  • out_stream ltlt symbol
  • in_stream.get(symbol)
  • verbatim_copy.cpp

10
Predefined character functions
  • Some character functions, more on pp. 918-919
  • ctoupper(z) // cZ
  • ctolower(H) // ch
  • islower(z) // returns true
  • isupper(b) // returns false
  • isalpha() // returns false
  • isdigit(c) // returns false
  • isspace() // returns false
  • Requires include ltcctypegt

11
Careful !
  • toupper and tolower return ints that can be
    converted to chars
  • char c
  • c tolower(Z)
  • cout ltlt c Output z
  • cout ltlt tolower(Z) Output 90
  • cout ltlt char(tolower(Z)) Output z

12
split_copy.cpp
  • How do we change verbatim_copy.cpp to put
    letters in one file and numbers in another ?
    (Everything else should go to both)

13
gtgt vs. get
  • When should you use gtgt and when should you use
    get ?
  • Use get when you care about every character in
    the input file (especially spaces).
  • Be careful about \n, \t etc.
  • Otherwise use gtgt

14
Arrays
15
Problem
  • Write a program that
  • gets n (say 5) numbers from the user,
  • calculates the minimum of those numbers, and
  • Prints out how far each is above the minimum
  • Example
  • Input 65 66 63 70 69
  • Output 2 3 0 7 6
  • Problem Need to access each data item twice.

16
Solution?
  • This would work (if you had a min(a,b)function)
  • int a0,a1,a2,a3,a4
  • cin gtgt a0 gtgt a1 gtgt a2 gtgt a3 gtgt a4
  • minimum min(a0,min(a1,min(a2,min(a3,a4))))
  • cout ltlt a0-minimum
  • cout ltlt a1-minimum
  • Problem
  • What if you wanted to compute for 100 numbers?
  • Not practical to extend this code.

17
Solution
  • Use an array!int score5
  • This declares 5 integer variables, with names
  • score0,score1,score2,score3,score4
  • The base type of the array is int.
  • The size of the array is 5.
  • Very Important! Arrays are numbered 0,..,size-1

18
Indexing
  • Each item can be treated individually as an
    integer variable by supplying an index, the
    number of the particular array item you want to
    access
  • score05
  • score1 score2-1 score3
  • cout ltlt score3 ltlt score1
  • The index can be a variable, it is evaluated
  • student3
  • scorestudent 99 // same as
  • score3 99

19
Terminology
  • scoresi1 7
  • scores is the name of the array
  • scoresi1 is an element of the array
  • i1 is the index of that array element
  • 7 is the value of that element of the array

20
Arrays and Loops
  • To read in all 5 numbers, you could (dont do
    this)
  • cin gtgt score0 gtgt score1 gtgt score2
  • gtgt score3 gtgt score4
  • Or, use a loop. This is more powerful.
  • int count
  • for(count0countlt5count)
  • cin gtgt scorecount
  • Be consistent, always write it like this
  • for(count0countltsizecount)

21
Minimum.cpp
  • Gets n (say 5) numbers from the user,
  • Calculates the minimum of those numbers, and
  • Prints out how far each is above the minimum
  • What happens if we read 6 numbers?

22
Arrays in Memory
  • To declare a non-indexed variable in memory, two
    pieces of information are needed
  • Address, the first byte of variable
  • Type, determines how many bytes are used
  • For indexed variables (arrays), three items are
    needed
  • Address, the first byte of variable
  • Type, determines how many bytes are used for each
    element of the array.
  • Size, number of elements in the array.

23
Arrays are dangerous
  • They are dangerous because of the way that they
    are stored and referred to in memory.
  • You can address memory that you shouldnt.
  • This is called an index out of range error
  • Unlike previous errors, this can have
    consequences outside your program
  • Why isnt the compiler more careful?
  • Compile-time vs. run-time checking

24
Arrays in memory
Addresses
  • stored contiguously
  • Variables are referenced by their addresses
  • E.g, Score2 is the int variable that is
    2length(int) bytes further than score0.

4021
Score0
4023
Score1
4025
Score2
4027
Score3
4029
Score4
Score5
Score6
Other stuff, what?
Very very bad!!
25
Programming Tip
  • Make the maximum array size a global constant and
    always refer to this constant.
  • Makes it easy to adapt your program for larger
    (smaller inputs)
  • E.g.,
  • const int MAX_SIZE 100
  • double scoreMAX_SIZE
  • for(i0iltMAX_SIZEi)
  • cin ltlt scorei

26
Initializing arrays
  • Elements one at a time. Suppose MAX_SIZE 3
  • int ageMAX_SIZE
  • age0 21
  • age1 19
  • age2 35
  • All together, this is equivalent
  • int ageMAX_SIZE 21,19,35
  • With a loop
  • For(int i0iltMAX_SIZEi)
  • agei 0

27
Setting arrays equal
  • This is no good
  • int scoreMAX_SIZE, gradeMAX_SIZE
  • Score grade // will not compile
  • Score grade // still no good
  • Score0 grade0 // fine, but 1 element
  • You must step through the arrays with a loop
  • int scoreMAX_SIZE, gradeMAX_SIZE
  • for(i0iltMAX_SIZEi)
  • scorei gradei // element by element

28
Arrays and functions
  • Array elements as call-by-value arguments
  • Array elements as call-by-reference arguments
  • Entire arrays as arguments

29
Array elements as call-by-value arguments
  • square.cpp
  • This is nothing new, each array element is a
    variable of the given type.

30
Class Exercise
  • Write a stat program that
  • Reads in a list of scores from the user
  • calculates the minimum score
  • Calculates the average
  • Calculates the maximum score
  • Each of these tasks should be functions
  • stat.cpp

31
Entire arrays as arguments
  • When passing an array, the parameter list must
    contain two items. In our examples (stat.cpp)
  • num an array parameter
  • Brackets signify an array
  • Similar to a call-by-ref, the base type of the
    array and address to the 1st element are passed
    to the function
  • size, a call-by-value parameter
  • The number of items to be manipulated
  • Necessary to prevent index out of range

32
const parameter modifier
  • If your function does not modify the value of the
    array, have the compiler make sure that it does
    not happen accidentally.
  • int min(const int num, int size)
  • Make the compiler work for you

33
Class Exercise
  • Modify stat.cpp to calculate the stats of
    integers from a file.
  • The number of integers is known to be less than
    100, but otherwise unknown.
  • This needs to be kept track of.
  • What needs to change?
  • file_stat.cpp

34
Partially filled arrays
  • Need a variable to keep track of the actual size
    of the array.
  • Pass the actual size to the functions instead of
    MAX_SIZE.
  • Make sure that the actual size stays less than
    MAX_SIZE, otherwise trouble
  • With strings we will see another solution to this
    problem ...
Write a Comment
User Comments (0)
About PowerShow.com