#9 Arrays - Lecture#9 ARRAYS What is an array? An array is...

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

View Full Document Right Arrow Icon
Lecture #9 ARRAYS  What is an array? An array is a group of elements with the same data type (i.e., it is homogenous) An array is characterized by its name , dimension , size and element data type An array has a dimension which we will denote by d -when d = 1 , we say that the array is 1-dimensional; it is also referred to as a list -when d = 2 , we say that the array is 2-dimensional; it is also referred to as a table How do you declare a one-dimensional array in C? The syntax is: <data type> <array name> [<size>] Examples: char s[255]; int a[10]; int MyArray[100], M[50]; float X[25]; double Data[1000]; Key Point! -array elements, if not explicitly initialized, will have garbage values How do you reference an element in a one-dimensional array? The syntax is: <array name> [<index>] i.e., simply type the array name, followed by the left square bracket, followed by the array element index and finally by the corresponding right square bracket. Key Points! -the range of values that you can use as array index is from 0 to <size> -1 -the first element therefore is <array name>[0] -the last element is <array name>[<size> -1] Examples of valid array element references are:
Background image of page 1

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

View Full DocumentRight Arrow Icon
s[0] a[5] MyArray[5] Examples of invalid array element references are: s[-1] // index cannot be negative X[2.5] // index cannot be a real number Data[1000] // last index should size -1 What operations can you perform on arrays? Only operations on a per-array element basis are possible. Operations such as array to array assignment or adding two arrays are not supported by now. There are basically two operations that can be performed on array elements, namely: ° read operation, specifically, read the content of an array element ° write operation, specifically, overwrite the original content of an array element Examples of read operations are: cout<< a[1]; // assume that i was declared as an int variable for (i = 0; i < 1000; i++) cout<< Data[i]; Examples of write operations are: s[0] = ‘c’; X[2] = 3.1416; // assume that i was declared as an int variable
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.

This note was uploaded on 06/14/2011 for the course COMPUTER 091 taught by Professor Rajivsir during the Summer '11 term at MIT.

Page1 / 6

#9 Arrays - Lecture#9 ARRAYS What is an array? An array is...

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