Thread: Finding volume of cone error help!

  1. #1
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19

    Finding volume of cone error help!

    So my code runs but the problem is that the radius is always 0! Not sure what the problem is tried changing it to double and int and still won't work any help would be appreciated! I have attached the source codes below for you guys to see Btw boss.c is my header the calling program and iff.h is my source code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <graph.h>
    #include <math.h>
    
    #include "screen.h"
    #include "iffs.h"                             
    
    int main()
    {
       ClearTheScreen();
    
       PauseTheScreen();
    
       Heading();
    
       TriangleArea(10,20,2,5,7);
    
       VolumeOfSphere(12,20,5);
    
       VolumeOfCone(14,20,8,10);
     
       Footer();
    
    }
    Code:
    void TriangleArea(int row, int col, double a, double b, double c)
    {
       double Area;
       _settextposition(row,col);
    
       Area = a + b + c / 2;
    
       Area = Area * (Area - a) * (Area - b) * (Area - c);
    
       Area = sqrt(Area);
       printf("The area of a triangle with sides 2, 5, 7 = %.3f",Area);
    }                                                  
    void VolumeOfSphere(int row, int col, int sun)
    {
       int Volume;
       _settextposition(row,col);
    
       Volume = 4/3 * 3.14159 * sun * sun * sun;
       printf("Volume of sphere with radius %2d = %2d", sun, Volume);
    }
    void VolumeOfCone(int row, int col, int star, int moon)
    {
       int Volume;
       _settextposition(row,col);
    
       Volume = (1/3) * 3.14159 * star * star * moon;
       printf("Volume of cone with radius %d height %d = %d",Volume,star,moon);
    }
    Attached Files Attached Files
    • File Type: c boss.c (926 Bytes, 131 views)
    • File Type: h Iffs.h (813 Bytes, 115 views)
    Last edited by Salem; 10-21-2012 at 12:11 AM. Reason: inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Volume = (1/3) * 3.14159 * star * star * moon;
    1/3 is done in integer arithmetic.

    Try 1.0 / 3.0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hungrymouth's Avatar
    Join Date
    Oct 2012
    Posts
    19
    Oh no wonder! Ok, it works now thank you!

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You shouldn't put the actual implementations in your header file. Usually you only put the function prototypes in the header file and define the functions in a source file (.c) with the same name.

    Thus your "iffs.h" should look like:
    Code:
    #ifndef IFFS_H  // to protect the header file to be 
    #define IFFS_H  // included more than once in your project
    
    void TriangleArea(int row, int col, double a, double b, double c);
    void VolumeOfSphere(int row, int col, int sun);
    void VolumeOfCone(int row, int col, int star, int moon);
    
    #endif
    and your "iffs.c" should look like:
    Code:
    #include <stdio.h> // always include every header you want to use in your 
    #include <math.h>  // source file (you will need another one for "_settextposition")
    
    #include "iffs.h"  // always include the prototypes to allow the compiler to check them with your definitions
    
    void TriangleArea(int row, int col, double a, double b, double c)
    
    {
    // here come the definitions of your functions
    // ...
    You have to include "iffs.h" in "boss.c" too.

    Bye, Andreas

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You have the same problem with 4/3, which is simply 1.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB Problem : Volume to Volume copy by sector
    By anuj7anuj in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2011, 08:47 AM
  2. need help, Volume of cone is always 0??
    By zakareyya in forum C Programming
    Replies: 5
    Last Post: 03-07-2010, 04:43 PM
  3. Raw volume access (to a usb volume)
    By _izua_ in forum Windows Programming
    Replies: 9
    Last Post: 11-02-2009, 07:05 AM
  4. Volume of a Cone Equation always equals 0
    By Devolution in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 03:13 AM
  5. Problems with vision cone
    By Waterbottle in forum Game Programming
    Replies: 1
    Last Post: 07-02-2008, 01:24 PM

Tags for this Thread