Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array What it is. How to use it How to declare it How to initialize it.

Similar presentations


Presentation on theme: "Array What it is. How to use it How to declare it How to initialize it."— Presentation transcript:

1 Array What it is. How to use it How to declare it How to initialize it

2 What is an array? Consecutive group of memory locations that all have the same name and data type 100 value grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 65 88 To refer to a particular element in an array we specify the name of the array and the position in the array. This array has 5 elements. name position or subscript

3 Referencing elements in an array
100 grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 65 88 To calculate the sum of the first 2 elements int sum = grades[0] + grades[1]; To change the value of the last element to 92 grades[4] = 92; position = subscript Note that in a 5 element array the subscript goes from 0 to 4.

4 Declaring an array Must tell compiler name, size and data type
int grades[5]; char letters[26], numbers[10]; ARRAY_SIZE = 5; int grades[ARRAY_ SIZE]; const int array_size = 5; int grades[array_size]; May only use constants to declare the size of an array. const int array_size 5 defines array_size to be a constant variable that cannot be changed. It will always hold the number 5. constant variables may be used to declare the size of an array since they don’t change.

5 Initializing an array int counters[5]; // Initialize array
for (int i = 0; i < 5; i++) counters[i] = 0; Note that i goes from 0 to 4 for a 5 element array not 1 to 5.

6 Initializing an array with a declaration
int grades [5] = {100, 85, 50, 65, 88} or int grades [] = {100, 85, 50, 65, 88} char numbers[] = {‘0’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’} int n[10] = {0} // entire array is initialized to 0 When there are fewer initializers than elements in the array the remaining elements are initialized to 0.

7 Common programming errors
Forgetting to initialize an array Too many initializers int grades [5] = {100, 85, 50, 65, 88, 99} // error!! Forgetting that the subscript range is 0 to (size - 1) and not 0 to size. int grades[5]; grades[5] = 2; // error!! grades[5] =2 is error because there is no index 5 only to 4

8 Exercises Answer the following questions regarding an array called fractions. Define a constant variable array_size and initialize it to 10. const int array_size = 10; 2) Declare an array with array_size elements of type float and initialize the elements to 0.0. float T[array_size]={0.0}; 3) Name the fourth element from the beginning of the array. T[3]; 4) Assign the value to the 9th element in the array. T[8]=1.667; 5) Print all the elements of the array using a for repetition structure. for(i=0;i< array_size-1;i++) cout<<“The element number “ << i+1<< “ is “<<T[i];

9 More Exercises Find the error in the following code segments
1) int k = 4; char a1[k]; 2) int a2[5] = {1, 2, 3, 4, 5, 6} 3) int a[5]; for (int i = 0; i < = 5; i++) a[i] = 0; 1) k must be a constant variable. Add const since it’s the size of the array 2) too many initializers 3) boundary condition problem. a[5] = 0; is changing memory location that is not part of the array.

10 scalability int grades[5]; // Initialize array
for (int i = 0; i < 5; i++) grades[i] = 0; // Read in 5 grades. for (i = 0; i < 5; i++) { cout << "Enter next grade: "; cin >> grades[i]; } // Display grades cout << grades[i] << endl; Manipulating array with for loop

11 Scalability const int array_size = 5; int grades[array_size];
// Initialize array for (int i = 0; i < array_size; i++) grades[i] = 0; // Read in 5 grades. for (i = 0; i < array_size; i++) { cout << "Enter next grade: "; cin >> grades[i]; } // Display grades cout << grades[i] << endl; To change this program to handle 100 grades instead of 5 just need to change one line.

12 Compare Example numHits++;
if (first == firstGuess) numHits++; else if ((first == secondGuess) || (first == thirdGuess)) numMatches++; if (second == secondGuess) else if ((second == firstGuess) || (second == thirdGuess)) if (third == thirdGuess) else if ((third == firstGuess) || (third == secondGuess)) What if you wanted to change the game to handle 5 or 10 letter sequences? This excerpt is from homework 3. This is part of the function the compare routine does.

13 Compare using arrays const int length = 3; // To handle 5 or 10 just change this char computerLetters [length]; char usersGuess[length]; int numHits = 0, numMatches = 0; for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (computerLetters[i] == usersGuess[j]) if (i == j) numHits++; else numMatches++; }

14 Survey Example Twenty students were asked to rate the quality of the food in the cafeteria on a scale of 1 to 10 with 10 meaning excellent. Place the twenty responses in an array of integers and summarize the results of the poll. Initialize responses array with student’s responses. Create another array to use to summarize their responses. subscript 1 in the array will contain a count of the number of students responses = 1, subscript 2 will contain a count of the number of student responses = 2, etc. Display responses in a table with 2 columns: Rating Frequency

15 Survey code const int numResponses = 20; const int maxRange = 10;
int responses [numResponses] = {1, 2, 6, 4, 8, 5, 9, 7, 10, 6, 5, 8, 7, 3, 4, 6, 7, 8, 5, 6}; int frequency[maxRange + 1] = {0}; int pos; for (pos = 0; pos < numResponses; pos++) { frequency[responses[pos]]++; } cout << "Rating " << " Frequency" << endl; for (pos = 1; pos <= maxRange; pos++) { cout << setw(3) << pos << setw (11) << frequency[pos] << endl;

16 survey code scalability
To change number of students surveyed const int numResponses = 40; To change scale of responses from 1 to 10 to 1 to 5. const int maxRange = 5;

17 Exercise Change the survey code to also print a histogram as follows:
Rating Frequency Histogram * * * ** *** **** *** *** * *


Download ppt "Array What it is. How to use it How to declare it How to initialize it."

Similar presentations


Ads by Google