简体   繁体   English

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

[英]How to print the data in an array in C?

I have written above program in CODEBLOCKS to input 20 names into an array and input 20 marks into another array. 我已经在CODEBLOCKS中编写了上面的程序,以将20个名称输入一个数组并将20个标记输入另一个数组。 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. 但是当我检查平均值时,它给出了全部价值。

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. 因此,我想解决该错误,并且需要2个小数点的平均值。( %.00f )并且名称的打印不起作用。 Can you help? 你能帮我吗?

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

The conversion specification %.00f requests 0 digits after the decimal point, so that's what you get. 转换规范%.00f要求小数点后有0位数字,这就是您得到的。 If you want 2 digits after the decimal point, then use %.2f (or maybe %6.2f ). 如果要在小数点后两位,请使用%.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 < 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

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的平均得分。

Use printf("%.2f",avg) for 2 digits after the decimal point. 在小数点后2位使用printf("%.2f",avg) You are using printf("%.00f",avg) which means 0 digits after the decimal point. 您正在使用printf("%.00f",avg) ,它表示小数点后0位数。

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. 您也可以如上所述更改scanf Remove & . 删除&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM