1 / 24

Array

Array. What it is. How to use it How to declare it How to initialize it. What is an array? . Consecutive group of memory locations that all have the same name and data type. value. 100. grades[0] grades[1] grades[2] grades[3] grades[4]. 85. 50. 65. 88. name. position or

zev
Télécharger la présentation

Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 value 100 grades[0] grades[1] grades[2] grades[3] grades[4] 85 50 65 88 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

  4. Declaring an array Must tell compiler name, size and data type int grades[5]; char letters[26], numbers[10]; #define 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.

  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

  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!!

  8. Exercises Answer the following questions regarding an array called fractions. 1) Define a constant variable array_size and initialize it to 10. 2) Declare an array with arraysize elements of type float and initialize the elements to 0.0. 3) Name the fourth element from the beginning of the array. 4) Assign the value 1.667 to the 9th element in the array. 5) Print all the elements of the array using a for repetition structure.

  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;

  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 for (i = 0; i < 5; i++) { cout << grades[i] << endl; }

  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 for (i = 0; i < array_size; i++) { cout << grades[i] << endl; }

  12. Compare Example if (first == firstGuess) numHits++; else if ((first == secondGuess) || (first == thirdGuess)) numMatches++; if (second == secondGuess) numHits++; else if ((second == firstGuess) || (second == thirdGuess)) numMatches++; if (third == thirdGuess) numHits++; else if ((third == firstGuess) || (third == secondGuess)) numMatches++; What if you wanted to change the game to handle 5 or 10 letter sequences?

  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 1 1 * 2 1 * 3 1 * 4 2 ** 5 3 *** 6 4 **** 7 3 *** 8 3 *** 9 1 * 10 1 *

  18. arrays as function parameters • pass the name of the array without any brackets. • pass array size (optional) • always passed by reference

  19. arrays as function parameters Pass the name of the array without the brackets void PrintResults(int results[ ]); // function prototype void PrintResults(int results[ ]) // function definition { } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency);

  20. arrays as function parameters Pass array size void PrintResults(int results[ ], int size); void PrintResults(int results[ ], int size) { cout << "Rating " << " Frequency" << endl; for (int pos = 1; pos <= size; pos++) { cout << setw(3) << pos << setw (11) << results[pos] << endl; } } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange);

  21. arrays as function parameters always passed by reference void PrintResults(int results[ ], int size); void PrintResults(int results[ ], int size) { results[5] = 10; // does change caller’s data } calling sequence: const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange); cout << results[5] << endl; // displays 10;

  22. Use const to prevent function changes void PrintResults(const int results[ ], int size); void PrintResults(const int results[ ], int size) { results[5] = 10; // compiler error!! } calling sequence: // this doesn’t change const int maxRange = 10; int frequency[maxRange + 1] = {0}; PrintResults (frequency, maxRange);

  23. Entire arrays are passed by reference but not single elements const int maxRange = 10; int frequency[maxRange + 1] = {0}; void PrintResults( int results[ ], int size); calling sequence: PrintResults (frequency); results array was passed by reference. PrintResults could change it. void PrintResults (int element); calling sequence: PrintResults (frequency[4]); The 5th element of the array was passed by value not by reference. PrintResults cannot change it.

  24. Exercises 1) Write a function called FindAverage that accepts an array of integers as an input parameter and returns the average. 2) Find the errors in the following code: int PrintSum( const int values[ ], int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += values[i]; } values[0] = 100; values[size] = sum; }

More Related