C - 使用printf显示完整输出

时间:2018-03-24 07:13:51

标签: c arrays loops printf output

我有一个程序,应该为不同的学生捕捉不同课程的标记,然后显示获得的最高分。

它显示最高分,但我试图让它输出如下内容:

"最高的是[课程]中的[学生姓名] [mark]"

我想知道是否有办法获得那种类型的输出

这是我的代码:

#include <stdio.h>

void main() {
    const char STUDENTS = 3;
    const char COURSES = 3;
    char students[][15]= { "James", "Keith", "Jonathan" };  
    char courses[][6] = { "C++", "VB", "Java" };
    int marks[STUDENTS][COURSES];
    int i, j;
    int highest;

    for (i = 0; i < STUDENTS; i++) {
        for (j = 0; j < COURSES; j++) {
            printf("Enter marks for %s for %s: ", students[i], courses[j]);
            scanf("%d", &marks[i][j]);  
        }
    }

    printf("\nResults: \n");
    for (i = 0; i < STUDENTS; i++) {
        printf("\n%s: \t\n", students[i]);
        for (j = 0; j < COURSES; j++) {
            printf("%s: ", courses[j]);
            printf("%d\t\t", marks[i][j]);          
        }
        printf("\n");   
    }

    highest = marks[0][0];

    for (i = 0; i < STUDENTS; i++) {
        for (j = 0; j < COURSES; j++) {
            if (marks[i][j] > highest) {
                highest = marks[i][j];
            }
         }
    }
    printf("\nHighest was %s in %s with %d", students[i], courses[j], highest);

    getch(); 
}

2 个答案:

答案 0 :(得分:3)

当您找到最高分时,您需要保存ij的值:

highest = marks[0][0];

int highStudent = 0;
int highCourse = 0;

for (i = 0; i < STUDENTS; i++) {
    for (j = 0; j < COURSES; j++) {
        if (marks[i][j] > highest) {
            highest = marks[i][j];
            highStudent = i;
            highCourse = j;
        }

    }
}

printf("\nHighest was %s in %s with %d\n", students[highStudent], courses[highCourse], highest);

答案 1 :(得分:2)

正如Stephen Docy所写,你必须保存最高分的索引,或者保留最佳分数的名称和路线。

在编码时,在第三个嵌套循环的末尾i == STUDENTSj == COURSES,所以students[i]courses[j]指的是超出这些数组边界的元素。虽然这些地址适合与指向相同数组的指针进行比较,但是将它们传递给printf会导致printf具有未定义的行为,因为它将取消引用它们作为指向C字符串的指针。

另请注意,您的代码中还有其他问题:

  • 您应该测试scanf()的返回值,以避免在无效输入时出现未定义的行为。
  • main的返回类型为int,而不是void
  • 类型char容易出错,因为数组的大小。您可以使用intsize_t或宏来移植旧C标准,或者从阵列本身计算这些大小。
  • 您的尺寸和数组studentscourses只是巧合而不是构造相同。增加STUDENTS COURSES与对应数组不同步会产生未定义的行为。
  • 而不是非便携式功能getch(),您可以使用scanf("%*2[\n]")来读取待处理的换行符并等待用户再次点击回车键。

以下是修改后的版本:

#include <stdio.h>

int main() {
    char students[][15] = { "James", "Keith", "Jonathan" };  
    char courses[][6] = { "C++", "VB", "Java" };
    const int STUDENTS = sizeof(students) / sizeof(students[0]);
    const int COURSES = sizeof(courses) / sizeof(courses[0]);
    int marks[STUDENTS][COURSES];
    int i, j, highest, best_student, best_course;

    for (i = 0; i < STUDENTS; i++) {
        for (j = 0; j < COURSES; j++) {
            printf("Enter marks for %s for %s: ", students[i], courses[j]);
            if (scanf("%d", &marks[i][j]) != 1) {
                fprintf(stderr, "invalid input\n");
                return 1;
            }
        }
    }

    printf("\nResults:\n");
    for (i = 0; i < STUDENTS; i++) {
        printf("\n%s:\n", students[i]);
        for (j = 0; j < COURSES; j++) {
            printf("%s: ", courses[j]);
            printf("%d\t\t", marks[i][j]);          
        }
        printf("\n");   
    }

    highest = marks[0][0];
    best_student = best_course = 0;
    for (i = 0; i < STUDENTS; i++) {
        for (j = 0; j < COURSES; j++) {
            if (marks[i][j] > highest) {
                highest = marks[i][j];
                best_student = i;
                best_course = j;
            }
         }
    }
    printf("\nHighest was %s in %s with %d\n",
           students[best_student], courses[best_course], highest);

    scanf("%*2[\n]");
    return 0;
}