- An array is an indexed data structure that stores a collection of variables of the same data type.
- Individual variables within an array are called elements and are accessed using the array name and index.
- Arrays are declared with the data type, array name, and size. For example, int list[10] declares an array called list that can hold 10 integer elements.
- Elements can be initialized when the array is declared or assigned values later using a for loop. Arrays can also be passed as arguments to functions.
2. Jeanine Ingber
Arrays
An array is an indexed data structure
An array stores a collection of variables
All variables stored in an array are of the same
data type
An individual variable within an array is called an
element of the array
An element of an array is accessed using the array
name and an index
The name of the array is the address of the first
element. The index is the offset
3. Jeanine Ingber
Declaring an array
data 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
4. Jeanine Ingber
Declaring an array- Example
Example: 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[0]
list[1]
list[9]
5. Jeanine Ingber
Initializing Arrays
Arrays can be initialized at the time they are
declared.
Examples:
double taxrate[3] ={0.15, 0.25, 0.3};
char word[] = “hello”; //word has size 6
char list[5] = {‘h’,’e’,’l’,’l’,’o’};
//list of characters, not a string
double vector[100] = {0.0}; //assigns zero to all 100 elements
6. Jeanine Ingber
Assigning values to an array
•for loops are often used to assign values to an array
•Example:
int list[10];
for(int I=0; I<10; I++)
list[I] = I;
•Example
int count;
cin >> count;
for(int I=0; I<count; I++)
cin >> list[I];
7. Jeanine Ingber
Arrays As Arguments to
Functions
Individual elements of an array can be
passed as regular arguments. They can be
pass by reference or pass by value.
Example
void donothing(int&, int, int);//prototype
int main()
{
int array[5] = {1,2,3,4,5};
donothing(array[0], array[2], array[4]);
.
.
8. Jeanine Ingber
Passing Entire Arrays as
Arguments to Functions
Arrays are always pass by reference
The array name is the address of the first element
Arrays arguments that are not to be altered by the function
should use the modifier const in the prototype and the
function header
The maximum size of the array must be specified at the
time the array is declared. The actual number of array
elements that have values assigned will vary, so the actual
size of the array is often passed as an argument to the
function
9. Jeanine Ingber
Example
int maxval(const int array[], int actual_size); //prototype
const int MAXSIZE=100;
int main()
{
ifstream fin(“input”);
int numbers[MAXSIZE], temp, count=0, maximum;
fin >> temp;
while(!fin.eof() && count < MAXSIZE) //input data from file
{
numbers[count] = temp;
count++; //count number of entries
fin >> temp;
}
maximum = maxval(numbers, count);
cout << “The array has “ << count << “ values.nThe largest value is “
<< maximum << endl;
return 0;
}//end main
10. Jeanine Ingber
Function Definition for maxval
int maxval(const int array[], int actual_size)
{
int temp;
temp = array[0]; //initialize temp to first value in array
for(int I=1; I<actual_size; I++)
{
if(array[I] > temp)
temp = array[I];
}
return temp;
}//end maxval
12. Jeanine Ingber
Modifying arrays in functions -
Selection Sort
void selection_sort(int array[], int actual_size); //prototypes
int find_max_pos(const int array[], int size, int top);
void swap(int& v1, int& v2);
void get_data(istream& fin, int array[], int& count);
void print_data(const int[], int);
const int MAXSIZE=100;
int main()
{
int array[MAXSIZE], count;
get_data(cin,array, count);
cout << “the input data is “;
print_data(array, count);
selection_sort(array, count);
cout << “the sorted data is “;
print_data(array, count);
return 0;
}//end main
14. Jeanine Ingber
Function Definition for find_max_pos and
swap
int find_max_pos(const int array[], int actual_size, int top)
{
int temp;
temp = top; //initialize temp to index of first value in array
for(int I=top+1; I<actual_size; I++)
{
if(array[I] > array[temp])
temp = I;
}
return temp;
}//end maxval
void swap(int& n1, int& n2)
{
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
16. Jeanine Ingber
Two Dimensional Arrays
C++ supports multi-dimensional array
– data type array_name[row_size][column_size]
– int matrix[3][4];
row[0]
row[1]
row[2]
in memory
row1 row2 row3
17. Jeanine Ingber
Accessing Array Elements
int matrix[3][4];
– matrix has 12 integer elements
– matrix[0][0] element in first row, first column
– matrix[2][3] element in last row, last column
– matrix is the address of the first element
– matrix[1] is the address of the second row
20. Jeanine Ingber
Example
if(row_count == col_count)
{
cout << "Sum of elements along the main diagonal of A is "
<< Sum_Diag(A, row_count);
cout << "Sum of elements above the main diagonal of A is "
<< Sum_Above_Diag(A, row_count);
}//end if
return0;
}
#include<iostream>
using namespace std;
const int ROWS=10, COLS=10;
//prototypes
void Fill_Up(double[][COLS], int&, int&);
double Sum_Diag(const double[][COLS], int);
double Sum_Above_Diag( const double[][COLS], int);
int main()
{
double A[ROWS][COLS];
int row_count, col_count;
Fill_Up(A, row_count, col_count);