Program only returning 1st value of array

I'm writing code that declares 3 arrays in main then sets the 3rd equal to the 1st multiplied by the 2nd in another function. It's then supposed to give display
the 3rd array within the main function. For some reason, when I run it it repeats the 1st value over and over instead of listing the following ones.
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
 #include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int i;

double calcVolts(const double *current, const double *resistance, double *voltage)
{
    
    for(i=0; i<10; i++)
   { 
      voltage[i] = current[i] * resistance[i];
   

return voltage[i];
}}

int main()
{
 const int num=10;  
   double current[num] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
   double resistance[num] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8}; 
   double voltage[num];
   for (int i=0; i<num; i++)
   {
       voltage[i]= calcVolts(current, resistance, voltage);
   cout<<"Voltage number "<< i+1 << " is: "<< voltage[i]<< endl;
   }
   return 0;
}






The output is Voltage number 1 is: 42.48
Voltage number 2 is: 42.48
Voltage number 3 is: 42.48
Voltage number 4 is: 42.48
Voltage number 5 is: 42.48
Voltage number 6 is: 42.48
Voltage number 7 is: 42.48
Voltage number 8 is: 42.48
Voltage number 9 is: 42.48
Voltage number 10 is: 42.48

What am I doing wrong?
Your calcVolts function doesn't need to return anything, make it void and remove return statement on line 17. Call it once before you start printing out your answers in main.

1
2
3
4
5
6
//In main() after arrays are declared
calcVolts(current, resistance, voltage);
for (int i=0; i<num; i++)
   {
      cout<<"Voltage number "<< i+1 << " is: "<< voltage[i]<< endl;
   }
Topic archived. No new replies allowed.