Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.

Similar presentations


Presentation on theme: "Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the."— Presentation transcript:

1 Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the array, and is distinguished from the other elements by using an index number (or subscript). An element of an array is just like a regular variable of the same type.

2 Declaring Arrays Basic Declaration: int values[10]; double rates[5];
char lastName[21]; Declaring and Initializing the Elements int nums[7] = { 22, 56, 38, 14, 0, 1854, 971 }; char lastName[6] = {'W', 'e', 'i', 'n', 'e', 'r'}; double temps[4] = ( 22.7, , -300, };

3 Declaring Arrays Special Declaring and Initializing
int nums[] = { 22, 56, 38, 14 }; char name[] = {‘W’, ‘e’, ‘i’, ‘n’, ‘e’, ‘r’}; double values[10] = { 14.7, , }; double values[10] = { 0 }; You can't declare with empty braces if you don't have an initialization list. When you declare more elements than you have values in the initialization list, all other elements are initialized to a zero value.

4 cout << "The value is: " << values[5] << endl;
int values[8] = { 22, 786, 13, 44, 0, -121, 98, 1432 }; values[0] = 55; cout << "The value is: " << values[5] << endl; values[7]++; values[0] += values[1]; cin >> values[1]; Anything I can do to an int variable, I can do to an element of an int array.

5 values[count * 2] = values[1];
int values[8] = { 22, 786, 13, 44, 0, -121, 98, 1432 }; int count = 3; values[count] = 17; values[count + 1] = 186; values[count * 2] = values[1]; for( count = 0; count < 8; count++ ) values[count] += 6; for( count = 0; count < 8; count += 2 ) values[count] *= values[count]; The most powerful aspect of arrays is that you can specify an element with a variable or an expression. Process different elements of an array in a loop. NEXT: Standard applications of arrays array_total.cpp array_highest.cpp array_count.cpp array_find.cpp array_overrun.cpp

6 Using Parallel Arrays Two or more arrays where the same elements of each array are related to each other. For example, one array is a list of employee ID's. Another array is a list of the number of hours that each person worked. Element 0 of the first array identifies an employee. Element 0 of the second array is the number of hours that employee worked. Search the first array for a particular employee. When found, the same element number in the second array is the number of hours (s)he worked. Draw it on the board. array_parallel.cpp

7 Using Parallel Arrays ids hours 1 2 3 4 5 1111 1 2 3 4 5 43.25 2323
1 2 3 4 5 1111 1 2 3 4 5 43.25 2323 37.5 1781 40.0 6775 45.5 3217 22.75 4002 37.5


Download ppt "Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the."

Similar presentations


Ads by Google