gpt4 book ai didi

c - 如何在C中打印数组中的数据?

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:01 25 4
gpt4 key购买 nike

我在 CODEBLOCKS 中编写了上面的程序,将 20 个名称输入到一个数组中,并将 20 个标记输入到另一个数组中。在这个程序中,我想计算平均数,计算最高分并检查获得最高分的名字。我试过上面的程序,它编译没有错误。但是当我检查平均值时,它给出了全部值。

例如:如果正确的平均值是 48.95,则程序输出 49 作为平均值。

所以,我想解决这个错误,需要 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);
}

最佳答案

转换规范%.00f要求小数点后 0 位,所以这就是你得到的。如果你想要小数点后 2 位数字,那么使用 %.2f (或者可能是 %6.2f )。

要解决 Matteo Pacini 的问题在他的comment ,并处理其他杂项问题,您可以使用与此类似的代码变体:

#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;
}

给定这样的示例输入数据(文件 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

这是运行程序(名为 ga)的输出:ga < ga.marks (因为我不会坐着输入 20 个名字和 20 个数字作为程序的输入):

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

请注意,提示对于从文件输入会适得其反。代码在输入的末尾回显其输入。它进行计算等。我没有费心调整数据以产生 48.95 的平均分。

关于c - 如何在C中打印数组中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com