0

I am coding this using C. I am trying to get the pow function to work on this problem. using the base as the variable that the user would input. This program asks the user to calculate the area, and costs of a simple open can. here is the code:

//Pre-processor Directives
#include <stdio.h>
#include <math.h>
#define PI 3.14159

//Start of function
int main(void)
{
    //Declared variables
    float base_area, height_area, total_area_per_container;
    float radius, height, cost_per_container, total_cost, cost_per_sq_cm;
    int containers;

    //user input
    //radius input
    printf("Enter radius of base in cm: ");
    scanf("%f", &radius);
    //height input
    printf ("Enter height of container in cm: ");
    scanf("%f", &height);
    //material cost
    printf("Enter material cost per square cm: ");
    scanf(" $%f", &cost_per_sq_cm);
    //amount of containers
    printf("Enter the number of containers to be produced: ");
    scanf("%d", &containers);

    //calcualtions of each container
    base_area = PI * pow(radius,2);
    height_area = 2 * PI * radius * height;
    total_area_per_container = base_area + height_area;

    //calculation of the cost of the material
    cost_per_container = total_area_per_container * cost_per_sq_cm;
    total_cost = containers * cost_per_container;

    //Print results
    printf("Surface area of container: %.2f cm\n", total_area_per_container);
    printf("Cost per container: $%.2f\n", cost_per_container);
    printf("Total production costs: $%.2f\n", total_cost);

    //exit program
    return (0);
}

everything works fine if i take out the pow(radius,2) under the comment calculations of each container and put in "radius * radius" I just wanted to test to see how the pow function works. I feel like I am doing something wrong. Also I am using NetBeans IDE 8.0.2 to write the code.

Update1: using the gcc compiler that my instructor has. compiling my code on his computer gives me this faling response:

1st part is a bunch of jargin saying i am copying my code to his computer what follows is below- the directories my stuff is stored on was removed

In function `main':
undefined reference to `pow'
collect2: error: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/hw5] Error 1
gmake[2]: Leaving directory
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory
gmake: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)
  • 2
    `` defines `M_PI` constant which should be used in place of `PI`. – Sergey Kalinichenko Sep 12 '15 at 01:36
  • 2
    `pow(radius,2)` is in general slower and less accurate than `radius * radius` – Chris Beck Sep 12 '15 at 01:38
  • @ChrisBeck `C` doesn't support overloaded functions. – Cloud Sep 12 '15 at 01:40
  • oops, I overlooked that tag – Chris Beck Sep 12 '15 at 01:40
  • @user3602535 I see nothing wrong with your use of the `pow` function. I suggest that you print the `radius` and the results of the `pow` function. – user3386109 Sep 12 '15 at 01:43
  • Thanks dasblinkenlight I had no idea! – AndrewDonaldStockton Sep 12 '15 at 02:01
  • can you mention the test case on which your program fails? – shivam mitra Sep 12 '15 at 02:03
  • My apologies i dont think i clarified the problem. The compiler fails. i will update the initial question. – AndrewDonaldStockton Sep 12 '15 at 02:07
  • @dasblinkenlight: `M_PI` is non-standard. – Keith Thompson Sep 12 '15 at 20:47
  • @Dogbert, supplying an alternative to a standard library function is no overloading at all. And the errror hasn't to do with that. Compiler is complainting about not having used `-lm` on linking, so the actual reason of the error is having no `pow()` function at all.. – Luis Colorado Sep 13 '15 at 09:21
  • @LuisColorado I suggest you re-read my answer and comments. C doesn't support overloading, but that's not my answer. I note that the error is due to the linker not resolving `libm`. The original question, prior to edits, didn't mention the linker error and that code didn't compile, just that different results were achieved with different numerical calculations. You'll need to also read the revision history to see the whole picture. – Cloud Sep 13 '15 at 14:22

2 Answers2

1

Compilation is failing since you're not linking against the math library. Try compiling via:

gcc infile.c -lm

Second, there's a glitch in your code. The scanf() calls are failing due to not "consuming/gobbling-up" the trailing newline character. Don't use scanf(): use fgets() and the atoX() and sscanf() functions if you must do string parsing like this. The strtok() call in my getBuf() function is just their in case you use this example for other types of string parsing in the future. The fgets() function doesn't use a shared stream buffer like scanf() does.

I've updated your code listing, and am able to get the same results using both of your proposed power calculation methods.

Code Listing


/*******************************************************************************
 * Pre-processor Directives
 ******************************************************************************/
#include <stdio.h>  // printf()
#include <stdlib.h> // atof() will compile but return zero if this is missing
#include <math.h>   // pow()
#include <stdbool.h>    // bool

#define PI      M_PI
#define BUF_LEN     (256)


/*******************************************************************************
 * Function prototypes
 ******************************************************************************/
bool getBuf(char* buf);


/*******************************************************************************
 * Function definitions
 ******************************************************************************/
int main(void)
{
    //Declared variables
    float base_area, height_area, total_area_per_container;
    float radius, height, cost_per_container, total_cost, cost_per_sq_cm;
    int containers;
    char buf[BUF_LEN] = { 0 };

    // User input
    //radius input
    printf("Enter radius of base in cm: ");
    if ( !getBuf(buf) ) { return (-1); }
    radius = atof(buf);

    //height input
    printf ("Enter height of container in cm: ");
    if ( !getBuf(buf) ) { return (-1); }
    height = atof(buf);

    //material cost
    printf("Enter material cost per square cm: ");
    if ( !getBuf(buf) ) { return (-1); }
    cost_per_sq_cm = atof(buf);

    //amount of containers
    printf("Enter the number of containers to be produced: ");
    if ( !getBuf(buf) ) { return (-1); }
    containers = atoi(buf);

    //calcualtions of each container
    base_area = PI * pow(radius, 2.0);
    //base_area = PI * radius * radius;
    height_area = 2 * PI * radius * height;
    total_area_per_container = base_area + height_area;

    //calculation of the cost of the material
    cost_per_container = total_area_per_container * cost_per_sq_cm;
    total_cost = containers * cost_per_container;

    //Print results
    printf("Surface area of container: %.2f cm\n", total_area_per_container);
    printf("Cost per container: $%.2f\n", cost_per_container);
    printf("Total production costs: $%.2f\n", total_cost);

    //exit program
    return (0);
}

bool getBuf(char* buf)
{
    if (!buf)
    {
        printf("Bad input.\n");
        return false;
    }
    fgets(buf, BUF_LEN, stdin); // Get a string of data
    strtok(buf, "\n");      // Clear out trailing newline
    return true;
}

Sample Output


gcc test.c -lm && ./a.out 
Enter radius of base in cm: 1
Enter height of container in cm: 2
Enter material cost per square cm: 3
Enter the number of containers to be produced: 4
Surface area of container: 15.71 cm
Cost per container: $47.12
Total production costs: $188.50

Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
0

Thank you everyone for your time in answering my question. The real answer my problem despite the inefficient use of code was this:

I am taking a new intro to C programming course and to submit the homework I copy the information over to the teachers computer where it undergoes the testing and compiling of the code that I have written. I do not know why but the code when it undergoes this process, on the professors side, does not appear to have the -lm process attached. that was the issue, the math library was not linked.

:/ my apologies for the confusion.

Thank you @Dogbert for the upgraded way to code I will try and read my book to figure out what fgets(), atoX(), sscanf(),getBuff(), and strtok() mean.