Let’s design a function to add the elements in an array.
In C++, as in C, the array name itself is often treated as a de facto pointer. For our function to add the array, we will pass as the first argument the name of the array, and the second argument will be the int bounds of the array.
#include <iostream>
int sumArray(int iArray[], int iBounds);
using namespace std;
int main(void){
int iNums[] = {4, 8, 15, 16, 23, 42};
int iBounds = sizeof(iNums) / sizeof(int);
cout << "The number of elements in the array is: " << iBounds << endl;
cout << "The sum of the elements is: " << sumArray(iNums, iBounds) << endl;
return 0;
}
int sumArray(int iArray[], int iBounds){
int iSum = 0;
for(int i = 0; i < iBounds; i++){
iSum += iArray[i];
}
return iSum;
}
Again, in most contexts C++ treats the name of an array as if it were a pointer, specifically to the first element in the array. If we wish to use pointer syntax in the array’s parameter list, we may do so.
#include <iostream>
using namespace std;
//function overloading
//first function handles c-style strings
//second hands int arrays
void printArray(char *szString);
void printArray(int *iNums, int iBounds);
int main(void){
char *szText = "El Psy Congroo.";
char *szText2 = "";
int iNums[] = {53, 88, 118, 389, 443, 546};
//no need to use reference operator on
//array indentifiers
printArray(szText);
printArray(szText2);
printArray(iNums, sizeof(iNums) / sizeof(int));
return 0;
}
void printArray(char *szString){
while(*szString!=''){
cout << *szString << " ";
szString++;
}
}
void printArray(int *iNums, int iBounds){
int i;
for(i = 0; i < iBounds; i++){
cout << *(iNums+i) << " ";
}
}
We recall here that adding one to a pointer, which includes an array name in this context, in fact adds a value equal to the byte size of the type to which the pointer is pointing. Thus, pointer arithmetic and array subscription are equivalent methods of iterating through a array.
We must explicitly pass the size of the array to the function because sizeof() only yields the size of the pointer, and not the size of the entire array.
#include <iostream>
using namespace std;
void sizeOfArray(int iArray[]);
void sizeOfArray(double *dArray);
int main(void){
double dArry[] = {4.55, 4.29, 19.99, 21.27};
int iArry[] = {1138, 47, 8088, 64};
cout << "size of double array in main(): " << sizeof(dArry) << endl;
sizeOfArray(dArry);
cout << "size of int array in main(): " << sizeof(iArry) << endl;
sizeOfArray(iArry);
return 0;
}
void sizeOfArray(int iArray[]){
cout << "sizeof int array (in the user-defined function): " << sizeof(iArray) << endl;
}
void sizeOfArray(double *dArray){
cout << "sizeof double array (in the user-defined function): " << sizeof(dArray) << endl;
}
Since a function with an array name argument has access to the original array, not a copy, we can use a function call to assign values to the arrays elements. We can use a loop to read successive values into the array, and then utilize a special flag value to indicate the end of input.
#include <iostream>
using namespace std;
int populateArray(int iArray[], int iBounds){
int i, iValue;
cout << "Fill the array with positive integer values." << endl;
cout << "Up to " << iBounds << " separate entries allowed." << endl;
for(i = 0; i < iBounds; i++){
cout << "Enter value #" << i + 1 << " (Enter any negative number to finish): ";
cin >> iValue;
if(!cin){
//bad input!
cin.clear();
while(cin.get()!='\n'){ continue; }
cout << "Bad input, returning..." << endl;
return i;
}
else if(iValue < 0){
return i;
}
else {
iArray[i] = iValue;
}
}
}
double arrayAverage(int iArray[], int iBounds){
int iSum = 0;
for(int i = 0; i < iBounds; i++){
iSum += iArray[i];
}
return (double)iSum / iBounds;
}
int main(void){
int iArray[50];
int iBounds = populateArray(iArray, 50);
double dAverage = arrayAverage(iArray, iBounds);
cout << "Number of elements in the array: " << iBounds << endl;
cout << "Average is " << dAverage << endl;
return 0;
}
One problem for us to think about is the function to sum up the elements of the array. What if we accidentally modified the value passed in to the function? For elements passed in by value, it wouldn’t be such a big deal – only the local variable would be incorrect, and the problem would be contained. However, if we are dealing with a value passed in by reference, for example an array, any accidental change we make to it will be permanent.
To guarantee that a function won’t alter the value of the original data structure, we can safeguard the parameter using the keyword const when we define the array.
#include <iostream>
using namespace std;
void displayArray(const double dArr[], int n){
for(int i = 0; i < n; i++){
cout << "Element # " << (i + 1) << ": ";
cout << dArr[i] << endl;
}
}
void displayReversedArray(const double dArr[], int n){
for(int i = n - 1; i >= 0; i--){
cout << "Element # " << (i + 1) << ": ";
cout << dArr[i] << endl;
}
}
int main(void){
double dArray[] = {72.91, 18.01, 54.25, 5.1, 5.48};
displayArray(dArray, sizeof(dArray) / sizeof(double));
displayReversedArray(dArray, sizeof(dArray) / sizeof(double));
return 0;
}
If you get the chance, please take a look at my C programming book: