Counter with float number

Asked

Viewed 59 times

0

I’m new to C programming. My problem is that I’m not able to add float numbers with an i counter, which would be integer, which is the best solution?

#include <stdio.h>
#include<stdlib.h>

int main()
{
  float nota[4];
  int i;

for(i=0; i<4; i++){
  printf("Digite a nota posição %d", i);
  scanf("%f", nota[i]);
}

for(i = 0; i < 4; i++){
  printf("A nota posição %d  é = %.2f\n",i, nota[i]);
}

  return 0;
}
  • Why you want to put float inside a counter?

  • What does "I’m not getting it"? Doesn’t the program compile? Doesn’t it work? Shows different values than expected?

1 answer

0


Your problem is in the scanf, some IDE even shows this underlined line, with a Warning, because since you are sending a float variable to it, you need to send the same reference. For this, you use the &:

scanf("%f", &nota[i]);

Just changing this line, your code will work and the entered values will be inserted into the array correctly.


See online: https://repl.it/repls/ShockingCrowdedRoutine

  • Really my problem was in &, thank you so much for your help

Browser other questions tagged

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