Const int instead of number in the vector in C

Asked

Viewed 35 times

-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;
}
  • 1

    Say o programa rejeitou is something very vague, post which error the compiler reported. In the code that posted there is a confusion between bbimestre and bimestre and I believe that an exchange of Bimestre with Numeroaluno which in this particular case does not cause side effects because the variables have the same value.

  • error giving is variable-sized Object may not be initialized, I created the variable bbimestre because bimestre is constant

  • 1

    If you declared your array with dynamic dimensions you cannot initialize it in the declaration. Note that bimestre is different from Bimestre, because C is case sensitive. See working on: https://ideone.com/fLGJT3

  • 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?

  • 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.

  • 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.

Show 1 more comment

1 answer

0

#include <stdio.h>

int main (){

const int Bimestre  = 4;
const int Numeroaluno = 4;

float AlunoBimestre [Bimestre][Numeroaluno];
int media = 0;
float MediaAluno[Bimestre];

printf("Digite as notas do aluno 1");
int aluno = 0;

for (int aluno = 0;aluno <Bimestre;aluno++){
    for (int bbimestre = 0; bbimestre < Numeroaluno; bbimestre++){
    scanf("%f", &AlunoBimestre[aluno][bbimestre]);
    media = media + AlunoBimestre[aluno][bbimestre];
    }
    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;
}

Browser other questions tagged

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