简体   繁体   English

打印来自多个循环输入的输出

[英]Printing outputs from multiple looping input

I'm sorry for the weird title but english isnt my first language so I don't know how to make it specific. 很抱歉我的标题很奇怪,但是英语不是我的第一语言,所以我不知道如何具体化。 I just learned coding for 2 weeks, so I'm a total newbie in this subject. 我刚刚学习了2个星期的编码,所以我是这个学科的新手。

So, let get started with my problem. 因此,让我们开始解决我的问题。

this is what I'm supposed to program. 这就是我应该编程的东西。 正确的输出

But so far, this is what I can do. 但是到目前为止,这是我能做的。 我的美食

and this is my code : 这是我的代码:

#include <stdio.h> 
#include <string.h>
int main() {

float stud_total;
float stud_score;
char stud_name[50];
float stud_total_score = 0;
char max_name[50] = "test";
float max_score = 55;
float min_score = 50;
float avg_score;
char temp_name[50] = "dwdw";

//Start of Total Student between 2-10 looping
do {

    printf("Input Total Student [2..10] : ");
    scanf("%f", &stud_total);fflush(stdin);
} while (stud_total < 2 || stud_total>10);
//end of Total student looping
printf("\n");


//Start Name & Score Input Looping
for (int i = 1;i <= stud_total;i++) {

    do {
        printf("Input Name for Student %d [3..50] : ", i);
        scanf("%s", stud_name);fflush(stdin);
    } while (strlen(stud_name) < 3 || strlen(stud_name) > 50);

    do {
        printf("Input Score for Student %d [0..100] : ", i);
        scanf("%f", &stud_score);fflush(stdin);
    } while (stud_score < 0 || stud_score>100);

    if (stud_score < max_score) {
        min_score = stud_score;
        memset(min_name, '\0', sizeof(min_name));
        strcpy(stud_name, stud_name);
        strcpy(min_name, stud_name);
    }
    else if (stud_score >= min_score) {
        max_score = stud_score;
        memset(max_name, '\0', sizeof(max_name));
        strcpy(stud_name, stud_name);
        strcpy(max_name, stud_name);
    }

    stud_total_score = stud_total_score + stud_score;

    memset(temp_name, '\0', sizeof(temp_name));
    strcpy(stud_name, stud_name);
    strcpy(temp_name, stud_name);
}

//Math part
avg_score = stud_total_score / stud_total;
//END of Name & Score Input Looping

    //Start of Score Table

for (int k = 1;k <= 50;++k) {
    printf("-");
}
printf("\n%-4s %-32s %3s\n", "No.", "Nama", "Nilai");

for (int k = 1;k <= 50;++k) {
    printf("-");
}
for (int j = 1;j <= stud_total;++j) {
    printf("\n%-4d %-32s %3.2f", j, temp_name, stud_score);
}



//END of Score Table 




//Start of conlusion
printf("\n");
printf("\n");
printf("Highest score is %.2f achieved by %s\n", max_score, max_name);
printf("Lowest score is %.2f obtained by %s \n", min_score, min_name);
printf("Student Average : %.2f", avg_score);

//end of conclusion



getchar();
getchar();
return 0;}

My question is, how do I make the table so that the input from inside the looping can be printed after the looping? 我的问题是,如何制作表格,以便在循环后可以打印来自循环内部的输入?

or if you have better idea please tell me. 或者,如果您有更好的主意,请告诉我。

I've tried putting the table inside the loop but it doesn't help at all as you may already know. 我已经尝试过将表放入循环中,但是您可能已经知道这完全没有帮助。

I think, what you want to do is first get number of students, then get their name and marks, finally compute avg, max, min etc, right? 我想,您想做的是首先获取学生人数,然后获取他们的姓名和分数,最后计算平均值,最大值,最小值等,对吗?

Best approach is using structures. 最好的方法是使用结构。 Since you are new, what you can do is: 由于您是新手,因此您可以执行以下操作:

  1. Create a 2d array of chars: 创建一个二维字符数组:

     char name[100][100], marks[100]. 
  2. Get number of students in a variable n . 获取变量n的学生n

  3. Use a loop to get names and marks: 使用循环获取名称和标记:

     for(i=0;i < n;i++){scanf("%s",name[i]);} scanf("%d",marks[i]); 
  4. Now you can print it using the same loop, change scanf to printf. 现在您可以使用相同的循环打印它,将scanf更改为printf。

  5. For avg, max and min, iterate through marks, like: 对于avg,max和min,遍历标记,例如:

     marks = 0; for(i=0;i< n;i++){sum = sum + marks[i];} printf("%d",marks); 

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

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