70 likes | 177 Views
CLASS NOTES April 12, 2013. Prof. R. Lambiase, PE. Ens 117 Engineering computations. An array is a list of variables of the same type. That means we’ll need the know the following about an array:
E N D
CLASS NOTES April 12, 2013 Prof. R. Lambiase, PE Ens 117 Engineering computations
An array is a list of variables of the same type. That means we’ll need the know the following about an array: • The array name – This identifies it to the computer. Just as is the case with a simple variable, the computer will associate a memory location with this name. • The common data type – The data type can be int, char, double, or any other type we’ve been using for simple variables. You can even define an array of arrays. This creates a multi-dimensional array. • How many variables (elements of the array) are in the list – If there are N elements, these elements will be numbered 0 through N-1. Arrays
Arrays are declared in a fashion similar to simple variables, you need to declare the name, data type, and number of elements. double weight, density[10]; char type, material[10]; In this example, density[ ] is an array of 10 doubles, and material is an array of 10 characters. As with simple variables, the array elements can be initialized when declared. char type, material[10] = {‘A’, ‘B’, ‘I’, ‘S’}; Initialization starts with element 0, so it will have the value ‘A’, element 3 will have the value ‘S’, and elements 4 through 9 will not have any initial value. Declaring Arrays
An array element is used by placing an integer value in the square brackets following the array name. n = 4; speed[n]= 0.549 * speed[n-1]; In the example above, the value of element 4 of speed[ ] will become the value of element 3 times 0.549. Character arrays will have some special properties when treated as character strings, but for all other arrays, the elements must be assigned individually. As an example, if there are two arrays x[ ] and y[ ], the following syntax would be incorrect: x[ ] = y[ ]; Incorrect Syntax!!! Arrays are typically copied using a for loop. Using Arrays
The name of the array, without the square brackets, is a pointer to the first element. So for an array called Cap[ ], & Cap[0] is identical to Cap. We’ll use a function with pointers to input data into an array. boolGetData(double *x, int *y); double Cap[50]; int Max = 0; while(GetData(&Cap[Max], &Max)); As long as the function GetData( ) gets valid data, it will place that data in *x, increment *y, and return a true. If the data is not valid, it returns a false. When the while loop is completed, Max will contain the number of elements that were loaded into the array. Because all the functions in the while loop are performed by GetData( ), this is the rare case when it’s correct to place a semicolon at the end of the while statement. Pointers and Array Input
When we’re entering data, we don’t always know how many items will be entered. That’s why the while loop is generally chosen for this task. But once we’ve entered data, we know exactly how many items were entered. For those tasks, the for loop is generally the easiest. Continuing with the example from the previous slide, I’ll output all the elements that were just entered. for(k = 0; k < Max; k++) cout << Cap[k] << “\n”; The code above will print out all Max elements, number 0 to Max -1, and print them on their own line. In a similar fashion, if we wanted to copy the values in Cap[ ] to another array called Load[ ], we could do it as shown below: for(k = 0; k < Max; k++) Load[k] = Cap[k]; Arrays and the for Loop
Using array names as function parameters gives us the ability to make large amounts of data available to that function. Arrays and Functions