Calculating voltage

I am having trouble getting the output of voltage. All I get are 0's and a 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
using namespace std;

void calcVolts(double* current, double* resistance, double* voltage) {
     int i = 0;
     for (i = 0; i < 10; i++) {
         *voltage++ = (*current++) * (*resistance++);
         cout << *voltage << endl;
     }
     return;
}

main() {
       
       double current[10] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
       double resistance[10] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
       double voltage[10] = {};
       
       calcVolts(current, resistance, voltage);
             
       system("pause");
       return 0;
}
What is the algorithm for calculating voltage?
Line 13:
Meant to be int main()

Line 7:
This is difficult to read and not intuitive. Since you have a for loop, I'd recommend you do this:
 
voltage[i] = current[i] * resistance[i];


1
2
*voltage++ = (*current++) * (*resistance++);
cout << *voltage << endl;

When you're printing out voltage, you're actually printing the element after the one you've just calculated, because of the increment. Since your voltage array has been zero-initialised, it will just print out 0s.
A simple fix:
1
2
3
*voltage = (*current) * (*resistance);
cout << *voltage << endl;
voltage++; current++; resistance++;
Last edited on
Under the function calcVolts. All I am suppose to do is multiply each input from resistance and current to get voltage. I am stricted to using pointers in the calcVolts function.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void calcVolts(double* current, double* resistance, double* voltage) {
     int i = 0;
     for (i = 0; i < 10; i++)
     {
         *voltage++ = (*current++) * (*resistance++);
         cout << *voltage << endl;
     }
     return;
}

int main() {
       
       double current[10] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
       double resistance[10] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
       double voltage[10] = {};
       
       calcVolts(current, resistance, voltage);
             
       system("pause");
       return 0;
}


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

void calcVolts(double current[], double resistance[], double voltage[]) 
{
	int i;
	for (i = 0; i < 10; i++) 
	{
		voltage[i] = current[i] * resistance[i];
		cout << voltage[i] << endl;
	}
	return;
}

int main() 
{       
       double current[10] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
       double resistance[10] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
       double voltage[10] = {0};
       
       calcVolts(current, resistance, voltage);
             
       system("pause");
       return 0;
}
Last edited on
Under the function calcVolts. All I am suppose to do is multiply each input from resistance and current to get voltage. I am stricted to using pointers in the calcVolts 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
#include <iostream>
using namespace std;

void calcVolts(double *current, double *resistance, double *voltage) 
{
	int i;
	for (i = 0; i < 10; i++) 
	{
		cout << (*voltage = (*current) * (*resistance)) << endl;
		++voltage; ++current; ++resistance;
	 }
	return;
}

int main() 
{       
       double current[10] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
       double resistance[10] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
       double voltage[10] = {0};
       
       calcVolts(current, resistance, voltage);
             
       system("pause");
       return 0;
}
Alright. Now I understand where I made my mistake. Thanks for the help!
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

void calcVolts(double* current, double* resistance, double* voltage, const int size)
{
    for (int i = 0; i < size; i++) {
        voltage[i] = current[i] * resistance[i];
    }
    return;
}

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


This version shows how the pointers work in detail.
Last edited on
Topic archived. No new replies allowed.