Hello, I am to write a program that will send three one dimensional arrays to a function. Two of the arrays have 10 values and one has room set aside for 10 values to be placed into it. The function will take the corresponding elements from each array,multiply them together and put the result into the array with space for 10 values. I am having a hard time figuring out how I can get them to multiply each element and then place them into the array. My guess would be a for loop, or maybe a couple of for loops. Any help appreciated. Code: #include<iostream> using namespace std; double calc_volts(); int main() { double volts[10]; double current[10] = {10.62 ,14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.8}; double resistance[10] = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8}; calc_volts(volts); calc_volts(current); calc_volts(resistance); system("pause"); return 0; } double calc_volts() int i; for (i=0;i<????;i++) { volts[i] = current[i] * resistance[i]; } return volts[i];
Pass them in as parameters: Code: void calc_volts(double *volts,double *current,double *resistance); int main() { double volts[10]; double current[10] = {10.62 ,14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.8}; double resistance[10] = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8}; calc_volts(volts, current, resistance); return 0; } Then just replace ???? with 10 and that should do the trick. You might want to display the results somewhere as well. calc_volts() doesn't need to return anything but it will need to take the three arrays as parameters/arguments.
Ok thanks that makes sense. I actually do need to return the array that was filled and display the results in main(). If it's returning an array I can still use double for the return since all the values in the array will be doubles correct? If not how would I go about returning the array itself.
When you modify volts[n] in the array, because you have passed a pointer to the array you're directly modifying the array volts[] in main itself. For example: Code: void func(int *y) { y[0]=5; // modifies x in main } main() { int x[2]; x[0]=0; func(x); printf("%d\n",x[0]); // will print 5, not 0 } Didn't test this but it should compile and run.