COMP104B Introduction to Comp Sci 2 Introduction and Revision 1
Housekeeping Margaret Jefferies 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++
Revision One dimensional Arrays (1)What is an array? (2)When would you use an array? (3)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 Games Write 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 Arrays Initialisation 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;
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] = ; book_collection[1] = ; book_collection[2] = ; book_collection[3] = ; book_collection[4] = ; ….
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:
Deleting an item from a One Dimensional Array Delete book 3 book_collection[2] = 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:
Strings - a special type of array See chapter 9 of Bronson char str[20]; str = “this is a string” \0 is the string terminating character thisisastring\0
Strings - a special type of array See chapter 9 of Bronson char str[20]; str = “this is a string”; ‘\0’ is the string terminating character the compiler inserts it for you thisisastring\0
One dimensional arrays Strings char str[20] means we can have strings up to length 19. Say we instead we had the declaration char str[16]; str = “this is a string”; this is not a valid string because it does not have a terminating character thisisastring