Fill vector in C

Asked

Viewed 1,889 times

0

I have an exercise that asks the teacher to type 5 notes, and display the typed notes, I wrote the code, but it’s not running the right way, someone can help?

Follows code

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

int main()
{
setlocale(LC_ALL, "Portuguese");
int i = 0;
float nota[5];
float total = 0, media = 0;

for(i = 0; i < 5; i++)
{
printf("Digite a nota do usuário!\\n");
scanf("%f", &[i]);

}
printf("As notas digitadas foram %.2f\\n", nota[i]);

return 0;
}
  • 2

    scanf("%f", &[i]) What was supposed to happen on this line? Because not only is it not "running in the right way," but it is making an error during the build exactly on this line. It doesn’t even "run".

  • 2

    That line scanf("%f", &[i]); is correct?

4 answers

1

Missing a loop to print. Note that after the read loop the variable i will contain the value 5, which is outside the vector boundary.

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

int main() {
    setlocale(LC_ALL, "Portuguese");
    int i = 0;
    float nota[5];
    float total = 0, media = 0;
    for(i = 0; i < 5; i++) {
        printf("Digite a nota do usuário!\\n");
        scanf("%f", &nota[i]);
    }
    for(i = 0; i < 5; i++)
        printf("As notas digitadas foram %.2f\\n", nota[i]);
    return 0;
}

1

On this line where you take the keypad value typed by the user is missing the name of your note array

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

Correct:

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

and on the line you print the notes

printf("As notas digitadas foram %.2f\\n", nota[i]);

is speaking a For for you to go through the array indices again to print the same.

follows the code: code

0

I made only two changes to your code: I changed the line that was written scanf("%f", &[i]); for scanf("%f", &nota[i]); and implemented a loop for around the line printf("As notas digitadas foram %.2f\n", nota[i]);.

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

int main()
{
  setlocale(LC_ALL, "Portuguese");
  int i = 0;
  float nota[5];
  float total = 0, media = 0;

  for(i = 0; i < 5; i++)
  {
      printf("Digite a nota do usuário!\n");
      //Primeira alteração de &[i] para &nota[i]
      scanf("%f", &nota[i]);

  }

  //Segunda alteração um laço for para imprimir as notas coletadas
  for(i = 0; i < 5; i++)
  {
      printf("As notas digitadas foram %.2f\n", nota[i]);
  }
  return 0;
}

Code running on Repl.it

  • I checked in Code::Blocks and it worked! Thanks Everyone!!!

0

Friend the mistake made was something very simple, your "scanf("%f", &[i]);" is wrong the declaration of the variable, the correct would be "scanf("%f", &note[i]);" because of this you are signaling the vector correctly. I noticed tbm an error when showing the notes that it is, you did not have it rotate the vector correctly to show each written note, in case it would replace your "printf("The typed notes were %.2f n", note[i]);" by a for and the printf inside.

Thus in the case:

is (i = 0; i < 5; i++) { printf("The typed notes were %.2f n", note[i]); }

Browser other questions tagged

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