-2

First of all I have to say that i have no experience with C My problem is I needed to write a make file to seperate 2 functions wrote like this before ;

#include <math.h>
#include <iostream>
using namespace std;

int main()
{
    float A,B,x,y;
    int   z;
    x=-0.62;
    y=0.82;
    z=25;
    A = pow(y, cbrt(double x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    B = y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    cout << "your answer for A = " << A << endl;
    cout << "your answer for B = " << B << endl;
}

In this point all is good and code is working , but when i try to seperate the functions like this its function A;

float A(float *x, float *y,int *z){
    A = pow(y, cbrt(double x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    return A;
}

And its B;

#include <math.h>
#include <iostream>
using namespace std;

int B(float *x,*y,int *z){
    B = y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    return B;
}

Here is my main function;

#include <math.h>
#include <iostream>
using namespace std;

int main()
{
    float A,B,x,y;
    int   z;
    x=-0.62;
    y=0.82;
    z=25;
    float A(x,y,z);
    float B(x,y,z); 
    cout << "your answer for A = " << A << endl;
    cout << "your answer for B = " << B << endl;

}

I get various amount of errors such as 'no known conversion' or 'pow not defined' how can i solve it ? I am pretty sure its about my variables and how i call them but.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
Ata Reenes
  • 188
  • 1
  • 10
  • Why do you want to pass the parameters by pointer? – Lukas-T Jan 06 '21 at 14:10
  • You shouldn't give your functions and variables the same names. – Thomas Sablik Jan 06 '21 at 14:11
  • 1
    Change `int B(float *x,*y,int *z)` to `int B(float *x, float *y, int *z)`. – D-RAJ Jan 06 '21 at 14:11
  • Also note that the function is not called from your `main()` function. – MikeCAT Jan 06 '21 at 14:12
  • i have no idea about C it was just curiousity can you help me with how should i approach this ? – Ata Reenes Jan 06 '21 at 14:12
  • What exactly do you expect "`cbrt(double x)`" to do? This does not appear to be valid C++. And I'm pretty sure that "`float A(x,y,z);`" doesn't do what you think it does. What do you think this should do? – Sam Varshavchik Jan 06 '21 at 14:12
  • @DhirajWishal It will improbe the code a little, but keeps it wrong. Pointers should be dereferenced in the formula in the function body or the arguments should be changed to `float`. – MikeCAT Jan 06 '21 at 14:12
  • 1
    You keep talking about C, when in fact you are using C++ ;) Seems like you could use a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to cover the basics of the language. – Lukas-T Jan 06 '21 at 14:15
  • 1
    In function `A`, you do `A = pow(y, cbrt(double x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);`. What is `A` that you are assigning to? Functions can't access variables from other functions. – NathanOliver Jan 06 '21 at 14:16
  • 2
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jan 06 '21 at 14:16
  • 1
    What i wanted to do is the same thing with first main function i showed.Just want to seperate function a and b with different files and compile them together – Ata Reenes Jan 06 '21 at 14:20
  • @AtaReenes -- `float A(x,y,z);` -- What programming languages have you used in the past? The reason why I ask is that even though you're stating that you know nothing about `C++`, you're making basic mistakes in setting up how to make function calls. Many languages have similar syntax to C++ in terms of variable assignment and calling functions -- – PaulMcKenzie Jan 06 '21 at 14:21

2 Answers2

2

Don't use same name for variables and functions.
Also you don't need to use pointers to pass arguments in this case.

    #include <math.h>
    #include <iostream>
    using namespace std;
    
    float A_func(float x, float y,int z){
        float A = pow(y, cbrt((double) x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
        return A;
    }
    
    int B_func(float x,float y,int z){
        float B = y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
        return B;
    }
    
    
    int main()
    {
        float A,B,x,y;
        int   z;
        x=-0.62;
        y=0.82;
        z=25;
        A = A_func(x,y,z);
        B = B_func(x,y,z); 
        cout << "your answer for A = " << A << endl;
        cout << "your answer for B = " << B << endl;
    
    }
w0lf
  • 353
  • 3
  • 10
  • can you also tell me how can i make 3 files such as a.cpp b.cpp and main.cpp and compile them ? – Ata Reenes Jan 06 '21 at 14:28
  • 1
    @AtaReenes create a.cpp and b.cpp files with relavent codes. then #include "a.cpp" and #include "b.cpp" in main.cpp. Then you can compile main.cpp. if you are use g++ then use `g++ main.cpp -o main.out` to compile. (I'm assuming a.cpp, b.cpp and main.cpp are in same folder) – w0lf Jan 06 '21 at 14:40
1

I think this is what your looking for. There were multiple errors. Ill comment them out for you.

#include <math.h>
#include <iostream>
using namespace std;

// You dont need to pass variable pointers to a function like this.
float A(float x, float y, int z) {
    // When declaring a variable, make sure that you specify the return type.
    float A = pow(y, cbrt(x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    return A;
}

// Plus, you dont need to include the header files (maths.h and iostream> before every function.
// Just include them at the beginning of the source file (or in the header file) and that's gonna be enough. 
// If your having the functions defined in different source files, you might need to include them once every file.

// You dont need to pass variable pointers to a function like this.
int B(float x, float y, int z) {
    // When declaring a variable, make sure that you specify the return type.
    float B =  y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    return B;
}

int main()
{
    float x, y;
    int   z;
    x = -0.62;
    y = 0.82;
    z = 25;

    // Over here, dont name the variable with the same name as the function as the compiler will have a hard time 
    // undestanding whats going on.
    // Function calls are done like this: return_type variable = function_name(arguments);

    float a = A(x, y, z);
    float b = B(x, y, z);
    cout << "your answer for A = " << a << endl;
    cout << "your answer for B = " << b << endl;
}

Note: Pointers are basically memory addresses. You can pass pointers to a function where the value of the argument will be altered by the function. And you have to make sure you dereference it when you're working with the actual values (ie: *y) stored in the memory address. To get the address from a variable, you have to use the address-of (&) operator (ie: &y).

D-RAJ
  • 3,263
  • 2
  • 6
  • 24