lecture8slides

lecture8slides - Wednesday, October 24th Todays Topics 1....

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, October 24 th Today’s Topics 1. Project #4 questions 2. Review of last class 3. More C strings! 4. Passing arrays to functions 5. Sorting arrays 6. Multi-dimensional arrays (if we have time) The Midterm is NEXT WEDNESDAY! REVIEW ALL OF THE EXAMPLES WE DID IN CLASS! ALL OF THEM. TYPE THEM IN! UNDERSTAND HOW THEY WORK! NO VOODOO! SYNTAX COUNTS ON THE EXAM! IT WILL NOT BE EASY! NO KIDDING.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What’s wrong with the following program? #include <iostream> #include <string> using namespace std; main() { string a; a[0] = 'H'; a[1] = 'i'; cout << a; } Answer: You may only access existing characters in a string: main() { string a=”abc”; a[0] = 'H'; // OK! a[1] = 'i'; // OK! cout << a; }
Background image of page 2
Review Examples What’s wrong with this program? void main(void) { int squared[4]; for (int i=1;i<=4;i++) squared[i] = i*i; } What’s wrong with this program? void main(void) { int squared[3] = {1,4,9}; int i, num_elem; num_elem = sizeof(squared) / sizeof(int) for (i = num_elem; i >= 0; i--) cout << squared[i]; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review: C strings A C string is just a character array with a bunch of characters followed by a 0 character. All C strings are character arrays. Only those character arrays that end with a 0 are C strings. main() { char str[10]; str[0] = ‘a’; str[1] = ‘b’; str[2] = 0; cout << str; cout << strlen(str); cout << sizeof(str); } Remember: cout prints the characters in the array until it hits the 0 character. The 0 is a sentinel value that means “End Of String.”
Background image of page 4
What’s wrong with this program? void main(void) { char goats[5]; goats[0] = 'B'; goats[1] = 'a'; goats[2] = 'a'; goats[3] = 'h'; goats[sizeof(goats)] = 0; cout << goats; } What’s wrong with this program? void main(void) { char pooped[4] = "HELL"; cout << pooped; }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constants and Arrays Can we do this? const int NUM_NERDS = 10; void main(void) { int grades[NUM_NERDS]; grades[0] = 99; // etc. . } How about this? const int NUM_BOYS = 24; const int NUM_GIRLS = 26; void main(void) { int grades[NUM_BOYS + NUM_GIRLS]; grades[0] = 99; // etc. . }
Background image of page 6
Which variables are valid strings? void main(void) { char a[4] = {'S','e','l','l'}; char b[5] = {'S','e','l','l',0}; char c[5] = {'S','e','l','l'}; char d[] = {'S','e','l','l'}; char e[] = {'S','e','l','l',0}; char f[] = "Sell"; cout << a << endl; cout << b << endl; cout << c << endl; cout << d << endl; cout << e << endl; cout << f << endl; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 27

lecture8slides - Wednesday, October 24th Todays Topics 1....

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online