尝试进行一些规范化并不断收到错误

时间:2017-04-04 18:39:52

标签: c++ int

我一直收到错误"错误:无效转换为'int *'到'int'[-fpermissive]"我不知道为什么;

我这样做是为了完成家庭作业,我们还没有讨论指针,所以使用它们是不可能的。

这是我的代码(PS我是编程新手)

#include <iostream>
#include <cmath>


using namespace std;

double normalize (int , int);
double normalize (double,double);
int n=0;
int i=0;
const  double SIZE=5;
double mean=0;
double meanDivide;



int main()
{
    char dataType;
    int norm[5];
    int value =1;


    cout<<"Which data type do you want (i, d): ";
    cin>>dataType;

    if (dataType =='i')
    {
        while(value<5) 
        {
            cout<<"Enter value "<<value << ": ";
            cin>> norm[n];
            value++;

        }
    }
    else if (dataType=='d')
    {
        cout<<"Enter value "<<value << ": ";
            cin>> norm[n];
            value++;

    }


    cout<<"The mean is: "<<normalize(norm,5)/* The error comes from here and 

I do not know what to do or why */
<<endl;

    cout<<"The normalized values: "<<endl;

    int j=0;

    cout<<"norm[1] = "<<norm[j]<<endl;
    j++;
    cout<<"norm[2] = "<<norm[j]<<endl;
    j++;
    cout<<"norm[3] = "<<norm[j]<<endl;
    j++;
    cout<<"norm[4] = "<<norm[j]<<endl;
    j++;
    cout<<"norm[5] = "<<norm[j]<<endl;





    return 0;

}


double normalize(int norm[],int SIZE)
{


    while(i<6)
    {
        meanDivide +=norm[i];
        i++;
    }
    i=0;

    while (i<n)
    {

        norm[i] -=meanDivide;
        i++;
    }

    mean = meanDivide / 5;

    return mean;
}

double normalize (double norm[],double SIZE)
{


    while(i<6)
    {
        meanDivide +=norm[i];
        i++;
    }
    i=0;

    while (i<n)
    {

        norm[i] -=meanDivide;
        i++;
    }
    mean = meanDivide / 5;

    return mean;
}

这是我应该得到的输出。

//For integers:

Which data type do you want (i, d): i
Enter value 1: 0
Enter value 2: 3
Enter value 3: 4
Enter value 4: 8
Enter value 5: 12
The mean is: 5.4
The normalized values:
norm[1] = -5
norm[2] = -2
norm[3] = -1
norm[4] = 2
norm[5] = 6


//For doubles:
Which data type do you want (i, d): d
Enter value 1: 5.5
Enter value 2: 1.23
Enter value 3: 2.02
Enter value 4: 9.99
Enter value 5: 6.32
The mean is: 5.012
The normalized values:
norm[1] = 0.488
norm[2] = -3.782
norm[3] = -2.992
norm[4] = 4.978
norm[5] = 1.308

2 个答案:

答案 0 :(得分:1)

您正在声明这样的方法:

double normalize (int , int);
double normalize(double*, double);

然而,你正试图像以下那样实现它们:

double normalize(int norm[], int SIZE) {/*...*/}
double normalize(double norm[], double SIZE) {/*...*/}

请注意,参数类型不同,int, intint[], int不同。这意味着您的实现实际上定义了一个全新的函数,与您在示例顶部声明的函数无关。当您调用normalize函数时,只会找到初始声明,并尝试将int norm[5]与失败的int匹配。要解决此问题,请确保声明正确。将声明更改为:

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

这将解决您在此问题中询问的错误,但您的示例仍有其他问题。其中一些在评论中有所体现。

答案 1 :(得分:0)

您为非指针参数声明原型:

double normalize (int , int);
double normalize (double,double);

使用指针参数调用:

cout<<"The mean is: "<<normalize(norm,5)

此时的C ++编译器并不关心下面的指针参数的实现。尝试将实现与原型匹配。

另外
a)您增加value但使用n作为索引:
            CIN&GT;&GT;范数[N];             值++;

b)你忽略SIZE
c)在两种情况下,SIZE都应该是unsigned int d)你的例子不是最小的(https://stackoverflow.com/help/mcve

您可能会发现这通常很有用:
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/