#include <iostream>
#include <iomanip>
usingnamespace std;
double calculateCharges(double hours[], double sizeofArray);//call function
int main()
{
//create variables for three customers
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
double calculateCharges(double hours[], double sizeofArray)
{
double result[3];
//if less then 3 hours return $2.00
if(hours[]<3)
{
result[0]=2.00;
return result[0];
}
//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[]<24)
{
//calculation of total charge for less then 24 hours and return charge
result[1]=2.00+(hours-3)*0.5;
return result[1];
}
//if total time is equal to 24 hours return $10.00
else
{
result[2]=10.00;
return result[2];
}
}
}//end of function
The issue I'am having is I don't know how I pass the information from the main into a array in the function and how to pass the array into the if statements. P.S. I'am a beginner at c++ programming.
When passing an array, you do not include the brackets, just the name. I then sent the location in the array. Hope the changes I made are understood, or, if not, ask about them.
#include <iostream>
#include <iomanip>
usingnamespace std;
double calculateCharges(double hours[], int LocationInArray);//call function
int main()
{
//create variables for three customers
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
double calculateCharges(double hours[], int LocationInArray)
{
double result[3];
//if less then 3 hours return $2.00
if(hours[LocationInArray]<3)
{
result[LocationInArray]=2.00;
return result[LocationInArray];
}
//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[LocationInArray]<24)
{
//calculation of total charge for less then 24 hours and return charge
result[LocationInArray]=2.00+((hours[LocationInArray]-3)*0.5);
return result[LocationInArray];
}
//if total time is equal to 24 hours return $10.00
else
{
result[LocationInArray]=10.00;
return result[LocationInArray];
}
}
}//end of function