0

Each row without last row is person's score at math, eng, kor, science.

I mean that arr[0][0] = person 1's math score:

arr[0][1] = person 1's eng score
arr[0][2] = person 1's kor score
arr[0][3] = person 1's science score
arr[0][4] = person 1's sum at each subject score
and arr[4][0] = sum at person 1,2,3,4's math score

and this code is run at vscode in mac

#include <stdio.h>

int main(void)
{
    long i,j;
    int arr[5][5];

    printf("성적관리 프로그램 입니다.\n");
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            switch (j)
            {
                case 0:
                    printf("국어 점수를 입력해 주세요: \n");
                    scanf("%d",&arr[i][j]);
                    break;
                case 1:
                    printf("영어 점수를 입력해 주세요: \n");
                    scanf("%d",&arr[i][j]);
                    break;
                case 2:
                    printf("수학 점수를 입력해 주세요: \n");
                    scanf("%d",&arr[i][j]);
                    break;
                case 3:
                    printf("국사 점수를 입력해 주세요: \n");
                    scanf("%d",&arr[i][j]);
                    break;
                default:
                    printf("오류 발생");
            }
            
        }
    }
    printf("개인 별 총점을 구하겠습니다.\n");
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            arr[i][4] += arr[i][j];
        }
    }
    printf("과목 별 총점을 구하겠습니다.\n");
    
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
        {
            arr[4][i] += arr[j][i];
        }
    }

    for(i=0; i<4; i++)
    {
        arr[4][4] += arr[4][i];
    }
    
    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            printf("%3d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;

}

The result is:

  1   2   3   4  10 
  1   2   3   4  10 
  1   2   3   4  11 
  1   2   3   4 1874065002 
  5 6040476 708575245 2898508 71751423
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
chobodark
  • 25
  • 4
  • 1
    You should initialize the values in the array to 0 before adding to them. – 001 Jun 24 '22 at 15:12
  • 1
    See: [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/a/1597426) – 001 Jun 24 '22 at 15:22

1 Answers1

1

Your problem is that you aren't initializing the array elements to 0, so there are reminders in memory that not reset to 0, consider doing this after the array declaration:

for (i = 0; i < 5; i++)
{
    for (j = 0; j < 5; j++)
    {
        arr[i][j] = 0;
    }
}

or the {} shortcut at definition:

int arr[5][5] = {};