jeudi 30 avril 2015

Gcc/G++ doesn't give correct results when compiling more than 3 times

I created a program in C for a class in my college (on Linux with gcc) that do some calculus. The program works fine the first and second time i compiled it and run it (gcc name of file -o name of output file), but the third time always gives different values without having changed any code. My question is...the problem is in gcc/g++ or in my code? On Windows under Dev-C++ it works fine no matter how many times i run it. Apologies about the indentation if it is wrong/weird i had some trouble indenting here on the site and didn't have time to pay much attention to it.

#include <stdio.h>

float bullinton(float *altobj, float *distobj, float transmissor, float receptor, int n){

int i=0,j,l,w, k=0, count_tgt=0, count_tgr=0, max_tgt=0, max_tgr=0, max=0;
float tgt[n], tgr[n], somatorio_dist=0, alt=0, maxtgt=0, maxtgr=0, soma_transmissor, soma_recetor;  //tangentes do transmissor e receptor

    //Linha de vista para o transmissor
    for(i=0;i<n;i++){
        soma_transmissor+=distobj[i];
        tgt[i]=(altobj[i]-transmissor)/(soma_transmissor);
    }

    //Linha de vista para o receptor
    for(i=n-1,k=n;i>=0;i--){
        soma_recetor+=distobj[i+1];
        tgr[i]=(altobj[i]-receptor)/(soma_recetor);
    }

    //Mostra os valores da tangente e do receptor em cada posição
    for(i=0,k=n;i<=n && k!=0;i++,k--){
        printf("A tangente do transmissor na %d posição e: %.2f\nA tangente do receptor na %d e: %.2f\n",i,tgt[i],k,tgr[i]);    
    }

    //valor maximo de tgt
    for(i=1;i<n;i++){
        if(tgt[i]>tgt[max_tgt]){
            max_tgt=i;
        }
    }   

    //valor maximo de tgr
    for(i=1;i<n;i++){
        if(tgr[i]>tgr[max_tgr]){
            max_tgr=i;
        }

    }

    //Mostra o valor maximo de tgt e tgr
    printf("O valor maximo de tgt e: %.2f\nO valor maximo de tgr e: %.2f\n",tgt[max_tgt],tgr[max_tgr]);

    if(transmissor==receptor){
        for(i=0;i<n+1;i++){
            somatorio_dist+=distobj[i];
        }
    }

    float tgtt=1/tgt[max_tgt];
    float tgrr=1/tgr[max_tgr];

    alt=somatorio_dist/(tgtt + tgrr);   

    printf("Tgtt=%.2f\nTgrr=%.2f\n",tgtt,tgrr);
    printf("Somatorio %.2f\nA altura e: %.2f\n", somatorio_dist,alt);
}

int main(){

    int i, numero_objectos=0;
    float altura_objecto[numero_objectos], distancia_objecto[numero_objectos], transmissor, receptor;


    puts("Introduza o numero de objectos: ");
    scanf("%d",&numero_objectos);

    for(i=0;i<numero_objectos;i++){
        printf("Introduza a altura para o %d objecto:\n",i+1);
        scanf("%f",&altura_objecto[i]);
    }

    for(i=0;i<numero_objectos+1;i++){
        printf("Introduza a distancia para o %d objecto:\n",i+1);
        scanf("%f",&distancia_objecto[i]);
    }

    puts("Introduza a altura do transmissor: ");
    scanf("%f",&transmissor);

    puts("Introduza a altura do receptor: ");
    scanf("%f",&receptor);

    bullinton(altura_objecto, distancia_objecto, transmissor, receptor, numero_objectos);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire