以下代码输出一个表格,显示分数,平均分数,偏差分数和平方偏差。
所需输出的样本(取自homework assignment):
在答案键上,列都很好地对齐,但我无法弄清楚如何让最后一行对齐,所以我需要知道如何对齐列。所有其他列都是对齐的,因为我在printf代码中放了一堆空格,但我需要知道如何将它们与任何输入数据对齐,因为如果偏差分数超过它将推动所有内容并且将不再对齐
#include<stdio.h>
#include<math.h>
float FindMean (float, float, float, float, float);
float Deviation (float, float);
float SqrdDev (float);
float main (void)
{
float num1,num2,num3,num4,num5;
float Meanscore;
float DeviationScore1,DeviationScore2,DeviationScore3,DeviationScore4,DeviationScore5;
float Square1,Square2,Square3,Square4,Square5;
float Sum, variance, StandDev;
printf("Please type the 5 test scores: ");
scanf("%f %f %f %f %f",&num1,&num2,&num3,&num4,&num5);
Meanscore= FindMean (num1,num2,num3,num4,num5);
DeviationScore1=Deviation(num1,Meanscore);
DeviationScore2=Deviation(num2,Meanscore);
DeviationScore3=Deviation(num3,Meanscore);
DeviationScore4=Deviation(num4,Meanscore);
DeviationScore5=Deviation(num5,Meanscore);
Square1=SqrdDev(DeviationScore1);
Square2=SqrdDev(DeviationScore2);
Square3=SqrdDev(DeviationScore3);
Square4=SqrdDev(DeviationScore4);
Square5=SqrdDev(DeviationScore5);
Sum=Square1+Square2+Square3+Square4+Square5;
variance=Sum/5;
StandDev=sqrt(variance);
printf("Score - Mean Score = Deviation Score Squared Deviation\n");
printf("(X) (M) (X-M) (X-M)squared\n");
printf("---------------------------------------------------------\n");
printf("%.0f %.1f %.1f %.2f\n",num1,Meanscore,DeviationScore1,Square1);
printf("%.0f %.1f %.1f %.2f\n",num2,Meanscore,DeviationScore2,Square2);
printf("%.0f %.1f %.1f %.2f\n",num3,Meanscore,DeviationScore3,Square3);
printf("%.0f %.1f %.1f %.2f\n",num4,Meanscore,DeviationScore4,Square4);
printf("%.0f %.1f %.1f %.2f\n",num5,Meanscore,DeviationScore5,Square5);
printf(" Sum: %.2f\n",Sum);
printf("Variance=%.2f/5=%.2f\n",Sum,variance);
printf("Standard deviation=SD=%.2f\n",StandDev);
return 0;
}
float FindMean (float dig1,float dig2,float dig3,float dig4,float dig5)
{
float Meanscore = (dig1+dig2+dig3+dig4+dig5)/5;
return Meanscore;
}
float Deviation (float dig,float Mean)
{
float Deviation = dig-Mean;
return Deviation;
}
float SqrdDev (float Dev)
{
float SqrdDev = Dev*Dev;
return SqrdDev;
}
答案 0 :(得分:1)
关于评论所说的例子;
更改此行:(类似于所有printf行)
printf("%.0f %.1f %.1f %.2f\n",num1,Meanscore,DeviationScore1,Square1);
为此,使用格式填充而不是嵌入空格:
printf("%.0f %10.1f %10.1f %10.2f\n",num1,Meanscore,DeviationScore1,Square1);
格式说明符More ...