struct function parameters

Asked

Viewed 142 times

0

I am making a code that calculates the sum and subtraction of complex numbers, however, I cannot find the error to compile.

#include<stdio.h>
#include<math.h>
typedef struct NI {
    float real;
    float imag;
}complexo;
float soma(float z.real,float z.imag,float w.real,float w.imag){
complexo z,w,s;
    s.real=z.real+w.real;
    s.imag=z.imag+w.imag; 
    printf("%f + %f",s.real,s.imag);
    printf("i\n");
   return s.real,s.imag;
}
float produto(float z.real,float z.imag,float w.real,float w.imag){
complexo z,w,m;
    m.real = z.real*w.real - z.imag*w.imag;
    m.imag = z.real*w.imag + z.imag*w.real;
    printf("%f + %f",m.real,m.imag);
    printf("i\n");
return m.real,m.imag;
}
int main(){
complexo z,w,s,m;
char op;

    scanf("%f %f %c %f %f",&z.real, &z.imag, &op, &w.real, &w.imag);

    if(op=='+'){
        soma(z.real,z.imag,w.real,w.imag);
    }
    if(op=='*'){
        produto(z.real,z.imag,w.real,w.imag);
    }


return 0;
    }
  • Pass the structure as parameter. See: https://ideone.com/VvWTlG

1 answer

0

I will separate the explanation into two parts, errors in logic(1) and errors in the syntax of the language C(2):

(1) Much of the logic of your program is right, in other words, you are doing the multiplication and summation of complex numbers according to the definitions of these operations by mathematics. But you should remember that the sum of two complex numbers results in a complex number and not a real number ("float"), an error you made in both functions;

(2)In C the definition of function has this form:

    tipo_do_retorno     nome_da_funcao(declaracao de parametros)
     {
          /*comandos a serem preenchidos*/
     }

Using its code as an example and the definition of function in C, the statement of the function that sums two complex numbers could be made as follows:

complexo soma(float parteRealZ, float parteImagZ, float parteRealW, float parteImagW){
     /*comando a serem preenchidos*/

}

Note that in the declaration of the function parameters you should only enter the type and the name of the parameter e.g " float parteRealZ ", the access of the parameters using for example the '.' (dot) does not happen in the function declaration;

If you look closely you will notice that you are not using efficiently the type you created (complex) because in your sum function you are receiving 4 values, but this same function could be summarized to:

complexo soma(complexo z, complexo w){
    /*comando que serão preenchidos*/

}

One can read the above function as "the sum function takes two arguments, named x and y of the complex type, and returns a complex number corresponds to the sum of these two numbers", this same reasoning can be extended to function multiplies. In other words, you can create a function using the type you created ;).

Last thing, in C language, you can only return a single value in a function and that value should be the type you specified in your function statement, for example, take this simplest function:

int soma(int x, int y){
    int aux  = x + y;
    return aux;
}

It returns only a single element of type int.

In addition to the necessary changes I made an additional change to your code, created a function that displays a complex number.

That way your code could look like this:

#include<stdio.h>
/*#include<math.h>          nao foi usada*/ 

typedef struct NI {
    float real;
    float imag;
}complexo;


complexo soma(complexo z, complexo w){

    complexo aux_soma;

    aux_soma.real = z.real + w.real;
    aux_soma.imag = z.imag+ w.imag; 

    return aux_soma;
}

complexo produto(complexo z, complexo w){

    complexo aux_mult;

    aux_mult.real = z.real * w.real - z.imag * w.imag;
    aux_mult.imag = z.real * w.imag + z.imag * w.real;

    return aux_mult;
}

void printComplexo(complexo x){
    printf("%f %fi\n", x.real, x.imag);
}

int main(){
    complexo z,w,s,m;
    char op;

    scanf("%f %f %c %f %f",&z.real, &z.imag, &op, &w.real, &w.imag);

    if(op=='+'){
        s = soma(z, w);
        printComplexo(s);
    }
    if(op=='*'){
       m = produto(z,w);
       printComplexo(m);
    }

    return 0;
}

Browser other questions tagged

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