ARRAYS IN C++
How to create arrays in C++
Finding the value at a given index in C++
Using For Loops To Cycle through an array
Passing an array into a function
Passing higher-dimensional arrays into a function
Arrays are always passed by reference
How to create arrays in C++
Arrays in C++ work much like Arrays in Java. They are 0 indexed (meaning their index values start at 0). The following are all ways we can create an integer array in C++
int x[10]; //creates an integer array of 10 elements. None of these elements are initialized
int x[]={3,4,5}; //creates an integer array of 3 elements with x[0]=3,x[1]=4, x[2]=5. Note that we don't have to specify the size
You can also create arrays of doubles and any of the other types you have encountered thus far.
A 2-dimensional array can be created as follows
int x[2][4]; //a 2D array of 8 elements (2 in one direction, 4 in the other)
You can create a 3D array in the same way.
Finding the value at a given index in C++
Same as java!
cout<<x[3]; //will give you the 3rd index, 4th value of the x array.
Do not overrun the end of your array by putting an index value that's bigger than the length of the array. In C++ this could lead to a segmentation fault or, the program may run but do something funny. The result of overrunning the end of an array is undetermined.
Using For Loops To Cycle through an array
The following code initializes all the elements of an array to 0. This should be review. Note that we put the length of the array as the bounds of the for loop
int x[10];
for (int i=0; i<10;i++)
{
x[10]=0;
}
Passing an array into a function
The following code features a function that adds one to every element of an int array
Click here to run it
#include
using namespace std;
void addOneToEachElement(int x[],int length);
void printArray(int x[], int length);
int main() {
int a[10]={0,1,2,3,4,5,6,7,8,9};
addOneToEachElement(a, 10);
printArray(a,10);
}
void addOneToEachElement(int x[], int length)
{
for(int i=0; i<length; i++)
{
x[i]++;
}
}
void printArray(int x[], int length)
{
for(int i=0; i<length; i++)
{
cout<<"index "<<i<<" is "<<x[i]<<endl;
}
}
Few important things to note
The length of the array does not have to be specified in the square brackets when passing an array to function-- this is only for 1-D arrays
It is common practice to pass in an additional length variable as a second parameter to a function. Unlike java, in C++ we do not have a .length() member function to use
The value of the array changed even though the array wasn't passed by reference
Passing higher dimensional arrays into a function
if you have a function that accepts 2-D or 3-D or more D arrays, the size of all the dimensions but one must be specified
void my2DFun(int array[][10]);
void my3DFun(int array[][10][15]);
Arrays are always passed by reference
Arrays are always passed by reference partly because passing by value would be memory inefficient. Think about if you have a 10000 element array, if you pass that by value that would mean the function memory stack would have a copy of the 10,000 elements in the array. This is memory inefficient, and thus, arrays are passed by reference by default. This means that if you pass an array into a function and change its values within that function, that change will be reflected outside that function.
However, to give you slightly more insight into arrays, create an array and print out just the name of the array. So basically do the following code
int x[10];
cout<<x; // What is printed out? What does this remind you of?
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of