Function accepting arguments of objects with 2 arrays

Hello again! I've almost figured this out, but I'm confused on how to pass some arguments to my function. Below I have a function multiplyArrays(Time). It accepts an object Time, that has 2 arrays in it, x[] and y[]. Each has already been initialized with 10,000 doubles. I want the function to multiply x and y for each of the 10,000 entries.

However, I'm lost on how to pass that information to my function. I've tried a few different things, but my head is pounding now... and I'm taking a break from it. Any help would be appreciated! I put the code below, I expect I am missing some statements in the function to assign to the arrays... but I'm not sure. I left out my constructor for the class Time.

1
2
3
4
5
6
7
8
9
10
void multiplyArrays(Time) {			// Multiply 2 arrays	
	for(int i = 0; i < 10000; i++) {	// For loop to run 10,000 times
		cout << x[i] << " * " << Time::y[i] << " = " << (x[i]*y[i]) << "\n";	// Display the results of the multiplication
	}

int main() 
{
	Time myArrays;		// Create and initialize an object myArrays, calls constructor.
	
	multiplyArrays(myArrays);		// Call the function to multiply the arrays in myArrays. 
Last edited on
You would pass it in the same way you pass in any other variable.

How would you pass in an int to a function?
1
2
3
4
void my_func(int a)
{
    cout << a << endl;
}

The name of the int is a in the above example.
But you never gave an actual name to your Time object.

You could make the definition of the function something like:
void multiplyArrays(Time time) { ... };

However, copying two large arrays is expensive, so I would suggest passing by a constant reference instead. Your full definition and implementation would look like this:

1
2
3
4
5
void multiplyArrays(const Time& time) {
	for (int i = 0; i < 10000; i++) {
		cout << time.x[i] << " * " << time.y[i] << " = " << (time.x[i]*time.y[i]) << "\n";
	}
}


If you're still having trouble, give the definition of your class. If your x and y arrays are static, then my way won't work.
Last edited on
Topic archived. No new replies allowed.