1_Review_8-1 - 1 CIS 15AG Review Chapter 8: Arrays Array an...

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

View Full Document Right Arrow Icon
CIS 15AG Review Chapter 8: Arrays Array an internal collection of elements of the same type, called the base type of the array . Why use arrays (as opposed to individual variables)? - group a large amount of related data under a single name - easy processing using loops (shorter code, easier to understand and maintain) - pass a single name to a function instead of passing more individual elements (think of a function with 25 parameters instead of 1!) A linear list is a list in which each element has a unique successor except the last one. One way of implementing a linear list is using an array. What are the other ways of implementing linear lists? Are there lists that are not linear? (Of course there are! More about these in advanced courses. For now you should know that the array is the first data structure you learn about.) Arrays and Memory – a n array occupies contiguous memory; for instance, an array of 5 integers takes 5 * sizeof(int) bytes. Size or Length of an Array – its number of elements Fixed Length Arrays – the number of elements is a constant. Maximum Size – the maximum number of elements the array can store; not all of them might be used all the time. Actual Size – the actual number of elements the array holds; it has to be less than or equal to the maximum size (always to be checked!) Variable Length Arrays – the number of elements may be changed during the execution of the program. // it is a C99 feature (not required) Declare and Defining Arrays // type , name , and size (or length): the number of elements int scoreList[40]; char title[80]; double pricelist[100]; Another way to declare and define an array is to use a defined constant instead of a literal constant for its maximum size: #define MAX_SIZE 100; // . . . int aList[MAX_SIZE]; 1 0 1 2 3 4 98 76 84 100 95
Background image of page 1

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

View Full DocumentRight Arrow Icon
CIS 15AG Review Chapter 8: Arrays The Name of an Array – in C has a special meaning. We refer to an array by using its name, for instance aList , but aList is not technically an array itself. aList is a pointer constant: it represents the address of the first element in the list: aList and are the same! (You'll learn more about it later.) int aList[5] = { 98, 76, 84, 100, 95 }; Initializing Fixed Length Arrays int scoreList[5] = { 98, 76, 84, 100, 95 }; char gradeList[4] = { 'A', 'C', 'B', 'A' }; double priceList[9] = { 65.99, 24.75, 30.00 }; // partial initialization: the last 6 elements are set to ZERO int aList[10000] = {0}; // all elements are set to ZERO int bList[] = { 9, 8, 7, 5 ); // default size is 4 Accessing Elements in an Array – using an auxiliary variable called index that represents the location of the element in the array: the first one is at location 0, the second one is at location 1, and so on. The index must be of an integral type (it could be any
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

1_Review_8-1 - 1 CIS 15AG Review Chapter 8: Arrays Array an...

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

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