Manipulation of values in input files c . txt code

Asked

Viewed 698 times

0

This is what I need to do

Create an algorithm where you calculate through various temperature inputs the percentage outside the variation stipulated as maximum and minimum having as out of +-3 values from stipulated temperature values, out of that is outside the target values, and should present the corresponding percentage to what is outside.

how far I’ve come :

#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 don’t know exactly how to do this part

--AVERAGE THE VALUES AND SHOW THE PERCENTAGE THAT IS OUT ACCORDING TO THE ENTRANCE VALUE'

--DISPLAY RESULT ON SCREEN AND RETURN IN FILE . TXT

1 answer

1

Here is an example (tested), with functions exemplifying how to calculate the average and the 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 in the set:

inserir a descrição da imagem aqui

Then we calculate the standard deviation of all such samples in relation to the average:

inserir a descrição da imagem aqui

Thus, we squared the standard deviation of each sample in relation to the average:

inserir a descrição da imagem aqui

With this, we are able to calculate the Variance:

inserir a descrição da imagem aqui

Input file:

2.0
3.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0

Exit:

$ ./variancia 
Media    : 5.700000
Variancia: 6.810000
  • Line (52) p = realloc( p, i * sizeof(double)); error for me ! Invalid conversion from VOID to DOUBLE

  • @Agenaro: The following edition: p = (double*) realloc( p, i * sizeof(double));

Browser other questions tagged

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