Introduction To C++

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Arrays

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

•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];
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]);
.
.

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

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;
}

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;

const int ROWS=10, COLS=10;


//prototypes
void Fill_Up(double[][COLS], int&, int&);
void Display(double[][COLS], int, int);
void Form_Transpose(double[][COLS], int, int, double[][COLS]);
int main()
{
double A[ROWS][COLS], A_Transpose[ROWS][COLS];
int row_count, col_count;
Fill_Up(A, row_count, col_count);
cout << "Matrix A (" << row_count << "rows, " << col_count << "columns)\n";
Display(A, row_count, col_count);
Form_Transpose(A, row_count, col_count, A_Transpose);
cout << "Matrix A_Transpose (" << col_count << "rows, ”
<< row_count << "columns)\n";
Display(A_Transpose, col_count, row_count);
return 0;
}//end main
Jeanine Ingber
Example
#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);
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;
}
Jeanine Ingber
Quiz

Jeanine Ingber

You might also like