I have written above program in CODEBLOCKS to input 20 names into an array and input 20 marks into another array. In this program, I want to calculate the average, calculate the highest mark and check the name who has the highest mark. I tried above program and it compiled without error. But when I check the average, it gives full value.
我在CODEBLOCKS中写了上面的程序,在数组中输入20个名字,在另一个数组中输入20个标记。在这个程序中,我想计算平均值,计算最高标记并检查具有最高标记的名称。我尝试了上面的程序,它编译没有错误。但是当我检查平均值时,它会给出全部价值。
For example: if the correct average is 48.95, program outputs 49 as average.
例如:如果正确的平均值为48.95,则程序输出49为平均值。
So, I want to solve that error and need the average in 2 decimal points.(%.00f
) and also the printing of the name doesn't work. Can you help?
所以,我想解决这个错误,并需要2个小数点的平均值。(%。00f)以及名称的打印不起作用。你能帮我吗?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int grades[20];
char names[20];
int a,b,c=1,d=1,e,f,high,g=0,loc;
float avg,tot=0;
for(a=0;a<20;a++)
{
printf("Input Name %d : ",c);
scanf("%s",&names[a]);
c++;
}
for(b=0;b<20;b++)
{
printf("Input Marks %d : ",d);
scanf("%d",&e);
if(e>=0&&e<=100)
{
grades[b]=e;
d++;
}
else
{
printf("OUT OF RANGE.PLEASE INPUT A VALID NUMBER.\n");
b--;
}
}
for(f=0;f<20;f++)
{
tot=tot+grades[f];
}
printf("Total is %.00f\n",tot);
avg=tot/20.00;
printf("Average is %.00f\n",avg);
high=grades[0];
for(g=0;g<20;g++)
{
if(high<grades[g])
{
high=grades[g];
loc=g;
}
}
printf("The average is %.00f\nThe highest grade is %d\nThe name of the person who has the highest grade is :%s",avg,high,names[loc]);
printf("The average is %.00f\n",avg);
}
3 个解决方案
#1
3
The conversion specification %.00f
requests 0 digits after the decimal point, so that's what you get. If you want 2 digits after the decimal point, then use %.2f
(or maybe %6.2f
).
转换规范%.00f请求小数点后的0位数,这就是你得到的。如果你想要小数点后2位数,那么使用%。2f(或者可能是%6.2f)。
To fix the point that Matteo Pacini made in his comment, and deal with miscellaneous other issues too, you could use a variant of your code similar to this:
为了解决Matteo Pacini在评论中提出的观点,并处理其他各种问题,您可以使用与此类似的代码变体:
#include <stdio.h>
int main(void)
{
int grades[20];
char names[20][20];
int a, b, f, high, g, loc;
float avg, tot = 0;
for (a = 0; a < 20; a++)
{
printf("Input Name %d: ", a+1);
if (scanf("%19s", names[a]) != 1)
return 1;
}
for (b = 0; b < 20; b++)
{
int e;
printf("Input Marks %d: ", b+1);
if (scanf("%d", &e) != 1)
return 1;
if (e >= 0 && e <= 100)
grades[b] = e;
else
{
printf("OUT OF RANGE.PLEASE INPUT A VALID NUMBER.\n");
b--;
}
}
putchar('\n');
for (a = 0; a < 20; a++)
printf("%-20s %3d\n", names[a], grades[a]);
for (f = 0; f < 20; f++)
tot = tot + grades[f];
printf("Total is %6.2f\n", tot);
avg = tot / 20.00;
printf("Average is %6.2f\n", avg);
high = grades[0];
loc = 0;
for (g = 0; g < 20; g++)
{
if (high < grades[g])
{
high = grades[g];
loc = g;
}
}
printf("The average is %6.2f\n", avg);
printf("The highest grade is %d\n", high);
printf("The name of the person who has the highest grade is: %s\n", names[loc]);
return 0;
}
Given sample input data like this (file ga.marks
), where the data was generated by two programs:
给定样本输入数据(文件ga.marks),数据由两个程序生成:
Student-01
Student-02
Student-03
Student-04
Student-05
Student-06
Student-07
Student-08
Student-09
Student-10
Student-11
Student-12
Student-13
Student-14
Student-15
Student-16
Student-17
Student-18
Student-19
Student-20
49
27
47
46
33
84
63
51
61
91
82
60
39
57
65
60
19
88
47
61
This is the output from running the program (named ga
) as: ga < ga.marks
(because I am not going to sit around typing 20 names and 20 numbers as input to a program):
这是运行程序(命名为ga)的输出:ga
Input Name 1: Input Name 2: Input Name 3: Input Name 4: Input Name 5: Input Name 6: Input Name 7: Input Name 8: Input Name 9: Input Name 10: Input Name 11: Input Name 12: Input Name 13: Input Name 14: Input Name 15: Input Name 16: Input Name 17: Input Name 18: Input Name 19: Input Name 20: Input Marks 1: Input Marks 2: Input Marks 3: Input Marks 4: Input Marks 5: Input Marks 6: Input Marks 7: Input Marks 8: Input Marks 9: Input Marks 10: Input Marks 11: Input Marks 12: Input Marks 13: Input Marks 14: Input Marks 15: Input Marks 16: Input Marks 17: Input Marks 18: Input Marks 19: Input Marks 20:
Student-01 49
Student-02 27
Student-03 47
Student-04 46
Student-05 33
Student-06 84
Student-07 63
Student-08 51
Student-09 61
Student-10 91
Student-11 82
Student-12 60
Student-13 39
Student-14 57
Student-15 65
Student-16 60
Student-17 19
Student-18 88
Student-19 47
Student-20 61
Total is 1130.00
Average is 56.50
The average is 56.50
The highest grade is 91
The name of the person who has the highest grade is: Student-10
Note that the prompts are counter-productive for input from file. The code echoes its input at the end of the input. It does the calculations etc. I didn't bother to tune the data to produce a mean score of 48.95.
请注意,提示对于从文件输入会适得其反。代码在输入结束时回显其输入。它进行计算等。我没有费心调整数据以产生48.95的平均分数。
#2
1
Use printf("%.2f",avg)
for 2 digits after the decimal point. You are using printf("%.00f",avg)
which means 0 digits after the decimal point.
使用printf(“%。2f”,avg)小数点后的2位数。您使用的是printf(“%。00f”,avg),表示小数点后的0位数。
#3
1
Your calculations are correct. Just the printing format is messed up.
你的计算是正确的。只是打印格式搞砸了。
Use the code below rather than %.00f
:
使用下面的代码而不是%.00f:
%.2f
To fix your name problem you have many ways. One possibility is as below:
要解决您的姓名问题,您有很多方法。一种可能性如下:
char names[20][50];
This means you have 20 names with each length 50 max.
这意味着你有20个名字,每个长度最多50个。
scanf("%s", names[a]);
Also you can change your scanf
as above. Remove &
.
您也可以像上面那样更改您的scanf。去掉 &。
#1
3
The conversion specification %.00f
requests 0 digits after the decimal point, so that's what you get. If you want 2 digits after the decimal point, then use %.2f
(or maybe %6.2f
).
转换规范%.00f请求小数点后的0位数,这就是你得到的。如果你想要小数点后2位数,那么使用%。2f(或者可能是%6.2f)。
To fix the point that Matteo Pacini made in his comment, and deal with miscellaneous other issues too, you could use a variant of your code similar to this:
为了解决Matteo Pacini在评论中提出的观点,并处理其他各种问题,您可以使用与此类似的代码变体:
#include <stdio.h>
int main(void)
{
int grades[20];
char names[20][20];
int a, b, f, high, g, loc;
float avg, tot = 0;
for (a = 0; a < 20; a++)
{
printf("Input Name %d: ", a+1);
if (scanf("%19s", names[a]) != 1)
return 1;
}
for (b = 0; b < 20; b++)
{
int e;
printf("Input Marks %d: ", b+1);
if (scanf("%d", &e) != 1)
return 1;
if (e >= 0 && e <= 100)
grades[b] = e;
else
{
printf("OUT OF RANGE.PLEASE INPUT A VALID NUMBER.\n");
b--;
}
}
putchar('\n');
for (a = 0; a < 20; a++)
printf("%-20s %3d\n", names[a], grades[a]);
for (f = 0; f < 20; f++)
tot = tot + grades[f];
printf("Total is %6.2f\n", tot);
avg = tot / 20.00;
printf("Average is %6.2f\n", avg);
high = grades[0];
loc = 0;
for (g = 0; g < 20; g++)
{
if (high < grades[g])
{
high = grades[g];
loc = g;
}
}
printf("The average is %6.2f\n", avg);
printf("The highest grade is %d\n", high);
printf("The name of the person who has the highest grade is: %s\n", names[loc]);
return 0;
}
Given sample input data like this (file ga.marks
), where the data was generated by two programs:
给定样本输入数据(文件ga.marks),数据由两个程序生成:
Student-01
Student-02
Student-03
Student-04
Student-05
Student-06
Student-07
Student-08
Student-09
Student-10
Student-11
Student-12
Student-13
Student-14
Student-15
Student-16
Student-17
Student-18
Student-19
Student-20
49
27
47
46
33
84
63
51
61
91
82
60
39
57
65
60
19
88
47
61
This is the output from running the program (named ga
) as: ga < ga.marks
(because I am not going to sit around typing 20 names and 20 numbers as input to a program):
这是运行程序(命名为ga)的输出:ga
Input Name 1: Input Name 2: Input Name 3: Input Name 4: Input Name 5: Input Name 6: Input Name 7: Input Name 8: Input Name 9: Input Name 10: Input Name 11: Input Name 12: Input Name 13: Input Name 14: Input Name 15: Input Name 16: Input Name 17: Input Name 18: Input Name 19: Input Name 20: Input Marks 1: Input Marks 2: Input Marks 3: Input Marks 4: Input Marks 5: Input Marks 6: Input Marks 7: Input Marks 8: Input Marks 9: Input Marks 10: Input Marks 11: Input Marks 12: Input Marks 13: Input Marks 14: Input Marks 15: Input Marks 16: Input Marks 17: Input Marks 18: Input Marks 19: Input Marks 20:
Student-01 49
Student-02 27
Student-03 47
Student-04 46
Student-05 33
Student-06 84
Student-07 63
Student-08 51
Student-09 61
Student-10 91
Student-11 82
Student-12 60
Student-13 39
Student-14 57
Student-15 65
Student-16 60
Student-17 19
Student-18 88
Student-19 47
Student-20 61
Total is 1130.00
Average is 56.50
The average is 56.50
The highest grade is 91
The name of the person who has the highest grade is: Student-10
Note that the prompts are counter-productive for input from file. The code echoes its input at the end of the input. It does the calculations etc. I didn't bother to tune the data to produce a mean score of 48.95.
请注意,提示对于从文件输入会适得其反。代码在输入结束时回显其输入。它进行计算等。我没有费心调整数据以产生48.95的平均分数。
#2
1
Use printf("%.2f",avg)
for 2 digits after the decimal point. You are using printf("%.00f",avg)
which means 0 digits after the decimal point.
使用printf(“%。2f”,avg)小数点后的2位数。您使用的是printf(“%。00f”,avg),表示小数点后的0位数。
#3
1
Your calculations are correct. Just the printing format is messed up.
你的计算是正确的。只是打印格式搞砸了。
Use the code below rather than %.00f
:
使用下面的代码而不是%.00f:
%.2f
To fix your name problem you have many ways. One possibility is as below:
要解决您的姓名问题,您有很多方法。一种可能性如下:
char names[20][50];
This means you have 20 names with each length 50 max.
这意味着你有20个名字,每个长度最多50个。
scanf("%s", names[a]);
Also you can change your scanf
as above. Remove &
.
您也可以像上面那样更改您的scanf。去掉 &。