This is for C++ Write a program that calculates voltage from current and resista
ID: 3699951 • Letter: T
Question
This is for C++
Write a program that calculates voltage from current and resistance values.
Create three one-dimensional built-in arrays named current, resistance and voltage, each capable of holding 10 double-precision values. The values stored in current and resistance are as follows:
current = 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98
resistance = 4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 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 resistancearrays, for example:
voltage[1] = current[1] * resistance[1]
Write the calcVolts() function (prototype, header, body) using pointers.
After calcVolts() has calculated and placed values in the voltage array, display the values in the array from within main()
voltage = current x resistance 42.48 10.63 4.00 126.56 14.89 8.50 79.26 13.21 6.00 ...... ...... ......Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void calcVolts(double c[], double r[], double v[], int n) {
for(int i=0;i<n;i++) {
*(v+i) = *(c+i) * *(r+i);
}
}
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.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8};
double voltage[10];
calcVolts(current, resistance, voltage,10);
cout<<"voltage = current x resistance"<<endl;
cout<<fixed<<setprecision(2);
for(int i=0;i<10;i++) {
cout<<voltage[i]<<" "<<current[i]<<" "<<resistance[i]<<endl;
}
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.