How to create arrays in C++ finding the value at a given index in C++ using For Loops To Cycle through an arrayC++ Programming

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 <iostream>

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?

Attachments:

Instructions Files

C++ Programming Experts

expert
Dinh Hong P.
C++ Programming

20 Answers

expert
Eric Bell
C++ Programming

88 Answers

View More Experts
Disclaimer

The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.

Get Free Quote!

267 Experts Online