Upload
raymond-dyer
View
21
Download
0
Embed Size (px)
DESCRIPTION
Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a; } for(int j=0; j
Citation preview
Palindromes revisited
Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a; } for(int j=0; j<i; j++) if(nums[j] != nums[i-1-j]) { cout << "no" << endl; return 0; } cout << "yes" << endl;
Text Palindromes
Instead of using numbers, let's use letters, or chars.
Also, we will read in until the end of the user input,
rather than reading in until a sentinel value. The
expression: cin >> a; (where a is a char)
will return a value equivalent to true if a character
is read in and a value equivalent to false if
nothing is read in. We can use that in a test.
Palindrome Code char lets[100], a; int i = 0; while(cin >> a) lets[i++] = a; for(int j=0; j<i; j++) if(lets[j] != lets[i-1-j]) { cout << "no" << endl; return 0; } cout << "yes" << endl;
To indicate the end of input, the user would type ^Z
(in Windows) or ^D (in Unix).
Another Example
Here's the problem: Let's read in characters, just like
in the last program, but then print out a histogram
showing the number of a's, b's, c's,, ... read in. For
example, if the program read in the text:This is a test. This is only a test. In the event of an
actual emergency, you would be told where to tune on
your radio dial.
Example (cont'd)
a: *******b: *c: **d: ****e: ************f: *g: *h: ****i: ******j:
Example Code
We will need a counter for each letter, a – z.The counters should be initialized to 0.Every time a char is read in, if it is a letter (a-z, or
A-Z), the appropriate counter should be
incremented. This requires a read-to-end-of-input
loop.Once everything is read in, print out the histogram.
Converting chars to ints
We need to convert the chars read in to be ints, since
arrays are indexed by ints. This is easy to do in C+
+. Any arithmetic operation applied to a char
automatically converts it to an int. The value of the
char is given by the ASCII (American Standard
Code for Information Interchange) code. For
example, 'A' is 65, and 'a' is 97, but we don't need to
know that.
Converting chars to ints (cont'd)
What we need to know is how to convert 'a' to 0, and
'b' to 1, and 'c' to 3, ... (and likewise 'A' to 0 and 'B'
to 1, ...). The expression c – 'a' will
automatically convert the value of c to its ASCII
value, and also 'a' to its ASCII and subtract the
two values. This does exactly what we want for
lower case letters. c – 'A' works for upper case
letters.
Example Code
int count[26]; char c; for(int i=0; i<26; i++) count[i] = 0; while(cin >> c) if('a' <= c && c <= 'z') count[c - 'a']++; else if('A' <= c && c <= 'Z') count[c - 'Z']++; for(int i=0; i<26; i++) { cout << (char)(i+'a') << ": "; printStars(count[i]); }
More About Arrays
Arrays must be declared to be a fixed size (in most
compilers – in some, arrays which are local
variables, can be declared with a variable)Usually named constants are used to denote the
size: const int NUMCHARS = 26; int a[NUMCHARS]; Array always start at 0, not 1.Arrays may be initialized:
int primes[4] = {2, 3, 5, 7};
Using Indexed Variables as Args.
Index variables (array elements) may be used as
arguments in function calls.They may correspond to either call-by-value or call-
by-reference parameters.If the indexed variable has a variable subscript, say
a[i], the subscript is evaluated when the function
is called, and that data object is used.
Using Whole Arrays as Arguments
A whole array may be used an argument in a
function call.Whole arrays are always call-by-reference
parameters, even without writing the “&” in the
parameter list.A parameter which is an array variable doesn't
cause a data object to be created – it becomes
another name of the array argument. No need to give the size of the array parameter.
Example
int foo(int x[], int size);
int main(void) { int a[10], s = 10; // read in array a; foo(a,s);}
Exercise
Write a function, maxElement, that is passed an
array of ints, a, and the number of elements in that
array, size, and returns the maximum element of
the array.