# Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.

## Presentation on theme: "Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer."— Presentation transcript:

Chapter 9 – One-Dimensional Numeric Arrays

Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer constant or expression following identifier –Subscript or index u Loops commonly used for manipulation Lesson 9.1

One-Dimensional Arrays u Declaration indicates name and reserves space for all elements u Values assigned to array elements using assignment statements u Array names classified as identifiers u Programmer sets size of array explicitly Lesson 9.1

Array Length u Determined by expression or value enclosed in brackets in declaration u General form for declaration type name[value]; u Value in brackets can be constant variable, expression, or literal Lesson 9.1 const int N = 26; double b[N]; int a[25]; int b[5+2];

Array Length u Must be integer constant greater than 0 u Only integer type variables (with modifiers) –int, char –signed, unsigned, short, long u Always within brackets following identifier u Examples: int c[32]; int c[-25], b[43.5]; Lesson 9.1 Valid Invalid

Array Subscripts u First index or subscript is 0 u int a[2]; –Data type of elements is int –Name of array is a –Number of elements is 2 –Valid subscripts are 0 and 1 u Address as a[0] and a[1] Lesson 9.1

Printing Array Elements u Use cout u Treat like single variable u Print one element at a time u Example: cout << "a[0] = " << a[0] << endl; Lesson 9.1

Initialization u In declarations enclosed in curly braces Lesson 9.2 int a[5] = {11,22}; Declares array a and initializes first two elements and all remaining set to zero int b[ ] = {1,2,8,9,5}; Declares array b and initializes all elements and sets the length of the array to 5

Working With Arrays u Remember subscripts must calculate to an integral value u Normally use loop to control array processing u Common loop is for loop –Start at zero –Continue while loop control variable < N t N is the size of the array Lesson 9.2

Using An Array u Declare int test[3]; –sets aside 3 storage locations u use index to reference the variables test[0] = 86; test[1] = 92; test[2] = 90; 869290 Lesson 9.2

Example u Declare and fill an array which will hold 5 double numbers double num[5]; for (int a = 0; a > num[a]; Initialize by reading from the keyboard Initialize by assigning values in loop num[a] = 0; Lesson 9.2

Input/Output u Typically use files u Reading from file, number of elements ? –Guard against reading past end of file –Use function eof( ) which returns 1 when end u Reading from file, number of elements known Lesson 9.3

Loop to Read Data Into an Array for (j = 0; !infile1.eof ( ); j++) { infile1 >> a[j]; } Example of for loop Lesson 9.3 Number of elements unknown!

Loop to Read Data Into an Array length = 0; infile >> data; while ((length < max_array_size) && ! infile.eof ( )) { a[length] = data; ++length; infile >> data; } Lesson 9.3 Example of while loop Number of elements unknown!

File Input with Known Number of Elements First line of file contains number of array elements. infile1 >> num_elem; for (j = 0; j < num_elem; j++) { infile1 >> a[j]; }

File Input – Sentinel Value u Particular predefined value contained in file that indicates end of data group u Loop and read data value by value until sentinel is read for (j = 0; a[j] != -1; j++) { infile1 >> a[j]; } Where –1 is the sentinel value.

Loop to Print Data From Array (scores is array of 20 test scores) for (int j = 0; j < 20; ++j) { cout << scores[ j ] << endl; } Lesson 9.3

Arrays and Functions u Pass address of array to function instead of element values u Function declaration –Data type and empty brackets int[ ] u Function call –Array name with no brackets x u Function header –Data type, name, and empty brackets int x[ ] Lesson 9.4

Passing Array Information u Three pieces of information –Size of single array element t Indicated by data type –Address of array t Indicated by array name in function call –Location to store array address t Indicated by identifier followed by brackets in function header Lesson 9.4

General Form Lesson 9.4 rtype function (type [ ], int); function (array, num); rtype function (type b[ ], int num_elem) Declaration Call Header Function return typeFunction name Type of values stored in array Type of second argument Array name in calling function Number of array elements b is identifier used to represent array within function num_elem used to represent number of array elements in function

Classes with Array Data Members u Automatically made accessible to public class member functions u Use enumeration to size data member arrays –Comma-separated list of identifiers –Identifier automatically assigned constant integer value –Value assigned depends on order in enumeration list Lesson 9.5

Enumeration u Keyword enum u Makes code more readable u Example: enum {sun = 1, mon, tues, wed, thur, fri, sat}; –Assigns integer constant 1 to sun, 2 to mon, etc. u Example of usage: int day; day = wed; Lesson 9.5 Same as day = 4;

Find Smallest Value in Array small = scores [0]; for (int j = 1; j < 20; ++j) { if (scores[ j ] < small) small = scores [ j ]; } Lesson 9.5

Arrays of Objects u Declared using class name, object name, and brackets enclosing integer constant Vehicle truck[3]; –Three objects (truck[0], truck[1], truck[2] of class Vehicle u Call member function –object, dot operator, and function name truck[0].set_data (50, 2, 3); Lesson 9.6

Arrays of Objects u Good when multiple pieces of information are linked u Use assignment statement to copy one object array element to another truck[1] = truck[0]; Lesson 9.6

Summary u Create and manipulate arrays u Trace and debug loops that manipulate arrays u Reserve memory during program execution u Use arrays to solve problems Learned how to:

Similar presentations