我尝试在Xcode上用C ++保存声明双精度数组。
double array[size];
但是当我打印值时,
for(int i=0; i<size; i++){
cout<<array[i]<<" ";
}
它正在打印整数。
void display(int grade[][size_of_qz]) {
double stAve[size_of_st];
computeStAve(grade, stAve);
cout << "Student Ave Quizes\n";
for (int row = 0; row < size_of_st; row++) {
cout << row + 1 << " " << stAve[row] << " ";
for (int col = 0; col < size_of_qz; col++) {
cout << grade[row][col] << " ";
}
cout << endl;
}
}
void computeStAve(int grade[][size_of_qz], double stAve[]) {
int temp = 0;
for (int row = 0; row < size_of_st; row++) {
for (int col = 0; col < size_of_qz; col++) {
temp += grade[row][col];
}
stAve[row] = temp / size_of_qz;
temp = 0;
}
}
答案 0 :(得分:2)
看起来像这行是您出现问题的原因:
stAve[row]=temp/size_of_qz;
与:double =(int / int)相同。您的除法将转换为整数。您需要执行以下操作:
stAve[row]=(double)temp/size_of_qz;
答案 1 :(得分:-2)
如果希望打印一定数量的数字,请使用
启动您的主要功能。def add():
global operator
operator = 1
...
,要包含的库为 iomanip 。