Reputation: 11
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)
Upvotes: 0
Views: 187
Reputation: 11
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.
Upvotes: 0
Reputation: 19333
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
Upvotes: 1