104k views
2 votes
Write a program that declares three one-dimensional arrays named current,

resistance, and voltage. Each array should be declared in main() and be capable of holding
10 double-precision numbers. The numbers to be stored in current are 10.62, 14.89,
13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, and 3.98. The numbers to be stored in resistance
are 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, and 4.8. Have your program pass these three arrays to a
function called calcVolts(), which calculates the elements in the voltage array as the
product of the equivalent elements in the current and resistance arrays: for example,
voltage[1] = current[1] * resistance[1].
After calcVolts() has put values in the voltage array, display the values in the array from
within main(). Write the calcVolts() function by using pointers.

User Ready Cent
by
8.2k points

1 Answer

5 votes

Final answer:

Creating a C++ program that calculates voltage values using Ohm's Law and displays the results, utilizing arrays and pointers for array element access.

Step-by-step explanation:

To address the student's query, a C++ program can be written that includes three one-dimensional arrays for 'current', 'resistance', and 'voltage'. The program will have a 'calcVolts()' function that takes the 'current' and 'resistance' arrays, calculates the 'voltage' using Ohm's Law (V = I * R), and stores it in the voltage array utilizing pointer arithmetic to access the array elements.

Here's an example of how the C++ function calcVolts() would look:

void calcVolts(double current, double* resistance, double voltage, int size) {

for(int i = 0; i < size; ++i) {

*(voltage + i) = *(current + i) * *(resistance + i);

}

}

And to display the values within the main function after calculation:

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, 10);

for(int i = 0; i < 10; ++i) {

cout << "Voltage[" << i << "]: " << voltage[i] << endl;

}

return 0;

}

The program successfully calculates and displays the voltages based on the given current and resistance values using pointers, demonstrating the use of functions and arrays in C programming.

User Alexandru Dranca
by
8.2k points