Number of Array parameters in function?

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
 
using namespace 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.
Arrays are passed to functions by pointer, not by copy, so modifying the array in your function modifies it in main as well - they're the same array.
I don't think my class has gone over pointers yet.
That's no reason to stop reading my post the instant you see the word "pointer".

In this code:
1
2
3
void MyFunction(int myArray[])
{
}
The parameter "myArray" is actually a pointer, not an array.
so if that were the case then the following three pointers would be a parameter in the function?

1
2
3
4

void myfunction(int myarray1[], my array2[], myarray3[])
{
}
Last edited on
Don't think too much about the fact that they're pointers, just know that that's how it works when you pass an array to a function.
Ok well thank you for the explanation hopefully I will be able to get a extension on when the program is due.
Huh? You're giving up after all the help I gave you?
no I have the programming class at 8:00 pm so I can't work on it but Ill try and do it still cause I will need to understand it for future projects.
Topic archived. No new replies allowed.