#include <stdio.h>
int main() {
int notas[5];
notas[0] = 1;
notas[1] = 4;
notas[2] = 7;
notas[3] = 5;
notas[4] = 10;
for(int i = 0; i < 5; i++) {
printf("Nota: %d\n", notas[i]);
}
}
#include <stdio.h>
int main() {
int notas[5];
notas[0] = 1;
notas[1] = 4;
notas[2] = 7;
notas[3] = 5;
notas[4] = 10;
for(int i = 0; i < 5; i++) {
printf("Nota: %d\n", notas[i]);
}
}
Oi, Guilherme! Como vai?
Agradeço por compartilhar seu código com a comunidade Alura.
O seu laço for
percorre corretamente o array notas
e imprime cada valor. Achei interessante você já iniciar o array com valores fixos, facilita para testar.
Você pode usar sizeof
para percorrer arrays sem precisar contar manualmente os elementos.
#include
int main() {
int notas[] = {1, 4, 7, 5, 10};
int tamanho = sizeof(notas) / sizeof(notas[0]);
for(int i = 0; i < tamanho; i++) {
printf("Nota: %d\n", notas[i]);
}
return 0;
}
Esse codigo calcula o tamanho do array automaticamente, evitando erros ao alterar a quantidade de elementos.