Home > Mobile >  How do I make use of a custom library in C
How do I make use of a custom library in C

Time:11-07

I've written some code in regard to a uni assignment, but I keep stumbling across the problem where my custom function doesn't give any other output then zero.

Basically, I'm asking how to retrieve the result of the function to use in my code.

Here I will paste my code.

    #include <stdio.h>

    //math.h is included via the header file because the compiler liked it better.

    int V;

    int H;

    int R;

    int result;

    #include "A1_header.h"

    int main()
    {

    //small introduction
    printf("Welcome to the volume test game\n");
    printf("We will start with spheres. \n");
    printf("Please input your sphere radius here:");
    scanf("%d", &R);
    CalcSphVolume(&V, &R);
    printf("please input your result:");
    scanf("%d", &result);
    if(result == V){
        printf("Congratulations, your answer is correct!\n");
    }
    else{
        printf("Wrong answer, the correct result was %d \n", V);
    }
    return 0;
}

Below is my .h file which I use to define functions.

    #ifndef Point

    #define Point

    #include <math.h>

    //these are the functions for Sphere calculations

    void CalcSphVolume(int V, int R) {
    V = 1.33*3.14(R*R*R);
    }

    void CalcSphRadius(int V, int R) {
    R = cbrt(V/4.1762);
    return;
    }

    //these are the functions for the Cone calculations

    void CalcConVolume(int V, int R, int H) {
    V = 0.33*(3.14*(R*R))H;
    return;
    }

    void CalcConHeight(int V, int R, int H) {
    H = V/(0.33*(3.14*(R*R)));
    } 

    void CalcConRadius(int V, int R, int H) {
    R = sqrt(V/(0.33*3.14H));
    }

    //these are the functions for the Cilinder calculations

    void CalcCilVolume(int V, int R, int H) {
    V = 3.14*H*(R*R);
    }

    void CalcCilHeight(int V, int R, int H) {
    H = V/(3.14(R*R));
    }

    void CalcCilRadius(int V, int R, int H) {
    R = sqrt(V/(3.14*H));
    }

    #endif

CodePudding user response:

I am not sure how you are running the code, because under ordinary circumstances this code will not compile.

There are a couple of things that are wrong in this code -

    void CalcSphVolume(int V, int R) {
        V = 1.333.14(RRR);
    }

Firstly, I think there might be an error in your formatting, because RRR is not the correct way to compute, what you want is (R*R*R), similarly, 1.333.14 is not an accepted datatype. From context, since it is the volume of a sphere, this should be

    void CalcSphVolume(int V, int R) {
       V = 1.333 * 3.14 * (R*R*R);
    }

Now that this is out of the way, there are some issues with this function (and similar to the other functions in your code.)

  1. You are mixing types here - 1.333 is double, but V is int, so your actual answer will be implicitly converted to an integer, and you will lose precision. So V should be of the type double. So you get a more accurate answer.
    void CalcSphVolume(double V, int R) {
       V = 1.333 * 3.14 * (R*R*R);
    }
  1. Another thing to note here is that your functions parameters are by value and do not accept pointers. This means that any result that you get in this void function, will be lost unless you explicitly return it. Local variables only exist in the scope of the function. Since you are passing a pointer to the function and trying to populate the value outside the function, you should modify the function and function signature to
    void CalcSphVolume(double* V, int* R) {
        int r = *R // for clarity
       *V = 1.333 * 3.14 * (r*r*r);
    }

We have to dereference to get the "actual" value that is held by the pointer. You cannot apply arithmetic logic in this manner to raw pointers.

An alternative way to accomplish the same is

    double CalcSphVolume(int R) {
       double V = 1.333 * 3.14 * (R*R*R);
       return V; // or simply return 1.333 * 3.14 * (R*R*R)
    }

Here you are passing by value, but returning the value computer back to the caller. So you could use this function like so -

double Volume = CalcSphVolume(R);

This is much more clearer in this case, instead of having to pass pointers all over the place.

For your use case, using pointers is not necessary - consider using pointers when you have to mutate or pass large objects (Imagine a really huge array, so you don't want to create a copy it each time you use it in a function) which cannot be declared on the stack.

CodePudding user response:

The issue is your function parameters are taking in an integer rather than a pointer to the memory address.

What you want to do instead is:

 void CalcSphRadius(int *V, int *R) 
 {
     *R = cbrt((*V)/4.1762);
     return;
 }

Now, the function is taking in a pointer to V and R and will read/write to their memory address.

As shown above, don't forget to dereference your pointers by using an asterisk '*' so that way you are writing to the value stored in those addresses, rather than just doing pointer arithmetic.

You can also retrieve values by using return types for functions.

 int CalcSphRadius(int V) 
 {
     return cbrt(V/4.1762);
 }

And then use them to assign variables like so:

R = CalcSphRadius(V);

CodePudding user response:

  1. Your .h file name should be A1_header.h Also, the code can be like this:
#ifndef Point

#define Point

#include <math.h>
#include <stdio.h>

//these are the functions for Sphere calculations

void CalcSphVolume(int V, int R);

void CalcSphRadius(int V, int R);

//these are the functions for the Cone calculations

void CalcConVolume(int V, int R, int H);

void CalcConHeight(int V, int R, int H);

void CalcConRadius(int V, int R, int H);

//these are the functions for the Cilinder calculations

void CalcCilVolume(int V, int R, int H);

void CalcCilHeight(int V, int R, int H);

void CalcCilRadius(int V, int R, int H);

#endif
  1. #include "A1_header.h" in the wrong place. It should be like
//directives
#include <stdio.h>
#include "A1_header.h"

int main(void)
{
    variable declaration;
    
    statements
}

Please try to learn the structure of the C program.

You need to declare the variables inside the main function.

  1. When you call the function CalcSphVolume(V, R); why the address of the variable? It should be:
printf("Please input your sphere radius here:");
scanf("%d", &R);
CalcSphVolume(V, R);
  1. Arrange all the functions properly eg: CalcCilHeight function should be properly formatted with *

    void CalcCilHeight(int V, int R, int H)
    {
        H = V/(3.14 * (R * R));
    }
    
  • Related