Introduction
Arrays are multi-valued variables of a particular type.
- Declaring an array
type identifier[size];
int grade[4];
- Provides size memory locations to store values of the declared type
- Can declare arrays andsingle-valued variables within the
same declaration statement
int lcv, grade[4];
- Access elements of the array using an index or
a subscript
- Use array elements as you would single-valued variables
- Indices start at 0 and go thru size-1
- To assign the "first" element of the grade array a 75:
grade[0] = 75;
- Common Mistake:Accessing element N
To access the last element of the grade array, use grade[3]
The valid indices are 0, 1, 2, and 3 for the four element array
- Indices can be represented by anything of type int
float avgs[5];
int i;
i=1;
avgs[0] = 82.6;
avgs[i] = 91.4;
avgs[i+3] = 65.6;
- Can access using a for loop:
const int SIZE=20;
int i, grade[SIZE];
for (i =0; i< SIZE; i++)
cin >> grade[i];
- The above code took advantage of using a constant
- Array Initialization can be done within the declaration statement using { }
- To declare an array of 3 floating point numbers and intitalize:
float nums[3] = { 1.0, 2.4, 5.6 };
- When initializing all values, you can omit the size
float nums[] = { 1.0, 2.4, 5.6 };
- You can initialize all to 0.0 with this:
int grades[20] = {0};
- BUT this only works with 0
int grades[20] = {4}; // Only initializes the first to 4, rest are 0
Processing Arrays
Arrays in Functions
- Array elements can be passed to functions just like a single-valued variable
Given the prototype: void myFunction(float);
If the main program contains: float n, arr[3];
myFunction can be called with: myFunction(n);
or with: myFunction(arr[0]);
- Entire Arrays can be passed to functions;
void getnums(int [], const int); //prototype
void getnums(int arr[], const int SIZE) //header
{
int i;
cout << "Enter " << SIZE << "numbers: ";
for (i=0; i<SIZE; i++)
cin >> arr [i] ;
}
- If no value is given within the [ ] in the header, then
the size of the argument is used. This permits great flexibility:
the function can be used for different sized arrays.
- No & is used for pass by reference. Pass by reference is the default.
- To achieve pass-by-value with an array, use const
void printnums(const int [], const int); //prototype
void printnums(const int arr[], const int SIZE) //header
{
int i;
cout << "The numbers are " ;
for (i=0; i<SIZE; i++)
cout << arr [i] ;
}
- Functions can return arrays, but pointers are needed (later this semester)
BEGINNING NEW MATERIAL FOR CPSC246
Multi-dimensional Arrays
- Declaring
- int a[2][3];
- float b [4][2][3];
- Initializing
- int a[2][3] = {{ 1,2,3}, {4,5,6}};
- If not enough values, rest of row initialized to 0
- If too many, used in next row
- Used for array of strings
- char names [4][ ] = {"Sue, "Mary", "Sally", "George");
- Access using for loop
- int arayeg[2] [3] = { {1, 2, 3}, {4, 5, 6}};
- for (i = 0; i < 2; i++)
- for (j = 0; j < 3; j++)
- cout<< arrayeg[i][j] << endl;
Vectors
- A vector is a dynamic array available through the STL - standard template library
- It holds values of any type
- You do not need to set a size when you declare it - i.e., optional
- You can add elements to it and C++ automatically ads spaces as needed.
- Like an array [ ] are used to access elements
Basics for how to use a vector
- Include the vector library
- #include<vector>
- Declare a vector to hold a type:
- vector<int> myVector;
- Add an element to the vector usig the push_back method
- myVector.push_back (43);
- myVector.push_back (81);
- Find the value of an element of the vector using the at method
- x=myVector.at(0); // x is assigned 43
- Find the size of a vector;
- x=myVector.size(); // x is assigned 2
- Remove the last element of a function from the vector
- myVector.pop_back();
- x=myVector.size(); // x is now 1
More vector info
- You can declare a vector with a size:
- vector<int> myVector (100);
- You can intialize all elments to 0:
- vector<int> myVector (100, 0);
- You can initialize a vector to a list of values:
- vector<int> myVector {5, 4, 3, 2, 1};
This automatically gives it an initial size
- To remove all the elements of the vector use clear()
- myVector.clear();