Sign in with
Sign up | Sign in
Your question

Need some help with C programming

Last response: in Apps General Discussion
Share
June 22, 2012 8:58:23 PM

Heres the code:

  1. #include <stdio.h>
  2.  
  3. void purpose(void);
  4. double cost_of_each_container(double cost);
  5. double total_cost_of_containers(double total);
  6.  
  7. int main(void)
  8. {
  9. double radius;
  10. double height;
  11. double cps;
  12. int containers;
  13. double cpc;
  14. double total;
  15.  
  16. purpose();
  17.  
  18. printf("\tEnter the radius of the base of the container in cm: ");
  19. scanf("%lf", &radius);
  20. printf("\tEnter the height of the container in cm: ");
  21. scanf("%lf", &height);
  22. printf("\tEnter the cost per square centimeter: ");
  23. scanf("%lf", &cps);
  24. printf("\tEnter the number of containers: ");
  25. scanf("%d", &containers);
  26. printf("\n\t ***** OUTPUT *****\n");
  27. printf("\n\tRadius of the container %14.2lfcm\n\n", radius);
  28. printf("\tHeight of the container %14.2lfcm\n\n", height);
  29. printf("\tCost per square centimeter %11.2lf\n\n", cps);
  30. printf("\tNumber of containers %17.2d\n\n", containers);
  31. printf("\t***** CALCULATED VALUES *****\n\n");
  32. cpc = cost_of_each_container(cost);
  33.  
  34. getch();
  35. return 0;
  36. }
  37.  
  38. void purpose(void)
  39. {
  40. printf("\n This program computes the cost per container and the total cost \n");
  41. printf(" To use this program the user has to enter the radius, height, and cost per square \n");
  42. printf(" from the keyboard and the program calculates the cost per container and total cost \n");
  43. printf(" then prints out the radius, height, cost per square centimeter, number \n");
  44. printf(" of containers, and the total\n\n\n");
  45. }
  46.  
  47. double cost_of_each_container(double cost)
  48. {
  49. double cpc;
  50.  
  51. cpc = containers * cps;
  52.  
  53. return(cpc);
  54. }


I'm trying to call the function cost_of_each_container but its not working and i have no idea what i'm doing wrong please help. I know the spacing in the void purpose part is bad but i will fix that later. And if someone could tell me the math for the function cost_of_each_container because i know thats not right (i am trying to find the cost per container). Thanks!

More about : programming

June 22, 2012 9:29:39 PM

This is an assignment and I have to make three functions called purpose which i called and that one works fine. Then i have to make another called cost_of_each_container and one more called total_cost_of_containers. I can't figure out what i am doing wrong when trying to call these functions. Also these functions cannot have any input or output ops only arithmetic. Thanks!
m
0
l
Related resources
June 22, 2012 11:04:45 PM

Someone help please?
m
0
l
a b L Programming
June 23, 2012 5:50:22 AM

I'm not experienced at all in C, but you appear to be passing calling cost_of_each_container an undeclared variable "cost". Surely your compiler would have picked up on this though.
m
0
l
a b L Programming
June 25, 2012 1:04:56 PM

+1 cost is missing.

So the math is fairly straightforward here you are trying to calculate the volume of a cylinder. So use:

volume = 3.14 * pow(radius,2) * height;

The cost of this volume is simply:

cost = cpc * volume;

Don't really understand the point of your cost_of_each_container function though when it's only taking a "cost" parameter. Really you want a definition like:

  1. double cost_of_each_container(double costPerCm, double height, double radius){
  2. double volume = 3.14 * pow(radius,2) * height;
  3. return volume * costPerCm;
  4. }
m
0
l
June 25, 2012 1:29:56 PM

