【问题标题】:Persistent segmentation fault in student grading array program学生评分数组程序中的持续分段错误
【发布时间】:2014-01-19 22:25:11
【问题描述】:

创建一个程序,首先为四项作业获得最大分数。然后它必须为这四项作业计入七名学生的分数。然后程序必须输出 7 名学生中的每一个在可用总分上的得分。我无法弄清楚这个讨厌的分割错误。程序编译和输入可用于四个作业的最高分工作得很好,当我尝试输入学生分数时会出现分段。任何帮助将不胜感激!

#include<stdio.h>

int main (void)
{
    int array[4][8];
    int max, rows, cols, count;

    printf("Please enter the maximum points available for the four assignment");
    printf(" (add a space behind each and return when finished): \n");
    scanf("%d %d %d %d", &array[0][0], &array[1][0], &array[2][0], &array[3][0]);

    max=array[0][0]+array[1][0]+array[2][0]+array[3][0];

    printf("Please enter each students set of scores"); 
    printf(" (return after each individual score): \n");

    for(cols=1; cols<8; cols++)
    {
            for(rows=0; rows<4; rows++)
            {
                    scanf("%d", array[rows][cols]);
            }
    }

    for(count=1; count<8; count++)
    {
            for(cols=1; cols<8; cols++)
            {
            printf("The points for student #%d, count");
            printf(" (%d / %d)",array[0][cols]+array[1][cols]+array[2][cols]+array[3][cols], max);
            printf("\n");
            }
    }


    return 0;

}

【问题讨论】:

  • 在 for() 循环中您的 cols 从 1 开始,但您正在存储/访问 scanf 和 max 行中的 0 列的任何原因?
  • scanf("%d", array[rows][cols]); --> scanf("%d", &amp;array[rows][cols]);
  • @MarcB cols:0 已使用。
  • 太棒了,谢谢大家。不得不更改我的第二组 for 循环,因为程序将为每个学生编号输出所有 7 组分数,总共输出 49 行而不是 7 行。现在它完美运行,再次感谢!

标签: c arrays loops for-loop


【解决方案1】:

你的一个引语放错了地方。改变

printf("The points for student #%d, count");

printf("The points for student #%d", count);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2018-11-02
    • 2023-03-10
    相关资源
    最近更新 更多