So I know how to pass one array parameter into a function but the question is how would I do it if I needed to pass more then one array parameter. As an example would be something like below how would I pass three array parameters into the function code? (note the following is only the function of my program main is is not included)
//function charge calculation with array and array size
double calculateCharges(double hours[], double calculate[], double charge[], int HoursArrayLocation)
{
double result[3];
//if less then 3 hours return $2.00
if(hours[HoursArrayLocation]<3)
{
result[HoursArrayLocation]=2.00;
return result[HoursArrayLocation];
}
//if more then 3 hours do the following
else
{
//if less then 24 hours but more then 3 hours choose this statement added with initial 3 hour charge
if (hours[HoursArrayLocation]<24)
{
//calculation of total charge for less then 24 hours and return charge
result[HoursArrayLocation]=2.00+((hours[HoursArrayLocation]-3)*0.5);
return result[HoursArrayLocation];
}
//if total time is equal to 24 hours return $10.00
else
{
result[HoursArrayLocation]=10.00;
return result[HoursArrayLocation];
}
}
}//end of function
int main() {
double hours[20], calculate[20], charge[20];
int num = 0;
calculateCharges(hours, calculate, charge, num);
return 0;
}
Should work. I'm not really seeing any issues here.
Edit: I think your issue here isn't passing the arrays, but returning the correct thing. Your function is returning a double. You have a double array declared inside of the function. The array isn't needed. If you just change double result[3]; to double result; you shouldn't have any issues. I'm not sure why you're using an array.
You would also need to change all result[HoursArrayLocation] to result. What happens when HoursArrayLocation is 3 or larger? Your program may crash due to accessing memory that's out of bounds.
I agree with Volatile Pulse, I see no issues here.
also, I see that only one item is ever used form the arrays [HoursArrayLocation] If this is the complete function perhaps it would be more appropriate to pass the actual values......
1 2 3 4 5 6 7 8 9
double calculateCharges(double hours, double calculate, double charge);
int main() {
double hours[20], calculate[20], charge[20];
int num = 0;
calculateCharges(hours[num], calculate[num], charge[num]);
return 0;
}