1 / 9

Data Storage

Data Storage.

levi
Download Presentation

Data Storage

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. Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored? If you have 40 values to work with do you need 40 variables? To solve this issue there is a storage structure called ‘array’. An array can be likened to a list or a table that contains multiple values of like data types. A ‘single dimension’ array is like a list. Syntax of declaration statement: int grades[10]; • int is the data type of values stored in the array • 10 is the maximum number of items that can be stored and is referred to as the size of the array. • grades is the name of the array [like a variable name] and subject to the same naming rules as those for variables

  2. Things to Note About Arrays • The different values stored in an array are called ‘elements’ • Elements are referenced by the name of the array and a sequence number [Ex. grades[n] where n is an integer] • The first element is always element number 0! So grades[0] is the first grade stored in array grades and grades[5] is actually the 6th grade stored in the array. (Now you know why CS folk start counting at 0) • Array elements are stored sequentially in memory. • Arrays can be of any valid data type (double, float, char, int, etc.) • The array will occupy its size * sizeof(its data type) bytes of sequential memory.

  3. Visual Concept int grades[10]; // 10 is the size of the array Grades[0] grades[5] grades[9] 0, 5 and 9 above are each referred to as a subscript or an index Subscripts or indeces are used to reference a particular element in an array. Example of assignment statements involving arrays: i = 3; grades[i] = 88; grades[i + 1] = 100; grades[0] = 96; 0 1 2 3 4 5 6 7 8 9 grades[3] is referred to verbally as grades sub 3

  4. Example of Simple ProgramsUsing an Array for Data Storage(also – how to find the smallest or largest value in an array)

  5. Initializing Arrays Arrays can be initialized just like other variables. We will use this feature seldom in this course except for ‘array constants’ but you need to understand the nuances of array initialization. Syntax: data_typearray_name[size] = {element0, element1, …}; • The number of elements in {}s cannot exceed size. • The list of values can be less than size in which case the array is partially initialized. • If an array is partially initialized the part not initialized is always set to 0. • If an array is not initialized the memory storage contains just what other uninitialized variables contain – junk.

  6. Parallel Arrays Parallel arrays are groups of arrays having the same size which may or may not necessarily have the same data type. Data stored in the same subscripted cell in each array in the group are all related. Ex: intemployee_id[20]; float hrly_wage_rate[20]; boolfull_time[20]; employee_id[8] and hrly_wage_rate [8] and full_time[8] would all refer to the same employee.

  7. No No’s! • Take great care to not run off either end of an array • A loop is required to assign the values stored in one array to another array int array1[SIZE], array2[SIZE}; array 1 = array2; //NO NONO! • You CANNOT compare one array to another. You can only compare elements of the arrays if (array 1 == array2) // NO NONO! • You must step through the elements of an array to output it cout << array; // NO NONO!

  8. Stepping Through an Array with a Known Number of Valid Elements If you know how many elements are stored in an array a ‘for’ loop is the best control structure to use to access each element in the array. Ex. Suppose we want to output to the console the contents of an array. Via code earlier in the program we have determined that the array contains n valid data entries. for (intk = 0; k < n; ++k) cout << array[k] << endl;

  9. Reading an Unknown Number of Data Values into an Array To read an unknown number of values into an array from a file or the console you CANNOT use a ‘for’ loop. Either a ‘while’ or ‘do .. while’ must be used with a check to make sure that you do not try to store data outside the array storage area. Most often you will use a ‘while’ loop if a ‘for’ loop is not feasible. This loop should count the number of items actually stored in the array so that ‘for’ loops can be using for any further processing

More Related