Write a program that declares three one-dimensional arrays named current, resist
ID: 3682665 • Letter: W
Question
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.
Explanation / Answer
#include<iostream>
using namespace std;
void calcVolts(float* c,float* r,float* v); //Function declaration
int main()
{
float current[] = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98};
float resistance[] = {4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8};
float voltage[10];
calcVolts( current, resistance, voltage);//as array name can also be treated as a pointer, here we use array name to pass address of first elements of respective arrays
cout<<" The voltage values are : ";
for(int i=0;i<10;i++)
cout<<voltage[i]<<" ";
}
void calcVolts(float* c,float* r,float* v) //Function definition where the first elements of current,resistance,voltage are pointed by pointers c,r,v respectively
{
for(int i=0;i<10;i++)
*(v+i) = *(c+i) * *(r+i); //Multiplication using pointers. Statement equivalent to : voltage[1] = current[1] * resistance[1];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.