Problem with arrays in C

2

I have to make a program that will read 4 notes from each of the 6 students in a class and store them in an array NOTES [6] [5]. For each student, one should calculate the arithmetic mean of the 4 notes and store this average in the last column of the matrix (index column 4). The program should also print the average of each student and, finally, the arithmetic average of the class.

I made a code and was executed, at the time of getting STUDENT 6 NOTE 2, instead of being typed STUDENT 6, it types a totally random number, and ends the program, and depending on the code of the different errors! / p>

What error am I making?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main() {
setlocale(LC_ALL, "Portuguese");
float nota[6][5], media;
int i, j;
media = 0;

for(i=1; i<=6; i++) {
    for(j=1; j<=5; j++) {
        printf("\nAluno %d Nota %d \n", i, j);
        scanf("%f", &nota[i][j]);
    }
}

for(i=1;i<=6;i++)
    {
    for(j=1;j<= 5;j++) {
        printf("%f", nota[i][j]);
    }
    printf("\n");
}

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

for(i=0; i<=6; i++) {
    nota[i][5] = nota[i][5]/4;
    media = nota[i][5] + media;
}

media = media/6;
for(i=0; i<=6; i++) {
    printf("\nA média do aluno %d = %.2f", i, nota[i][5]);
}
printf("\nA média dos alunos: %.2f", media);

return 0;
}
    
asked by anonymous 10.05.2018 / 02:18

2 answers

1

All loops are wrong indexes.

first loop

int n_alunos = 6;
int n_notas = 4;
for(i=0; i != n_alunos; i++) {
  for(j=0; j != n_notas; j++) {
    printf("\nAluno %d Nota %d \n", i, j);
    scanf("%f", &nota[i][j]);
  }
}

The initial value of the sum must be zero.

int j_media = 4;
for(i = 0; i != n_alunos; ++i)
{
  nota[i][j_media] = 0.0;
}

Sum of notes and average

for(i=0; i != n_alunos; ++i) {
  // soma das notas
  for(j = 0; j != n_notas; j++) {
    nota[i][j_media] = nota[i][j_media] + nota[i][j];
  }
  // média do aluno
  nota[i][j_media] = nota[i][j_media] / n_notas;
}

class average

for(i = 0; i!=n_alunos; i++) {
  media = nota[i][j_media] + media;
}
media = media / n_alunos;

output loops are on your own.

    
10.05.2018 / 03:57
0

In C the for should never start in 1 and go to N when working with vectors and arrays because the size of the vector or array goes from 0 to N-1 .

If you have a 6x5 array, I am for hi must go from 0 to 5 from 0 to 4 .

The error is because you are certainly trying to write in a nonexistent position in your array.

    
10.05.2018 / 02:26