Manipulation of values in input files c .txt

0

I need to do the following

Create an algorithm where you calculate by means of several temperature inputs the percentage outside the variance stipulated as maximum and minimum having as out of values + -3 from stipulated temperature values, otherwise the desired values, and should present the percentage referring to the to what is left out.

Where I came from:

#include<stdio.h>
#include<string.h>

int main(void){
    float entrance; 
    char Str[50];
    FILE *arq;
    char Linha[100];
    char *result;
    int i;
    // Abre um arquivo TEXTO para LEITURA
    arq = fopen("arq.txt", "rt");
    if (arq == NULL){
        printf("Problemas na abertura do arquivo\n");
        return;
    }
    i = 1;
    while (!feof(arq)){
        result = fgets(Linha, 100, arq); 
        if (result)
            i++;
    }
    fclose(arq);
    puts("\n\t                                           ");
    puts("\t           - - imported file - -             ");
    puts(" press Enter...                                ");
    getchar();  
    puts("\t* SELECIONE UMA OPCAO DESEJADA:             *");
    puts("\t*                                           *");
    puts("\t*  1          3 graus celcius               *");
    puts("\t*  2         -3 graus celcius               *");
    puts("\t*  3         -5 graus celcius               *");
    puts("\t*  4         -8 graus celcius               *");
    puts("\t*  5        -12 graus celcius               *");
    puts("\t*  6        -15 graus celcius               *");
    puts("\t*  7        -18 graus celcius               *");
    scanf("%f",&entrance);

    /*
        --FAZER A MEDIA DOS VALORES E MOSTRAR A PORCENTAGEM QUE
        FICA FORA DE ACORDO COM O VALOR DE 'ENTRANCE'
        --APRESENTAR RESULTADO EM TELA E RETORNAR EM ARQUIVO .TXT
    */
    getchar();
    return 0;
}

I do not know exactly how to do this part

- MAKE THE VALUE OF THE VALUES AND SHOW THE PERCENTAGE THAT STAY AWAY FROM THE 'ENTRANCE' VALUE

- PRESENT SCREEN RESULT AND RETURN TO .TXT FILE

    
asked by anonymous 27.08.2017 / 05:49

1 answer

1

Here is an example (tested), with functions exemplifying how to calculate average and variance of a finite set of samples obtained from a text file:

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


#define NOME_ARQUIVO_AMOSTRAS "arq.txt"


double media( double s[], int n )
{
    double sum = 0.0;
    int i = 0;

    for( i = 0; i < n; i++ )
        sum += s[i];

    return sum / n;
}


double variancia( double s[], int n )
{
    double sum = 0.0;
    double dev = 0.0;
    double med = media( s, n );
    int i = 0;

    for( i = 0; i < n; i++ )
    {
        dev = s[i] - med;   /* Desvio Padrao */
        sum += (dev * dev); /* Quadrado do Desvio */
    }

    return sum / n;
}


int carregar_serie( const char * arq, double * s[], int * n )
{
    FILE * fp = NULL;
    char amostra[ 100 ] = {0};
    double * p = NULL;
    int i = 0;

    fp = fopen( arq, "r" );

    if(!fp)
        return -1;

    while( fgets( amostra, sizeof(amostra), fp ) )
    {
        i++;
        p = (double*) realloc( p, i * sizeof(double));
        sscanf( amostra, "%lf", p + i - 1 );
    }

    fclose(fp);

    *s = p;
    *n = i;

    return 0;
}


int main( void )
{
    double * serie = NULL;
    int n = 0;

    if( carregar_serie( NOME_ARQUIVO_AMOSTRAS, &serie, &n ) < 0 )
    {
        printf("Erro ao abrir arquivo de amostras para leitura: '%s'\n", NOME_ARQUIVO_AMOSTRAS );
        return 1;
    }

    printf( "Media    : %f\n", media( serie, n ) );
    printf( "Variancia: %f\n", variancia( serie, n ) );

    free(serie);

    return 0;
}

Consider the following set of samples:

{ 2.0, 3.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }

First, we calculate the average of the samples of the set:

Next,wecalculatedthe standard deviation of all these samples from the mean:

Thus,wesquaredthestandarddeviationofeachsampleinrelationtothemean:

Withthis,weareabletocalculateVariance:

Input file:

2.0
3.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0

Output:

$ ./variancia 
Media    : 5.700000
Variancia: 6.810000
    
29.08.2017 / 21:04