5. arrays

5. arrays - ENGG1002 Computer Programming and Applications...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays Very often, a program needs to handle a collection of data of the same type , e.g., scores of students in a class Using individually named variables to handle such data is cumbersome, especially for large datasets Arrays in C++ provide a convenient way to process such data An array behaves like a list of variables (of the same type ) with a uniform naming mechanism Each individual variable/element of an array is referred to by the name of the array and an index Example: 2 array name score[0] score[1] score[2] score[3] score[4] indexes
Background image of page 2
Arrays Like regular variables, arrays are implemented as memory locations The compiler will assign an appropriate number of consecutive memory cells in the main memory to an array according to the type of data to be stored the size of the array 3 1024-1027 1028-1031 1032-1035 1036-1039 1040-1043 1044-1047 1048-1051 1052-1055 1056-1059 1060-1063 1064-1067 score[0] score[1] score[2] score[3] score[4] Main memory An array of 5 integers
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declarations An array declaration specifies the base type, the name and the size of the array Examples: Arrays and regular variables can be declared together, e.g., 4 Syntax base_type array_name[size]; int score[5]; // an int array of 5 elements char grade[8]; // a char array of 8 elements double gpa[3]; // a double array of 3 elements int max_score, min_score, score[5], passing_score; The size of an array must be a constant and cannot be a variable
Background image of page 4
Initializations An array may be initialized in its declaration by using an equal sign followed by a list of values enclosed within a pair of braces { } Example: If an array is initialized in its declaration, the size of the array may be omitted and the array will automatically be declared to have the minimum size needed for the initialization values Example: 5 int score[5] = {80, 100, 63, 70, 52}; score[0] = 80 score[1] = 100 score[2] = 63 score[3] = 70 score[4] = 52 int score[] = {80, 100, 63}; Size equals 3: score[0] = 80 score[1] = 100 score[2] = 63
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Initializations The compiler will report an error if too many values are given in the initialization, e.g., It is, however, legal to provide fewer values than the number of elements in the initialization Those values will be used to initialize the first few elements The remaining elements will be initialized to a zero of the array base type Example: 6 int score[5] = {80, 100}; score[0] = 80 score[1] = 100 score[2] = 0 score[3] = 0 score[4] = 0 int score[5] = {80, 100, 63, 70, 52, 0};
Background image of page 6
Initializations It is illegal to initialize or change the content of the whole array using an equal sign after its declaration All the assignment statements below are therefore invalid 7 int score[5]; score = {80, 100, 63, 70, 52}; score[] = {80, 100, 63, 70, 52}; score[5] = {80, 100, 63, 70, 52};
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Indexed Variables Each element of an array can be regarded as a variable of the
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 31

5. arrays - ENGG1002 Computer Programming and Applications...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online