如何打印C中的数组中的数据? [英] How to print the data in an array in C?

查看:98
本文介绍了如何打印C中的数组中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在$ C $个cblocks上面的程序写入输入20名到一个数组中,并输入20标志着进入另一个数组。在这个程序中,我要计算平均值,计算出最高分,并检查谁拥有最高分的名字。我想上面的程序,它没有错误编译。但是,当我检查的平均值,它给了充​​分的价值。

例如:如果正确的平均值为48.95,节目输出为49平均

所以,我想解决这个错误,需要在2小数点的平均值。(%。00F ),也是名不能打印。你能帮忙吗?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释的main()
{
INT等级[20];
焦炭名[20];
诠释A,B,C = 1,D = 1,E,F,高,G = 0,同上;
浮动平均,TOT = 0;
用于:(a = 0;一个与所述; 20;一个++)
    {
    的printf(输入名称%D:,C);
    scanf函数(%S,与放大器;名称[A]);
    C ++;
    }
对于(B = 0; B< 20; b ++)
    {
    的printf(输入标记%D:,D);
    scanf函数(%D,急症室);     如果(E> = 0&放大器;&安培; e控制= 100)
        {
            等级[B] = E;
            ð++;
        }
    其他
        {
            的printf(RANGE.PLEASE输入OUT有效数字\\ n);
            b--;
        }
    }
为(F = 0; F&小于20;˚F++)
    {
        TOT = TOT +等级[F]
    }
的printf(达尔%.00f \\ n,TOT);
平均= TOT / 20.00;
的printf(平均为%.00f \\ n,AVG);
高档次= [0];
对于(G = 0; G< 20; G ++)
{
    如果(高<等级[G])
        {
            高档次= [G]。
            LOC =克;
        }
}
的printf(平均为%.00f \\ n该档次最高的是谁拥有最高等级的人%d个\\ n该名:%S,AVG,高,名[LOC]);
的printf(平均为%.00f \\ n,AVG);
}


解决方案

转换说明%。00F 要求小数点后数字0,所以这就是你所得到的。如果你想小数点后2位,然后用%。2F (或者%6.2f

要修复利玛窦帕齐尼在他的<一个制作href=\"http://stackoverflow.com/questions/20816006/how-to-print-a-data-in-a-array-c#comment31212581_20816006\">comment,和处理其他杂项的问题太多,你可以用你的code的类似这样的变异:

 的#include&LT;&stdio.h中GT;INT主要(无效)
{
    INT等级[20];
    炭名称[20] [20];
    int类型的,B,F,高克禄;
    浮动平均,TOT = 0;
    用于:(a = 0;一个与所述; 20;一个++)
    {
        的printf(输入名称%D:,A + 1);
        如果(scanf的(%19S,名称[α])!= 1)
            返回1;
    }
    对于(B = 0; B&LT; 20; b ++)
    {
        INTê;
        的printf(输入标记%D:B + 1);
        如果(scanf函数(%d个,&安培;!E)= 1)
            返回1;        如果(E&GT; = 0&放大器;&安培; e控制= 100)
            等级[B] = E;
        其他
        {
            的printf(RANGE.PLEASE输入OUT有效数字\\ n);
            b--;
        }
    }
    的putchar('\\ n');    用于:(a = 0;一个与所述; 20;一个++)
        的printf(% - 20年代%3D \\ N,名字[A],等级[A]);    为(F = 0; F&小于20;˚F++)
        TOT = TOT +等级[F]    的printf(达尔%6.2f \\ n,TOT);
    平均= TOT / 20.00;
    的printf(平均为%6.2f \\ n,AVG);    高档次= [0];
    LOC = 0;
    对于(G = 0; G&LT; 20; G ++)
    {
        如果(高&LT;等级[G])
        {
            高档次= [G]。
            LOC =克;
        }
    }    的printf(平均为%6.2f \\ n,AVG);
    输出(最高等级为%d \\ n,高);
    的printf(谁拥有最高等级的人的名字是:%S \\ N,名字[LOC]);    返回0;
}

给定的样本输入数据是这样的(文件 ga.marks ),其中由两个程序生成的数据:

 学生-01
学生-02
学生-03
学生-04
学生-05
学生-06
学生-07
学生-08
学生-09
学生-10
学生-11
学生-12
学生-13
学生-14
学生-15
学生-16
学生-17
学生-18
学生-19
学生-20
49
27
47
46
33
84
63
51
61
91
82
60
39
57
65
60
19
88
47
61

这是从运行的程序(名为 GA )作为输出: GA&LT; ga.marks (因为我不会坐在那里打字20名和20个数字输入到程序):

 输入名称1:输入名称2:输入名称3:输入名称4:输入名称5:输入名称6:输入姓名7:输入姓名8:输入名称9:输入名称10:输入名称11:输入名称12:输入名称13:输入名称14:输入名称15:输入名称16:输入名称17:输入名称18:输入名称19:输入名称20:输入商标1:输入标记2:输入商标3​​:输入商标4:输入商标5:输入商标6:输入商标7:输入商标8:输入商标9:输入标志10:输入标记11:输入标记12:输入标记13:输入标记14:输入商标15:输入标记16:输入标记17:输入标记18:输入标记19:输入标记20:
学生01 49
学生02 27
学生03 47
学生04 46
学生05 33
学生-06 84
学生07 63
学生-08 51
学生-09 61
学生10 91
学生11 82
学生12 60
学生13 39
学生14 57
学生15 65
学生16 60
学生17 19
学生18 88
学生19 47
学生20 61
道达尔是1130.00
平均是56.50
平均值为56.50
最高等级为91
谁拥有最高等级的人的名字是:学生-10

请注意,该提示是适得其反从文件输入。在code。在输入的结尾呼应其输入。它计算等等。我没有刻意去调整数据,以产生一个48.95平均分。

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.

For example: if the correct average is 48.95, program outputs 49 as average.

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?

#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. If you want 2 digits after the decimal point, then use %.2f (or maybe %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:

#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:

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):

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.

这篇关于如何打印C中的数组中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