Arrays and Functions


Single Dimensional Arrays Passed to Functions

Arrays may not be returned by functions but they can be passed as arguments to functions. When they are arguments, they are passed as references. The declarations of functions that pass arrays have the brackets listed as the following declaration shows. Notice that it has two arguments which are single dimensional arrays:

Declaration:

image from book

 void enterSales(double salesAmounts[], string salesDays[]); 

image from book

Observe that the size of the arrays is not listed in the declaration.

The definition of a function that has single dimensional arrays as arguments must also have the array brackets as with the following function definition that corresponds with the function declaration above:

Definition:

image from book

 void enterSales(double salesAmounts[], string salesDays[]) {    for(int index = 0;index<=6;index++)    {      cout << "What was the sales on " << salesDays[index] << "? ";      cin >> salesAmounts[index];    } } 

image from book

The call of a function that has single dimensional arrays as arguments does not include the brackets as the following call to the above function indicates:

Call:

image from book

        enterSales(salesAmounts, salesDays); 

image from book

The function: enterSales( ) above was dependent on the number of days being seven. Therefore the size of the array was not required in the body of the function to help control access to the array. However as the following function declaration, function definition and call indicate, the size of the array sometimes needs to be passed to the function in order to control access to the array elements.

Declaration:

image from book

 void displaySales(short arraySize, double salesAmounts[], string SalesDays[]); 

image from book

Definition:

image from book

 void displaySales(short arraySize, double salesAmounts[], string salesDays[]) {   for(short index = 0;index< arraySize;index++)   {     cout << "The sales on " << salesDays[index] << " was $"          << salesAmounts[index] << endl;   } } 

image from book

Call:

image from book

 displaySales(arraySize, salesAmounts, salesDays); 

image from book

For an example of the above functions see DAYS8.CPP. Compile and run this program.

Multidimensional Arrays Passed to Functions

The example above dealt with passing single dimensional arrays to functions. Passing multidimensional arrays to functions needs to be treated slightly different as the following declaration, definition and call indicate.

First notice that in the declaration the first dimension only has the brackets listed as with the single dimensional array above. However the second dimension not only has the brackets but it also lists the size of the second dimension in the brackets. Next, notice that the same conditions hold for the definition as with the declaration. That is the first dimension only lists the brackets while the second dimension not only lists the brackets but the size of the second dimension as well. Next, look at the call. As with the single dimensional array example above, the call of the function passing multidimensional arrays does not list the brackets or the size of any of the respective dimensions for the multidimensional array.

Declaration:

image from book

 void enterAmounts(double generalLedger[][numberAccounts], string theMonths[]); 

image from book

Definition:

image from book

 void enterAmounts(double generalLedger[][numberAccounts], string theMonths[]) {   for(int indexMonths = 0; indexMonths <= 11; indexMonths++)   {      cout << endl << "Enter the amounts for "           << theMonths[indexMonths] << endl;      for(int indexAccounts = 0; indexAccounts <= 4; indexAccounts++)      {         cout << "The amount for account #"              << static_cast<int>(indexAccounts+1) << " ";         cin >> generalLedger[indexMonths][indexAccounts];      }   }   cout << endl << endl        << "All of the General Ledger Accounts"        << " have been entered."        << endl;   cout << "Continue? ";   char resp;   cin >> resp; } 

image from book

Call:

image from book

 enterAmounts(generalLedger,theMonths); 

image from book

The function: enterAmounts( ) just considered had its array arguments controlled by two different global variables. Therefore no arguments to control access to the arrays needed to be passed. However, what if global variables like these were not used? In this case the size of each array dimension would need to be passed as arguments to the function as can be observed in the declaration, the definition and the call of the function in the following example:

Declaration:

image from book

 void displayAmounts(double generalLedger[][numberAccounts],            string theMonths[], short numberMonths, short numberAccounts); 

image from book

Definition:

image from book

 void displayAmounts(double generalLedger[][numberAccounts],         string theMonths[],short numberMonths, short numberAccounts) {   cout << fixed << setprecision(2);   double totalLedger = 0.00;   for(short indexMonths = 0; indexMonths < numberMonths; ++indexMonths)   {      double totalAccountAmount = 0.00;      cout << endl << "The amounts for "           << theMonths[indexMonths] << endl;      for(short indexAccounts = 0; indexAccounts < 5; ++indexAccounts)      {         cout << "The amount for account #"              << (indexAccounts+1) << " $"              << setw(8)              << generalLedger[indexMonths][indexAccounts]              << endl;         totalAccountAmount += generalLedger[indexMonths][indexAccounts];      }      cout << "__________" << endl;      cout << "Total amount for " << theMonths[indexMonths]           << "$"           << setw(8) << totalAccountAmount << endl << endl;      totalLedger += totalAccountAmount;   }   cout << endl << "General Ledger Total $" << setw(8)        << totalLedger << endl << endl;   char resp;   cout << "Continue? ";   cin >> resp; } 

image from book

Call:

image from book

 displayAmounts(generalLedger, theMonths, numberMonths, numberAccounts); 

image from book

To view a program that demonstrates functions with multidimensional arrays as arguments see GENERALLEDGER.CPP.

When passing arrays with more than two dimensions: the declarations, the definitions and the calls would be handled similar to the multidimensional arrays in the example above. Each dimension beyond the second should be treated as the second dimension was in the example above. That is the size of the dimension must be used in the declaration and in the definition but not in the call.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net