UMBC CMSC 104 - Arrays, Part 1 of 2 (24 pages)

Previewing pages 1, 2, 23, 24 of 24 page document View the full content.
View Full Document

Arrays, Part 1 of 2



Previewing pages 1, 2, 23, 24 of actual document.

View the full content.
View Full Document
View Full Document

Arrays, Part 1 of 2

44 views

Lecture Notes


Pages:
24
School:
University of Maryland, Baltimore County
Course:
Cmsc 104 - Problem Solving and Computer Programming

Unformatted text preview:

Arrays Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration Initialization and Access Program Example Using Arrays Reading Sections 6 1 6 5 CMSC 104 Version 9 01 Data Types So far we have seen only simple data types such as int float and char Simple variables can hold only one value at any time during program execution although that value may change A data structure is a data type that can hold multiple values at the same time Synonyms complex data type composite data type The array is one kind of data structure CMSC 104 Version 9 01 Arrays An array is a group of related data items that all have the same name and the same data type Arrays can be of any data type we choose Arrays are static in that they remain the same size throughout program execution An array s data items are stored contiguously in memory Each of the data items is known as an element of the array Each element can be accessed individually CMSC 104 Version 9 01 Array Declaration and Initialization int numbers 5 The name of this array is numbers This declaration sets aside a chunk of memory that is big enough to hold 5 integers It does not initialize those memory locations to 0 or any other value They contain garbage Initializing an array may be done with an array initializer as in int numbers 5 5 2 6 9 3 numbers CMSC 104 Version 9 01 5 2 6 9 3 Accessing Array Elements Each element in an array has a subscript index associated with it numbers 5 0 2 1 6 2 9 3 3 4 Subscripts are integers and always begin at zero Values of individual elements can be accessed by indexing into the array For example printf The third element d n numbers 2 would give the output The third element 6 CMSC 104 Version 9 01 Accessing Array Elements con t A subscript can also be an expression that evaluates to an integer numbers a b 2 Caution It is a logical error when a subscript evaluates to a value that is out of range for the particular array Some systems will handle an out of range error



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Arrays, Part 1 of 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays, Part 1 of 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?