Introduction To C++
Introduction To C++
Introduction To C++
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
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
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]
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
Jeanine Ingber
Assigning values to an array
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
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
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
Jeanine Ingber
Quiz
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
Jeanine Ingber
Function Definition -
Selection Sort
void selection_sort(int array[], int size)
{
int max_pos;
for(int i=0; i<size-1; i++)
{
max_pos = find_max_pos(array, size, i);
swap(array[i], array[max_pos];
}
}//end selection_sort
Jeanine Ingber
Function Definition for find_max_pos and
swap
Jeanine Ingber
Selection Sort Illustrated
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
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
Jeanine Ingber
2-Dimensional Arrays
As arguments to functions
– column size must be declared in prototype and
header
Jeanine Ingber
Programming Example
#include<iostream>
using namespace std;
Jeanine Ingber