从多个循环输入打印输出

时间:2016-10-19 10:58:32

标签: c

我很抱歉这个奇怪的标题,但英语不是我的第一语言,所以我不知道如何具体说明。 我刚刚学习了2周的编码,所以我在这个主题上是一个新手。

所以,让我们开始解决我的问题。

这是我应该编程的。 Correct Output

但到目前为止,这是我能做的。 My Ouput

这是我的代码:

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

我的问题是,如何制作表格,以便循环后可以打印循环内部的输入?

如果你有更好的主意,请告诉我。

我已经尝试将桌子放在循环中,但它根本不是你可能已经知道的帮助。

1 个答案:

答案 0 :(得分:0)

我想,你想做的是先获得学生数量,然后得到他们的名字和分数,最后计算平均值,最大值,最小值等,对吗?

最佳方法是使用结构。既然你是新人,你可以做的是:

  1. 创建一个2d的字符数组:

    char name[100][100], marks[100].
    
  2. 获取变量n中的学生人数。

  3. 使用循环获取名称和标记:

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

  5. 对于avg,max和min,迭代标记,如:

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