int number;
memory address: 1001 1005 +---------+ | | +---------+ number
int list[5];
memory address: 1001 1005 1009 1013 1017 1021 +---------+---------+---------+---------+---------+ | | | | | | +---------+---------+---------+---------+---------+ LIST[0] LIST[1] LIST[2] LIST[3] LIST[4]
int list[67]; // an array of 67 integers indexed from 0 to 66 char string[20]; // an array of 20 characters indexed from 0 to 19 float price[30]; // an array of floating point numbers indexed // from 0 to 29
price[0] = 78.54; // Note, first element is index 0
price[5] = 100.25; // Note, this is the 6th element
cout << price[29];
float total = price[0] + price[1]; // add the first two values total += price[29]; // add the last value to the total price[15] *= 2; // double the price of the 16th value cout << price[6] - price[8]; // display the difference of two prices
// Simple array processing
#include <iostream.h>
void main( void )
{
int list[10]; // an array of 10 integers
int i; // used as an array index
i = 0; // always start at 0 for arrays
while( i < 10 ) {
cout << "Enter a whole number for element " << i << " ";
cin >> list[i]; // Note 1
i++;
}
cin >> list[i];reads a whole number typed at the keyboard by the user and stores it into element i of the array. The variable i takes on new values as the loop executes. For example, when i has a value of 6, the value entered at the keyboard will be stored into element 6 of the array. The statment will execute once for each value of i from 0 to 9 - thus filling the array.
// summing the array i = 0; // reset the index to start of array int total = 0; // to hold the array sum while( i < 10 ) { total += list[i]; i++; } cout << "The total of the values in the array is " << total << endl; }//end main
int numbers[5]={1, 2, 3, 4, 5};
int numbers[5]={1, 2, 3};array positions numbers[3] and numbers[4] are set equal to zero.
int numbers[]={1, 2, 3, 4};In this example, the array
numbers
will have 4
elements corresponding to the 4 initial values.
#includeint whatIsThis(int [], int); main() { const int arraySize = 10; int a[arraySize] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int result = whatIsThis(a, arraySize); cout << "Result is " << result << endl; return 0; } int whatIsThis(int b[], int size) { return b[size - 1]; }
Notice that following:
size
) so that the
function has access to that information.
float the_array[12];the number specified in the square brackets is:
char message[35]; int i = 0; while( i <= 35 ) { cout << message[i]; i++; }
// Passing arrays and individual array elements to functions #include#include void modifyArray(int [], int); void modifyElement(int); main() { const int arraySize = 5; int a[arraySize] = {0, 1, 2, 3, 4}; cout << "Effects of passing entire array call-by-reference:" << endl << endl << "The values of the original array are:" << endl; for (int i = 0; i < arraySize; i++) cout << a[i]; cout << endl; modifyArray(a, arraySize); // array a passed call-by-reference cout << "The values of the modified array are:" << endl; for (i = 0; i < arraySize; i++) cout << a[i]; cout << endl << endl << endl << "Effects of passing array element call-by-value:" << endl << endl << "The value of a[3] is " << a[3] << endl; modifyElement(a[3]); cout << "The value of a[3] is " << a[3] << endl; return 0; } void modifyArray(int b[], int sizeOfArray) { for (int j = 0; j < sizeOfArray; j++) b[j] *= 2; } void modifyElement(int e) { cout << "Value in modifyElement is " << (e *= 2) << endl; }