Problem with C matrices

Asked

Viewed 385 times

4

I have to do a program that will read 4 grades from each of the 6 students in a class and store them in a matrix NOTES[6][5]. For each student, the arithmetic mean of the 4 grades should be calculated and stored 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 went to execute, at the time of picking up STUDENT 6 GRADE 2, instead of being typed STUDENT 6, it types a totally random number, and ends the program, and depending, the code of the different errors!

What mistake I’m 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;
}

2 answers

2


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 has to be zero.

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

Sum of banknotes 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;

the output loops are on your own.

1

In C as the for should never begin in 1 and go up to N when working with vectors and matrices, because the size of the vector or matrix goes from 0 until N-1.

If you have a 6x5 matrix, I am for i must go from 0 to 5 and the j de 0 to 4.

The error is because you are certainly trying to write in a non-existent position of your matrix.

  • I packed up the for, and even so it is giving random numbers, and for all students, I used the same grade and gave different values and even and also using the %.2f for the average of all students, it seems that the formatting was annulled. Student average 0 = 3.75 Student average 1 = 11.25 Student average 2 = 15.25 Student average 3 = 20.75 Student average 4 = 16369920226015860000000000.00 Student average: 2728320037669310000000000.00

Browser other questions tagged

You are not signed in. Login or sign up in order to post.