Reputation: 11
A program I am working on for a school project ignores 0s when printing. For example, this function would not print the first arm (Arm 0) so I added an extra printf before the for loop but whenever my armArray[i].count is equal to 0 it ignores it like this:
Total Payoff: 75.67
Arm Count Avg. Payoff Value
0 3 -0.02 0.59
1 74 0.94 0.86
2 19 0.25 0.18
3 4 0.33 0.89
4 0.00 0.25
void PrintStatistics(const BanditArm armArray[], int numArms) {
double tPayoff = 0.0;
for(int i =0; i<numArms;i++){
tPayoff+=armArray[i].totalPayoff;
}
printf("Total Payoff:%6.2f\n",tPayoff);
printf("Arm Count Avg. Payoff Value\n");
printf(" 0%8.0d%14.2f%8.2lf\n",armArray[0].count,armArray[0].avgPayoff,armArray[0].payoffMean);
for(int i =1; i<numArms;i++){
if(armArray[i].count==0)
printf("%3d%8.0d%14.2f%8.2lf\n",i,0,armArray[i].avgPayoff,armArray[i].payoffMean);
else
printf("%3d%8.0d%14.2f%8.2lf\n",i,armArray[i].count,armArray[i].avgPayoff,armArray[i].payoffMean);
}
}
If more of the code is needed I can add the rest.
Upvotes: 1
Views: 75
Reputation: 14046
printf("%3d%8.0d%14.2f%8.2lf\n",i,0,armArray[i].avgPayoff,armArray[i].payoffMean);
The second format specifier %8.0d
provides a precision of 0
. For %d
the precision gives the minimum number of digits to appear. Since you gave 0
precision it means nothing will be printed for 0 values. There is no need for the precision in this case. So either remove it completely or give a precision of 1.
That is, %8d
or %8.1d
.
As a side note, you don't really need to have that last if/else
condition. The printf
in the else
case is sufficient for both the zero and non-zero cases.
Upvotes: 2