Download presentation
Presentation is loading. Please wait.
Published bySydney Dorsey Modified over 9 years ago
1
COMP104B Introduction to Comp Sci 2 Introduction and Revision 1
2
Housekeeping Margaret Jefferies email: mjeff@cs.waikato.ac.nzmjeff@cs.waikato.ac.nz Consultations by appointment Other important information is in the course outline
3
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++
4
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
5
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
6
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.
7
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.
8
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; 718 616404526372831
9
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?
10
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
11
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;
12
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; 7515060796510272752834185718144163991753200 ….
13
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
14
Deleting an item from a One Dimensional Array Delete book 3 book_collection[2] = 0; 75150607965102720718144163991753200 ….
15
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
16
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
17
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
18
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.