Jordannn15 said:
Heres the code:

  1. #include <stdio.h>
  2.  
  3. void purpose(void);
  4. double cost_of_each_container(double cost);
  5. double total_cost_of_containers(double total);
  6.  
  7. int main(void)
  8. {
  9. double radius;
  10. double height;
  11. double cps;
  12. int containers;
  13. double cpc;
  14. double total;
  15.  
  16. purpose();
  17.  
  18. printf("\tEnter the radius of the base of the container in cm: ");
  19. scanf("%lf", &radius);
  20. printf("\tEnter the height of the container in cm: ");
  21. scanf("%lf", &height);
  22. printf("\tEnter the cost per square centimeter: ");
  23. scanf("%lf", &cps);
  24. printf("\tEnter the number of containers: ");
  25. scanf("%d", &containers);
  26. printf("\n\t ***** OUTPUT *****\n");
  27. printf("\n\tRadius of the container %14.2lfcm\n\n", radius);
  28. printf("\tHeight of the container %14.2lfcm\n\n", height);
  29. printf("\tCost per square centimeter %11.2lf\n\n", cps);
  30. printf("\tNumber of containers %17.2d\n\n", containers);
  31. printf("\t***** CALCULATED VALUES *****\n\n");
  32. cpc = cost_of_each_container(cost);
  33.  
  34. getch();
  35. return 0;
  36. }
  37.  
  38. void purpose(void)
  39. {
  40. printf("\n This program computes the cost per container and the total cost \n");
  41. printf(" To use this program the user has to enter the radius, height, and cost per square \n");
  42. printf(" from the keyboard and the program calculates the cost per container and total cost \n");
  43. printf(" then prints out the radius, height, cost per square centimeter, number \n");
  44. printf(" of containers, and the total\n\n\n");
  45. }
  46.  
  47. double cost_of_each_container(double cost)
  48. {
  49. double cpc;
  50.  
  51. cpc = containers * cps;
  52.  
  53. return(cpc);
  54. }


I'm trying to call the function cost_of_each_container but its not working and i have no idea what i'm doing wrong please help. I know the spacing in the void purpose part is bad but i will fix that later. And if someone could tell me the math for the function cost_of_each_container because i know thats not right (i am trying to find the cost per container). Thanks!



try this code, it will now work, you still need to calculate values

  1. #include<conio.h>
  2. #include <stdio.h>
  3. void purpose(void);
  4. double cost_of_each_container(int containers, double cps);
  5. double total_cost_of_containers(double total);
  6. int main(void)
  7. {
  8. double cost;
  9. double radius;
  10. double height;
  11. double cps;
  12. int containers;
  13. double cpc;
  14. double total;
  15.  
  16. purpose();
  17.  
  18. printf("\tEnter the radius of the base of the container in cm: " );
  19. scanf("%lf", &radius);
  20. printf("\tEnter the height of the container in cm: " );
  21. scanf("%lf", &height);
  22. printf("\tEnter the cost per square centimeter: " );
  23. scanf("%lf", &cps);
  24. printf("\tEnter the number of containers: " );
  25. scanf("%d", &containers);
  26. printf("\n\t ***** OUTPUT *****\n" );
  27. printf("\n\tRadius of the container %14.2lfcm\n\n", radius);
  28. printf("\tHeight of the container %14.2lfcm\n\n", height);
  29. printf("\tCost per square centimeter %11.2lf\n\n", cps);
  30. printf("\tNumber of containers %17.2d\n\n", containers);
  31. printf("\t***** CALCULATED VALUES *****\n\n" );
  32. cpc = cost_of_each_container(containers,cost);
  33.  
  34. getch();
  35. return 0;
  36. }
  37. void purpose(void)
  38. {
  39. printf("\n This program computes the cost per container and the total cost \n" );
  40. printf(" To use this program the user has to enter the radius, height, and cost per square \n" );
  41. printf(" from the keyboard and the program calculates the cost per container and total cost \n" );
  42. printf(" then prints out the radius, height, cost per square centimeter, number \n" );
  43. printf(" of containers, and the total\n\n\n" );
  44. }
  45.  
  46. double cost_of_each_container(int containers, double cps)
  47. {
  48. double cpc;
  49.  
  50. cpc = containers * cps;
  51.  
  52. return(cpc);
  53. }


its output is like this
m
0
l
!