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

Info icon This 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 square6 A simple data type can only store one value at a time square6 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 square6 Many types of data structures exist (arrays, records, trees, etc.); one of the most common ones is the array square6 An array is a collection of data items of the same type square6 Example: double x[8]; this creates the array x with 8 array elements of type double , stored in adjacent memory cells
Image of page 1

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

View Full Document Right Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 9 Referencing arrays square6 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] square6 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 square6 The number of the array element is called the array subscript , or also the array index square6 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) square6 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
Image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 9 Manipulating array elements square6 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 square6 Often it is necessary to create two or more arrays to store related information, these are parallel arrays square6 Example: student ID and exam grade of the student #define NUM_STUDENTS 50 int id[NUM_STUDENTS]; double grade[NUM_STUDENTS]; square6 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
Image of page 3

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

View Full Document Right Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 9 Example of parallel arrays square6 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 82.1 97.3 74.8 88.7 91.2 84.5 [7] [6] [5] [4] [3] [2] [1] grade [0] SFTW120 - Programming Science 8 Lecture Notes 9 Array initialization square6 Any variable can be initialized (given an initial value) when it is declared; e.g.:
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern