SFTW120-LectureNotes-09

SFTW120-LectureNotes-09 - SFTW120 Programming Science...

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

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 9: C: Arrays (Textbook Chapter 8) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 9 Arrays s A simple data type can only store one value at a time s Often it is useful to group several related data items together into a data structure : a composite of data items stored under the same name s Many types of data structures exist (arrays, records, trees, etc.); one of the most common ones is the array s An array is a collection of data items of the same type s Example: double x[8]; this creates the array x with 8 array elements of type double , stored in adjacent memory cells
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 9 Referencing arrays s The array created by double x[8] with some numbers stored in it: -54.5 14.0 12.0 2.5 8.0 6.0 12.0 16.0 x[7] x[6] x[5] x[4] x[3] x[2] x[1] x[0] s Each element of the array can be referenced by giving the name of the whole array, and the number of the element in square brackets; e.g.: x[0] array element 0 (first element) x[1] array element 1 (second element) SFTW120 - Programming Science 4 Lecture Notes 9 Array subscripts s The number of the array element is called the array subscript , or also the array index s Array subscripts must be in the range of the array they refer to, from 0 to one less than the size of the array; e.g. for the array double x[8] , the array subscript can be from 0 to 7 (which is 8 numbers) s If an array subscript outside the valid range of the array is used, a memory cell outside the array will be accessed; the result is unpredictable, therefore the programmer has to be very careful to make sure that the array subscript is always in the valid range
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 9 Manipulating array elements s Individual array elements can be manipulated (used, changed) just like other variables; e.g. for array x : -54.5 14.0 12.0 2.5 8.0 6.0 12.0 16.0 x[7] x[6] x[5] x[4] x[3] x[2] x[1] x[0] printf("%.1f", x[0]); displays x[0] as 16.0 x[3] = 25.0; changes x[3] to 25.0 sum = x[0] + x[1]; new value of sum is 28.0 sum += x[2]; new value of sum is 34.0 x[3] += 1.0; changes x[3] to 26.0 x[2] = x[0] + x[1]; changes x[2] to 28.0 SFTW120 - Programming Science 6 Lecture Notes 9 Parallel arrays s Often it is necessary to create two or more arrays to store related information, these are parallel arrays s Example: student ID and exam grade of the student #define NUM_STUDENTS 50 int id[NUM_STUDENTS]; double grade[NUM_STUDENTS]; s The arrays have the same length, and the same array subscript is used to access data of the same student; from 0 for the first student to 49 for the last student: id[0] = 1234; set the first student’s ID to 1234 grade[0] = 84.5; set the first student’s grade to 84.5
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 9 Example of parallel arrays s Two (or more) arrays with the same length but different content: 1241 1240 1239 1238 1237 1236 1235 1234 [7] [6] [5] [4] [3] [2] [1] id [0] 87.3 79.6
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/04/2011 for the course SFST 120 taught by Professor Dr.robert during the Spring '11 term at University of Macau.

Page1 / 21

SFTW120-LectureNotes-09 - SFTW120 Programming Science...

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

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