Use of EOF in vectors

-1

Good night, I would like to know if in this program I am using EOF (end of file) right, that is, as long as I do not type 0, the vector is being allocated and recorded with a value ?? Here is the code below:

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

int main (void)
{
    int i,j, counter = 1,mediaaritmetica, novasequencia;
    int* sequencia = NULL;

    while (scanf(%d), &sequencia[i] != EOF)
    {
        sequencia = (int*) realloc(sequencia, counter * sizeof(int));
        scanf("%d", &sequencia[i]);
        counter++
    }   

    for (j = 0; j < counter ; j++)
    {
        mediaartimetica += sequencia[j];
    }

    mediaaritmetica = mediaartimetica/counter - 1;

    for ( k = 2, k < counter ; k++)
    {
        novasequencia = pow(sequencia[k], 2)/mediaaritmetica * (sequencia[k - 2] + sequencia[k -1] + sequencia[k])
        printf(".4%d\n", novasequencia);
    }

    return 0;
}
    
asked by anonymous 16.05.2015 / 05:49

2 answers

1

Some notes:

  • % with% error of quotes and parentheses
  • while (scanf(%d), &sequencia[i] != EOF) you are for the car ahead of the oxen
  • use sequencia = NULL; /* ... */ &sequencia[i] without assigning value
  • i needs parenthesis
  • mediaaritmetica = mediaartimetica/counter - 1; needs pow() . But you can do the same by multiplying the value: <math.h>
  • different identifiers !!! sequencia[k] * sequencia[k] and mediaaritmetica
  • 16.05.2015 / 08:54
    0

    The EOF is only used when moving files, just as the name already says End Of File , end of file, not vector.

    Beginning with its while , scanf is a method that for reading information and sequencia[i] does not yet exist, and conditions in while out any other structure separates with && or || (this is in C, C ++, etc). The correct thing is to have a variable to collect the value of scanf and through it check the input.

    int in = 1;
    sequencia = (int*) malloc(0);
    // continua enquanto 'in' diferente de '0'
    while(in != 0){
        scanf("%i", &in);                     // counter deve iniciar em 0 para
                                              // não ter problema no for abaixo.
        sequencia = (int*) realloc(sequencia, (couter + 1)*sizeof(int));
        sequencia[counter] = in;
        counter++;
    }
    

    Another thing, first of a malloc in sequencia .

    Your average has to have parentheses in counter - 1 to remove one before the division.

    mediaaritmetica = mediaartimetica/(counter - 1);
    

    And finally, you should declare k before using it in for .

    int k;
    for ( k = 2, k < counter ; k++){
    

    and all other variables start with 0, because when a variable is created, the compiler inserts value garbage into them.

    int i,j, counter = 1,mediaaritmetica=0, novasequencia=0;
    
        
    03.03.2016 / 23:03