我不斷收到錯誤「錯誤:從'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
你正在聲明你的方法像'double normalize(int,int);'但是像'normalize(int [],int)'一樣實現它們。 –
儘量避免使用全局變量,使用它們是一種不好的做法。還有一件事我不理解......你每次收到價值都不應該增加'n'嗎? –
@FrançoisAndrieux我應該怎麼做才能解決它? –