So in this function it is already passing the array into the function but the thing is one parameter being passed into the function and if so how do I go about passing 3 arrays as parameters into the function? Also the program only asks for a user entry of hours for three different cars so why would there be a point in making two additional arrays to be passed into the function?
#include <iostream>
#include <iomanip>
usingnamespace std;
//passing array into function
double calculateCharges(double hours[], int HoursArrayLocation);//call function
int main()
{
//create variables for three customers using array
double customers[3];
//ask user for first customer hours and output hours
cout<<"Enter hours for first customer: ";
cin >>customers[0];
//ask user for second customer hours and output hours
cout<<"Enter hours for second customer: ";
cin >>customers[1];
//ask user for third customer hours and output hours
cout<<"Enter hours for third customer: ";
cin >>customers[2];
//table labels
cout<<"\nCar\tHours\tCharge\n";
//output hours and charge for customer 1 with spacing aligned to the right
cout<<"1"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[0]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 0)<<endl;
//output hours and charge for customer 2 with spacing aligned to the right
cout<<"2"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[1]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 1)<<endl;
//output hours and charge for customer 3 with spacing aligned to the right
cout<<"3"<<setw(12)<<right<<fixed<<setprecision(1)<<customers[2]<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 2)<<endl;
//output total hours and charge for all three customers with spacing aligned to the right
cout<<"\n\nTOTAL"<<setw(8)<<right<<fixed<<setprecision(1)<<customers[0]+customers[1]+customers[2]<<fixed<<setprecision(2)<<setw(9)<<right<<calculateCharges(customers, 0)+calculateCharges(customers, 1)+calculateCharges(customers, 2)<<endl;
return 0;
}//end of main
//function charge calculation with array and array size
double calculateCharges(double hours[], int HoursArrayLocation)
{
double result;
//if less then 3 hours return $2.00
if(hours[HoursArrayLocation]<3)
{
result=2.00;
return result;
}
//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=2.00+((hours[HoursArrayLocation]-3)*0.5);
return result;
}
//if total time is equal to 24 hours return $10.00
else
{
result=10.00;
return result;
}
}
}//end of function
You already have a topic here: http://www.cplusplus.com/forum/beginner/126596/
Your code and question do not appear to be significantly different enough to warrant a new thread, unless I am misunderstanding something.
well the issues is I have to use three arrays to pass it into the function but I don't understand how I would go about doing that because of the reason that the program asks the user to enter the hours which is stored in the customers array only. So I don't understand how if I were to make two additional arrays how it would work in being passed into the function when the function uses only the three hours entered.
The other arrays sound like they would be left untouched by main and they would be assigned to by the function. That is why the function has "calculate" in its name, right?
From what I can understand of what this program is suppose to be do is that we have to use three arrays one to identify the 3 cars, one for the 3 different hours entered and the one for the 3 results as you said the calculate part is done in the function so I don't know if i define the result array in the main how would it go to function and and then return back to main for output.