I cannot make a vector point correctly to a function it belongs to

Asked

Viewed 88 times

0

I have tried several times, but the compiler does not understand the references made in the header of the function void CalculaValorparaPagar(float ValorPago[MAX], float Quant_Kwh[MAX],float Preco[MAX], int bandeira[MAX], int tipo[MAX]) and in main CalculaValorparaPagar(&ValorPago[MAX],Quant_Kwh,Preco,bandeira,tipo/i/); and gives the following error:

[Error] cannot Convert 'float' to 'float' for argument '1' to 'void Calculatedfloat(float, float, float, int, int)

How should I proceed?

                #include <stdio.h>
                //#include "headerfiledoconsumo.h"
                #define MAX 2
                int x;  
                float Imposto[MAX], Taxa_ilum [MAX];
                float Taxa_Band_Tar [MAX];
                float Quant_Kwh [MAX];
                int tipo [MAX];
                float ValorPago[MAX];
                float Preco[MAX];
                int bandeira[MAX];

                //void CalculaValorparaPagar();
                //void fazperguntas();
                //void imprimevalorpagar();

                void fazperguntas(/*float Quant_Kwh[MAX],float Preco[MAX],int tipo[MAX],int bandeira[MAX]*/){

                for (int i= 0; i<MAX; i++){

                //printf("Informe seu nome:\n");
                //scanf("%s", &nome[i]);
                printf("Informe seu consumo em KW: \n");
                scanf("%f", &Quant_Kwh[i]);
                getchar();
                printf("Digite o preco do Khw: \n");
                scanf("%f", &Preco[i]);
                getchar();
                printf("Informe o tipo de consumidor que voce eh: 1(residencial), 2(comercial), 3(Industrial)\n");
                scanf("%d", &tipo[i]);
                getchar();
                printf("Informe a bandeira tarifaria: Verde(1), Amarela(2), Vermelha(3)\n");
                scanf("%d", &bandeira[i]);
                getchar();

                }
                }



                void CalculaValorparaPagar(float *ValorPago[MAX], float Quant_Kwh[MAX],float Preco[MAX], int bandeira[MAX], int tipo[MAX]){


                    //int i = 10;

                    for(int i=0; i<MAX;i++){

                    if (bandeira[i] == 1){
                        Taxa_Band_Tar[i] = 0;
                    }

                    if(bandeira[i] == 2 || Quant_Kwh[i] > 100){

                        x = (Quant_Kwh[i]/100);
                        Taxa_Band_Tar[i] = x*3.5;
                    }

                    if(bandeira[i] == 3 || Quant_Kwh [i] >100){

                        x = (Quant_Kwh[i]/100);
                        Taxa_Band_Tar[i] = x*10.5;
                    }


                    if (tipo[i] == 1){

                        Taxa_ilum[i] = 23.45;
                    }

                    if(tipo[i] == 2){

                        Taxa_ilum[i] = 29.23;
                    }

                    if(tipo[i] == 3){

                        Taxa_ilum[i] = 37.06;
                    }



                    Imposto[i] = 0.04*Quant_Kwh[i] + 0.02*Taxa_ilum[i];
                    *ValorPago[i] = Quant_Kwh[i]*Preco[i] + Taxa_ilum[i] + Imposto[i] + Taxa_Band_Tar[i];

                    }
                }


                    void imprimevalorpagar(){

                        for(int i = 0; i<MAX; i++){ 

                 printf("Consumidor %d  Valor a pagar: %f\n", i,ValorPago[i]); 
                }

                }






                int main() {



                fazperguntas();


                CalculaValorparaPagar(&ValorPago[MAX],Quant_Kwh,Preco,bandeira,tipo/*i*/);


                imprimevalorpagar();

                }
  • The first thing you need to decide is whether to put the variables as global or local, doing both at the same time doesn’t work. It’s more complicated to do everything local, but it’s right, global it’s gambit.

  • Thank you very much, I’ll try here!

1 answer

0

I told you how much I hate printf and scanf? Anyway, ignore.

The message that the Clang displays is much more enlightening:

list. c:24:5: Warning: ignoring Return value of Function declared with 'warn_unused_result' attribute [-Wunused-result]

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

^~~~~ ~~~~~~~~~~~~~~~~~~~

list. c:27:5: Warning: ignoring Return value of Function declared with 'warn_unused_result' attribute [-Wunused-result]

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

^~~~~ ~~~~~~~~~~~~~~~

list. c:30:5: Warning: ignoring Return value of Function declared with 'warn_unused_result' attribute [-Wunused-result]

scanf("%d", &tipo[i]);

^~~~~ ~~~~~~~~~~~~~~

list. c:33:5: Warning: ignoring Return value of Function declared with 'warn_unused_result' attribute [-Wunused-result]

scanf("%d", &bandeira[i]);

^~~~~ ~~~~~~~~~~~~~~~~~~

list. c:110:25: Warning: incompatible Pointer types Passing 'float *' to Parameter of type 'float **' [-Wincompatible-Pointer-types]

Calculatedmap(&Value[MAX],Quant_kwh,Price, flag, type/i/);

                    ^~~~~~~~~~~~~~~

list. c:41:35: note: Passing argument to Parameter 'Valorpago' here

void Calculatedvalue(float *Value[MAX], float Quant_kwh[MAX], float Price[MAX], int flag[MAX], int type[MAX]){ ^

5 warnings generated.

The compiler points out that you tried to pass float* as if it were float**

And when executing, the little creature gives a beautiful Segmentation Fault. The problem is to treat as reference something that is already a vector.

But, particularly, I believe that you should implode this code and do something different. I will list what I see as problematic in your code:

  • Global variables. As I imagine you’re a beginner, I’ll be more absolute: don’t use global variables. Run away from them like demons run away from Van Helsing.

  • Magic constants. For example, what would that be 3.5 in this line : Taxa_Band_Tar[i] = x*3.5;? What is the "meaning of life" of this value? And of the other values? Magical constants of this genre should be named and, if possible, placed in a header file.

  • Functions with too many parameters. Why so much parameter in the same function? From what I’ve noticed, these parameters are somehow related, as if they were attributes of some object in the real world - namely, a light count.

  • Little encapsulation - part of the problem pointed out above: the parameters of the functions should be "closer", forming a logical unit - as a struct.

  • scanf(), all right, but printf() is great (although it has room for improvement; see the methods string.Format() of various descendants of C)

  • Yeah, I was kind of radical with printf (). It is that it is misused - the staff using %d to print float. It’s easy to abuse him

Browser other questions tagged

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