关于传递数组的困惑

时间:2013-11-03 19:52:02

标签: c++

有人可以帮我解决我的计划吗?我需要将三个数组传递给名为calc_volts的函数,然后计算伏特然后显示值。我不断收到错误,说明变量的“未引用的局部变量”或“未声明的idebtufier”; i,j,k和伏特。

    #include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;
double calc_volts(double, double, double, int);

int main()
{


    const int max = 10;
    int i; double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};

    int j; double volts[max];

    int k; double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};



}

double calc_volts(double current[],double volts[], double resistance[], int max)
{
    for (j = 0, j<max, j++)
    volts[j] = current[i]*resistance[k];

    return volts[j];

}

1 个答案:

答案 0 :(得分:2)

你有很多问题:

您的函数声明错误:

double calc_volts(double, double, double, int);

应该是:

double calc_volts(double[], double[], double[], int);

您必须调用该函数才能使用它:

int main()
{
    const int max = 10;

    double current[max] = {10.62,14.89,13.21,16.55,19.62,9.47,6.58,18.32,12.15,3.98};

    double volts[max];

    double resistance[max] = {4,8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};

    calc_volts(current, volts, resistance, max); // call the function to execute it    
}

变量ijkcalc_volts内不存在,因为它们是在main内声明的。在函数内声明的变量只能在该函数内使用。

要解决问题,只需将声明放在calc_volts