passing arrays by values

I need to pass these arrays to a fcn extend() which should caluclate elements in the amount array as the product of the elements in price and quantity arrays. After extend() passes values to amount, the values in the array should be displayed w.in main. Can you see the error? In function `double extend(double*, double*, double*, double)':
:26: error: cannot convert `double' to `double*' in assignment


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
#include <iostream>
using namespace std;

double extend(double[], double[], double[], double);
int main()
{
   const int MAXELS = 10;
   double price[MAXELS] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
   double quantity[MAXELS] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
   double amount[MAXELS];
   
   cout<< "The total amount is " 
   << extend(price, quantity, amount, MAXELS) << endl;
   
   return 0;
}

double extend(double vals[], double nums[], double tot[], double max)
{
   int i;
   double amount;
   tot = vals[0] * nums[0];
   for(i=0; i < max; i++);
     tot[i] = vals[i] * nums[i];
   return amount;
}


passing c-arrays (the ones you're using) by value isn't possible. You may instead pass a C++ vector, which is a type of an array.

If you want to do this because you want the array to be copied for you, this is unfortunately not possible with c-arrays. Just copy it yourself.

I'd recommend using vectors:

http://www.cplusplus.com/reference/stl/vector/

which can be copied and passed by value or reference or anything you want.
Line 22 is wrong and unnecessary, remove it. You do the calculations correctly in lines 23/24.

vals[i] and nums[i] are doubles, whereas tot is a double*. You cannot assign a double to a double* and you neither need nor want to do that.

Hi , i cannot get this ,

vals[i] and nums[i] are doubles, whereas tot is a double*
How come we are passing the array of amount which is array of double .
Topic archived. No new replies allowed.