COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

18
COMP104B Introduction to Comp Sci 2 Introduction and Revision 1

Transcript of COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

Page 1: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

COMP104BIntroduction to Comp Sci 2

Introduction and Revision 1

Page 2: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

Housekeeping

Margaret Jefferies

email: [email protected]

Consultations by appointment

Other important information is in the course outline

Page 3: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

Page 4: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

RevisionOne dimensional Arrays

(1) What is an array?

(2) When would you use an array?

(3) Name two operations that are performed on arrays

Page 5: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

Page 6: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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.

Page 7: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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.

Page 8: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

Page 9: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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?

Page 10: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

Page 11: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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;

Page 12: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

Page 13: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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:

751506079651027275283418571814416399175320

Page 14: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

Deleting an item from a One Dimensional Array

Delete book 3

book_collection[2] = 0;

751506079 6510272 0 718144163 99175320 0 ….

Page 15: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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:

751506079651027271814416399175320

Page 16: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

t h i s i s a s t r i n g \0

Page 17: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

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

t h i s i s a s t r i n g \0

Page 18: COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.

One dimensional arraysStrings

• 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

t h i s i s a s t r i n g