1 / 18

COMP104B Introduction to Comp Sci 2

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.

Download Presentation

COMP104B Introduction to Comp Sci 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP104BIntroduction to Comp Sci 2 Introduction and Revision 1

  2. Housekeeping Margaret Jefferies email: mjeff@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. RevisionOne dimensional Arrays • What is an array? • When would you use an array? • 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 GamesWrite 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 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.

  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; 7 18 18 6 16 40 45 26 37 28 31

  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; 751506079 6510272 752834185 718144163 99175320 0 ….

  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; 751506079 6510272 0 718144163 99175320 0 ….

  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 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

  17. 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

  18. 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

More Related