无法在 void function.the 中打印出最多 6 的十进制数 function.the 值已正确检查 debugging.the 问题出在输出上
Cannot print out decimal numbers upto 6 in a void function.the value are correct checked by debugging.the problem is with the output
**The void function automatically rounds off the value of a,b,c and prints out 0 instead of 0.5,0.33,0.166. I have checked the value of num1,num2,num3 and g they are correct just the problem is with the rounding off of the decimals of a,b and c.The setprecision function is also not working.
简单的概念是,当用户以数组形式输入时,我们必须计算正数的数量、负数的数量、零的数量(分别存储在 num1、num2、num3 中)。 **
void plusMinus(vector<int> arr) {
int g = arr.size();
int num1=0; // Initialized value of num1.
int num2=0; // Initialized value of num2.
int num3=0; // Initialized value of num3.
for(int t=0;t<g;t++){
if(arr[t]>0){
num1++;
}
else{
if(arr[t]<0){
num2++;
}
if(arr[t]==0){
num3++;
}
}
}
const double a = num1/g; // made const due to the automatic rounding-off //
const double b = num2/g; // made const due to the automatic rounding-off //
const double c = num3/g; // made const due to the automatic rounding-off //
cout << setprecision(6) << a << b << c; // The problem is with this line
}
你想让代码输出数字吗? bc 在这种情况下,您需要系统输出变量 num1、num2 和 num3。
**The void function automatically rounds off the value of a,b,c and prints out 0 instead of 0.5,0.33,0.166. I have checked the value of num1,num2,num3 and g they are correct just the problem is with the rounding off of the decimals of a,b and c.The setprecision function is also not working.
简单的概念是,当用户以数组形式输入时,我们必须计算正数的数量、负数的数量、零的数量(分别存储在 num1、num2、num3 中)。 **
void plusMinus(vector<int> arr) {
int g = arr.size();
int num1=0; // Initialized value of num1.
int num2=0; // Initialized value of num2.
int num3=0; // Initialized value of num3.
for(int t=0;t<g;t++){
if(arr[t]>0){
num1++;
}
else{
if(arr[t]<0){
num2++;
}
if(arr[t]==0){
num3++;
}
}
}
const double a = num1/g; // made const due to the automatic rounding-off //
const double b = num2/g; // made const due to the automatic rounding-off //
const double c = num3/g; // made const due to the automatic rounding-off //
cout << setprecision(6) << a << b << c; // The problem is with this line
}
你想让代码输出数字吗? bc 在这种情况下,您需要系统输出变量 num1、num2 和 num3。