View
213
Download
0
Embed Size (px)
Citation preview
Friday, December 29, 2006
Should array indices start at 0 or 1? My compromise of 0.5 was rejected
without, I thought, proper consideration.
- Stan Kelly-Bootle
Declaring an array
type array_name[size]; allocates memory for size variables index of first element is 0 index of last element is size-1 size must be a constant
Declaring an array- ExampleExample: int list[10];
allocates memory for 10 integer variables index of first element is 0 index of last element is 9 C++ does not perform any bounds checking on arrays
list[1]
list[0]
list[9]
Initializing Arrays
Arrays can be initialized at the time they are declared.
Examples:
double taxrate[3] ={0.15, 0.25, 0.3};
int num[10] ={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Initializing ArraysWhat is wrong with the following?
int num=10;
int iarray[num];
int size;
cin>>size;
int myArray[size];
double dArray[5]={3.2, 4.5, 6.7, 324.0, 45.8, 23.1, 34.9};
Assigning values to an array•Example
int list[10];int count, i;cin >> count;for(int i=0; i<count; i++)
cin >> list[i];
•for loops are often used to assign values to an array
Assigning values to an array•Example
int list[10];int count, i;cin >> count;for(int i=0; i<count; i++)
cin >> list[i];
What if count >9?
Assigning values to an arrayint main() { // this reserves 10 integer elements
int sample[10]; int t; // load the array for(t=0; t<10; ++t)
sample[t]=t; // display the array for(t=0; t<10; ++t)
cout << sample[t] <<“ “; return 0;} //output ?
Summarize ArraysAn array is an indexed data structure
An element of an array is accessed using the array name and an index
An array stores a collection of variablesAll variables stored in an array are of the
same data typeAn individual variable within an array is
called an element of the array
Summarize Arrays In C++ all arrays are stored in contiguous memory
locations
An index describes the position of an element within an array.
In C++ all arrays have zero as the index of their first element.
The name of the array is the address of the first element. The index is the offset
Arrays Total size of an array in bytes?Total bytes = number of bytes in type x number of
elementse.g. int sample[10];
sizeof operator
Arrays int a[10];
cout<<sizeof(a)<<endl;
cout<<sizeof(a[0])<<endl;
cout<<sizeof(int)<<endl;
double d[10];
cout<<sizeof(d)<<endl;
cout<<sizeof(d[0])<<endl;
cout<<sizeof(double)<<endl;
Arrays int main()
{
int i, min_value, max_value;
int list[10];
for(i=0; i<10; i++){
list[i] = rand();
cout<<list[i]<<" ";
}
Arrays // find minimum value
min_value = list[0];
for(i=0; i<10; i++) {
if(min_value>list[i]){
min_value = list[i];
}
}
cout << "minimum value: " << min_value << “\n”;
Arrays // find maximum value
max_value = list[0];
for(i=0; i<10; i++){
if(max_value<list[i]){
max_value = list[i];
}
}
cout << "maximum value: " << max_value << '\n';
return 0;
}
Arrays int a[10], b[10];
// ...
a = b; // error -- illegal
/*So how do we make the contents of one array same as the other?*/
Arrays // An incorrect program.
int main()
{
int crash[10], i;
for(i=0; i<100; i++)
crash[i]=i;
return 0;
}
Arrays - No bounds checking // An incorrect program.
int main()
{
int crash[10], i;
for(i=0; i<100; i++)
crash[i]=i;
return 0;
}