180 likes | 272 Views
COMP104B Introduction to Comp Sci 2. Introduction and Revision 1. Housekeeping. Margaret Jefferies email: mjeff@cs.waikato.ac.nz Consultations by appointment Other important information is in the course outline. Housekeeping.
E N D
COMP104BIntroduction to Comp Sci 2 Introduction and Revision 1
Housekeeping Margaret Jefferies email: mjeff@cs.waikato.ac.nz Consultations by appointment Other important information is in the course outline
Housekeeping • Lecture notes will be handed out in lectures and will be available on the course web site. These are only a skeleton of the lecture • You will need to be at the lecture to get the full story • We will cover material for the practicals • We will cover material for the exam and go over sample questions • Most importantly you will learn how to program in C++
RevisionOne dimensional Arrays • What is an array? • When would you use an array? • Name two operations that are performed on arrays
One-dimensional arrays • Say I want to store the points the Chiefs scored in each of its Super 12 GamesWrite down a declaration for an array to do this
One-dimensional Arrays • We need to be able to assign values to each of the positions in this array. • The Chiefs scores in order were: 7, 18, 18, 6, 16, 40, 45, 26, 37, 28, 31 Write the code (an assignment statement) that will assign the score for third game.
One Dimensional ArraysInitialisation • Usually we should first initialise the array. That is give it some initial value that makes sense. • This involves repetition, we repeatedly assign the same value to all the positions in the array. • This means we use a ….. to do the initialisation • Write the code that initialises all the values in the Chiefs’ score array to zero.
At the end of the super 12 competition the array will have the following values Use the following code to print out the values, one per line for (i = 0; i <= 10; i++) cout << chiefs_scores[i] << endl; 7 18 18 6 16 40 45 26 37 28 31
One dimensional Arrays • Sometimes we need to know if a position in an array has been given a value or not • We need to know if the position is used or empty • How can we tell this?
Deleting an item from a One Dimensional Array • say you stored your book collection in array using the ISBN number long int book_collection[]; What’s a suitable initialisation value
Deleting an item from a One Dimensional Array long int book_collection[MAX]; int i; for (i = 0; i <= MAX; i++) book_collection[i] = 0;
Deleting an item from a One Dimensional Array book_collection[0] = 751506079; book_collection[1] = 6510272; book_collection[2] = 752834185; book_collection[3] = 718144163; book_collection[4] = 99175320; 751506079 6510272 752834185 718144163 99175320 0 ….
Deleting an item from a One Dimensional Array for (i = 0; i <= MAX; i++) if (book_collection[i] != 0) cout << book_collection[i] << endl; Prints out: 751506079 6510272 752834185 718144163 99175320
Deleting an item from a One Dimensional Array Delete book 3 book_collection[2] = 0; 751506079 6510272 0 718144163 99175320 0 ….
Deleting an item from a One Dimensional Array for (i = 0; i <= MAX; i++) if (book_collection[i] != 0) cout << book_collection[i] << endl; Prints out: 751506079 6510272 718144163 99175320
Strings - a special type of array • See chapter 9 of Bronsonchar str[20]; str = “this is a string” \0 is the string terminating character t h i s i s a s t r i n g \0
Strings - a special type of array • See chapter 9 of Bronsonchar str[20]; str = “this is a string”; ‘\0’ is the string terminating character the compiler inserts it for you t h i s i s a s t r i n g \0
One dimensional arraysStrings • char str[20] means we can have strings up to length 19. • Say we instead we had the declarationchar str[16];str = “this is a string”; • this is not a valid string because it does not have a terminating character t h i s i s a s t r i n g