Indexed Variables as Function Arguments
-
An indexed variable of an array can also be used as actual arguments of a function. For example, if exam_score is an array which stores the exam scores as integers. Suppose letter_grade is a function that takes an integer argument that represents an exam score and returns a character that is the letter grade computed based on that score, then the letter grade for the third exam score can be output to the screen as follows:
cout << "The grade for the third exam score is: "
<< letter_grade(exam_score[2]) << endl;
-
//Illustrates the use of an indexed variable as an argument.
//Adds 5 to each employee's allowed number of vacation days.
#include <iostream>
using namespace std;
const int NUMBER_OF_EMPLOYEES = 3;
int adjust_days(int old_days);
//Returns old_days plus 5.
int main( )
{
int vacation[NUMBER_OF_EMPLOYEES], number;
cout << "Enter allowed vacation days for employees 1"
<< " through " << NUMBER_OF_EMPLOYEES << ":\n";
for (number = 1; number <= NUMBER_OF_EMPLOYEES; number++)
cin >> vacation[number-1];
for (number = 0; number < NUMBER_OF_EMPLOYEES; number++)
vacation[number] = adjust_days(vacation[number]);
cout << "The revised number of vacation days are:\n";
for (number = 1; number <= NUMBER_OF_EMPLOYEES; number++)
cout << "Employee number " << number
<< " vacation days = " << vacation[number-1] << endl;
return 0;
}
int adjust_days(int old_days)
{
return (old_days + 5);
}