Program in C does not add well the elements of a vector

0

I am doing a basic program in C, to calculate notes. It consists of an array in which some notes read by keyboard are stored. Once they are introduced, they are added one by one and stored in a variable.

The problem is that it does not add the elements of the vector, it only saves the last one in the variable. I leave the code:

#include <stdio.h>

int main() {

int i = 0, j, sino, porcentaje, cont = 0;
float notas[10], total = 0, notaActual;

do {
  printf("Escribe la nota: ");
  scanf("%f", &notaActual);

  printf("Escribe el porcentaje sobre 100 de esa nota sobre la nota total: ");
  scanf("%d", &porcentaje);

  notas[i] = notaActual*(porcentaje*0.01);

  printf("Continuar con las notas? (1/0): ");
  scanf("%d", &sino);

  if(sino == 1)
    i++;

} while (sino == 1 && i < 10); //No es necesario llenar el vector

for(j = 0; j <= i; j++) {
  total += notas[j];
  printf("%f ", notas[j]);
}

printf("\nTU NOTA SOBRE 10 ES %f", &total);

return 0;
}

If we enter a 5 with 50 percentage in two iterations, the output is:

2.50 2.50

TU NOTA SOBRE 10 ES 2.50
    
asked by DDN 15.11.2017 в 17:32
source

1 answer

0

What fails your code is that the printf method does not need the & to point to the variable. That is, your code looks like this:

#include <stdio.h>

int main() {

int i = 0, j, sino, porcentaje, cont = 0;
float notas[10], total = 0.0, notaActual;

do {
  printf("Escribe la nota: ");
  scanf("%f", &notaActual);

  printf("Escribe el porcentaje sobre 100 de esa nota sobre la nota total: ");
  scanf("%d", &porcentaje);

  notas[i] = notaActual*(porcentaje*0.01);

  printf("Continuar con las notas? (1/0): ");
  scanf("%d", &sino);

  if(sino == 1)
    i++;

} while (sino == 1 && i < 10); //No es necesario llenar el vector

for(j = 0; j <= i; j++) {
  total += notas[j];
  printf("%f ", notas[j]);
}

printf("\nTU NOTA SOBRE 10 ES %f", total);

return 0;
}
    
answered by 15.11.2017 / 19:47
source