Thread: volume of geometric shapes.

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    2

    Post volume of geometric shapes.

    Hello everyone.I need help.I am learning c programming but ı am not good at.My teacher give homework.My teacher wants a five the volume of the geometric shape.1. sphere 2. cylinder 3. cube 4.square prism.5.cone.The user will enter the number of the desired shape.Then the shape of the volume will be calculated.The desired shape only will be calculated.I know ıt s a long question, but I don't know if and else.please help me, thanks.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The general steps are simple:
    1) Ask which solid it is
    2) Ask additional information depending on the kind of solid( radius for sphere, height for cone etc )
    3) Calculate the volume for that shape
    4) Output the result for the user to see

    Steps 2 and 3 can be done with switch case or with if statements, both would be equivalent in this case.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    2
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    
    int main(void) {
    	int secim;
    			/*cylinder,cube, square pyramid,  cone,    sphere*/
    	printf("1.Silindir\n2.Küp\n3.Kare Piramit\n4.Koni\n5.Kure\n");
    	        /*please enter the number of the prism*/
    	printf("\nHacmi hesaplanacak prizmanin numarasini giriniz");
    	scanf("%d",&secim);
    	if(secim==1);
    	                          /*volue*/
    	{   float	pi=22/7.0,r,h,hacim;
    	             /*enter the height and radius.*/
    	    printf("\nLutfen hacmi hesaplanacak silindirin yaricapi ve yükseligini giriniz");
    	    scanf("%d %d",&r,&h);
          	hacim=pi*r*r*h;
          	        /*the volume of the cylinder.*/
        	printf("\nSilindirin hacmi %8.6f'dir.\n",hacim);
        }
    	(secim==2);
    	{   float pi=22/7.0,a,hacim;
    	             /*enter the length of the edge.*/
    	    printf("\nLutfen hacmi hesaplanacak kupun kenar uzunlugunu giriniz");
            scanf("%d",&a);
            hacim=a*a*a;
                     /*the volume of the cube.*/
            printf("\nKupun hacmi %8.5f'dir.\n",hacim);
        }
        if(secim==3);
        { float a,h,hacim;
                  /*enter the length of the edge.*/
          printf("\nLutfen hacmi hesaplanacak kare piramidin kenar uzunlugunu ve yüksekligini giriniz");
          scanf("%d %d",&a,&h);
          hacim=a*a*h/3;
                     /*the volume of the cylinder.*/
          printf("\nSilindirin hacmi %8.4f'dir.\n",hacim);
    	}
    	if(secim==4);
    	{
    	  float r,h,pi=22/7.0,hacim;
    	          /*enter the height and radius.*/
    	  printf("\nLutfen hacmi hesaplanacak koninin yarıicapi ve yüksekligini giriniz");
    	  scanf("%d %d",&r,&h);
    	  hacim=pi*r*r*h/3;
    	            /*the volume of the cone.*/
    	  printf("\nKoninin hacmi %8.3f'dir.");
    	}
    	if(secim==5);
    	{
          float pi=22/7.0,r,hacim;
                   /*enter the radius*/
    	  printf("\nLutfen hacmi hesaplanacak kurenin yaricapini giriniz");
    	  scanf("%d",&r);
    	  hacim=4/3*pi*r*r*r;	
    	           /*the volume of the sphere.*/
    	  printf("\nKurenin hacmi %8.2f'dir.");
    	}
    	return 0;
    }
    I've done it.but I have a problem.I don't get the error when compiling the program.but the result is always 0.my language is Turkish.I hope you understand.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You could try using a different (that is, up to date) compiler. If you're using TurboC, your college is stealing your future.
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘main’:
    main.c:19:12: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
          scanf("%d %d",&r,&h);
                ^
    main.c:19:12: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘float *’ [-Wformat=]
    main.c:24:2: warning: statement with no effect [-Wunused-value]
      (secim==2);
      ^
    main.c:28:15: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
             scanf("%d",&a);
                   ^
    main.c:25:12: warning: unused variable ‘pi’ [-Wunused-variable]
      {   float pi=22/7.0,a,hacim;
                ^
    main.c:37:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
           scanf("%d %d",&a,&h);
                 ^
    main.c:37:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘float *’ [-Wformat=]
    main.c:47:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
        scanf("%d %d",&r,&h);
              ^
    main.c:47:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘float *’ [-Wformat=]
    main.c:50:11: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
        printf("\nKoninin hacmi %8.3f'dir.");
               ^
    main.c:44:24: warning: variable ‘hacim’ set but not used [-Wunused-but-set-variable]
        float r,h,pi=22/7.0,hacim;
                            ^
    main.c:57:10: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]
        scanf("%d",&r);
              ^
    main.c:60:11: warning: format ‘%f’ expects a matching ‘double’ argument [-Wformat=]
        printf("\nKurenin hacmi %8.2f'dir.");
               ^
    main.c:54:25: warning: variable ‘hacim’ set but not used [-Wunused-but-set-variable]
           float pi=22/7.0,r,hacim;
                             ^
    A couple of points.
    1. Watch your printf/scanf formats - make sure they match exactly. For example, scanning an int (with %d) into a float isn't going to work.
    2. The ; at the end of every if statement means 'do nothing'. The code which follows in the braces always happens.
    3. 22/7 is a rubbish approximation of PI.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Geometric Calculator
    By Alcherian024 in forum C Programming
    Replies: 2
    Last Post: 12-07-2015, 12:25 AM
  2. Replies: 6
    Last Post: 09-29-2011, 04:23 AM
  3. USB Problem : Volume to Volume copy by sector
    By anuj7anuj in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2011, 08:47 AM
  4. Raw volume access (to a usb volume)
    By _izua_ in forum Windows Programming
    Replies: 9
    Last Post: 11-02-2009, 07:05 AM
  5. Arithmetic/Geometric/Harmonic
    By DJ_Steve in forum C Programming
    Replies: 12
    Last Post: 08-23-2009, 08:34 PM

Tags for this Thread