-2
This exercise required to store 4 grades of 4 students and then media put on a vector and present, well, I declared the matrix and instead of typing [4][4] I put two variables const int worth 4, and the program rejected, Does anyone know why this happened and if I’m wrong to put a constant instead of a number? error giving is variable-sized Object may not be initialized, I created the variable bbimestre because bimestre is constant
#include <stdio.h>
int main (){
const int Bimestre = 4;
const int Numeroaluno = 4;
float AlunoBimestre [Bimestre][Numeroaluno] = {0};
int media = 0;
float MediaAluno[Bimestre] = {0};
printf("Digite as notas do aluno 1");
for (int aluno = 0;aluno <Bimestre;aluno++){
for (int bbimestre = 0; bbimestre < Numeroaluno; bimestre++){
scanf("%f", &AlunoBimestre[aluno][bbimestre]);
media = media + AlunoBimestre[aluno][bimestre];
}
MediaAluno[aluno] = media / 4;
media = 0;
if (aluno!=3)
printf("Digite as notas do aluno %i \n", aluno + 2);
}
for (aluno = 0; aluno <= 4; aluno++){
printf("A media do aluno %i eh %f",aluno + 1, MediaAluno[aluno]);
printf("\n");
}
return 0;
}
Say
o programa rejeitou
is something very vague, post which error the compiler reported. In the code that posted there is a confusion betweenbbimestre
andbimestre
and I believe that an exchange ofBimestre
withNumeroaluno
which in this particular case does not cause side effects because the variables have the same value.– anonimo
error giving is variable-sized Object may not be initialized, I created the variable bbimestre because bimestre is constant
– Cl2727
If you declared your array with dynamic dimensions you cannot initialize it in the declaration. Note that
bimestre
is different fromBimestre
, because C is case sensitive. See working on: https://ideone.com/fLGJT3– anonimo
Ahh, if I don’t put any value on the vector then that gives the possibility to put value after using a loop is for example, but if I declare and put value at the same time I can no longer change the values within the vector?
– Cl2727
The problem is when this is done, during the compilation or at the start of the program load. A statement of an array of dynamic dimensions does not have its dimensions known during compilation.
– anonimo
Note that assigning values in the declaration of a variable, including arrays, does not prevent these values from being modified during program execution, except, of course, if the variable has been declared with
const
.– anonimo