C++ Arrays
C++ Arrays
C++ Arrays
Outline
What is an array?
Declaring C++ arrays
Initializing one-dimensional arrays
Reading and Writing one-dimensional arrays
Passing one-dimensional arrays to functions
Manipulating one-dimensional arrays:
Searching for something
Rearranging stored values
Computing and storing series of values
What Is An Array?
An array is a collection of values that have the
same data type, e.g.
A collection of int data values or
A collection of bool data values
We refer to all stored values in an array by its
name
If we would like to access a particular value
stored in an array, we specify its index (i.e. its
position relative to the first array value)
The first array index is always 0
The second value is stored in index 1
Etc.
How To Declare Arrays
To declare an array in C++, you should specify the
following things
The data type of the values which will be stored in the array
The name of the array (i.e. a C++ identifier that will be used to
access and update the array values)
The dimensionality of the array:
One dimensional (i.e. list of values ),
Two-dimension array (a matrix or a table), etc.
The size of each dimension
Examples
int x[10]; // An integer array named x with size 10
float GPA[30]; // An array to store the GPA for 30 students
int studentScores[30][5]; // A two-dimensional array to store the
scores of 5 exams for 30 students
One-dimensional Arrays
We use one-dimensional (ID) arrays to store and
access list of data values in an easy way by
giving these values a common name, e.g.
int x[4]; // all values are named x 10 x[0]
x[0] = 10; // the 1st value is 10
nd
5 x[1]
x[1] = 5; // the 2 value is 5
x[2] = 20; // the 3rd value is 20 20 x[2]
void subtract (const int x[], const int y[], int z[], int n)
{
for (int i=0; i<n; i++) z[i] = x[i] - y[i];
}
Example: Set Intersection
void intersection (const int set1[], int size1,
const int set2[], int size2, int s[], int &size)
{
size=0;
for(int i=0; i<size1; i++)
if (seqSearch(set2, size2, set1[i])>-1)
set[size++] = set1[i];
}
Example: Set Difference
void difference (const int set1[], int size1,
const int set2[], int size2, int s[], int &size)
{
size=0;
for(int i=0; i<size1; i++)
if (seqSearch(set2, size2, set1[i]) == -1)
set[size++] = set1[i];
}
Example: Set Union
void union (const int set1[], int size1,
const int set2[], int size2, int s[], int &size)
{
size = size1;
for(int i=0; i<size; i++) set[i]=set1[i];
for(int i=0; i<size2; i++)
if (seqSearch(set, size, set2[i]) == -1)
set[size++] = set2[i];
}
Example: Converting An Integer
Decimal Number to Binary Number
#include <iomanip.h> {
void main() while (n>0)
{
short int binary[64]={0}; {
int index, bits=0; binary[bits] = n % 2;
unsigned int n; n /= 2;
bits++;
cout << "Please enter an integer value: ";
}
cin >> n;
Guess what happens when you enter -1!
cout << "The binary number is "; for (index=bits-1;index>=0;index--)
if (n==0) cout << binary[index];
cout << 0 << endl; cout << endl;
else
}
}
Example #2: Converting an integer
decimal number to binary number
#include <iomanip.h> {
void main() while (n>0)
{
short int binary[64]={0}; {
int index, bits=0; binary[bits] = n % 2;
unsigned int n; n /= 2;
bits++;
cout << "Please enter an integer value: ";
}
cin >> n;
Guess what happens when you enter -1!
cout << "The binary number is "; for (index=bits-1;index>=0;index--)
if (n==0) cout << binary[index];
cout << 0 << endl; cout << endl;
else
}
}
Example 4: Search for The Largest
Value in 1D Array
#include <iostream.h>