Thread: Need help in C

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    Need help in C

    Hi all,
    I am new in programming with C. Will you please help me to solve the following problem:

    I want a program whose function is to enter the volume of a sphere 'x' and the program has to find out the unknown radius of the sphere and when the program finds radius, the program has to tell you if the sphere can go through the pipe or not.

    Code:
    #include <stdio.h>
    
    void main ()
    
    {
    	float a,b;
        printf("If you have a sphere with a volume of 'x', will it fit in a pipe with a radius of 5cm ??\n\n");
    	printf("Enter volume 'x': ");
    	scanf("%f",&a);
    	printf("The radius of the shpere is: %f\n\n",((a*3)/(4*3.142))^0.333);
    	b= ((a*3)/(4*3.142))^0.333;
    
    	if (b>5)
    	    {printf("The sphere will not fit in the pipe.\n\n\n\n");}
    	else if (b<5)
    	    {printf("The sphere will fit in the pipe.\n\n\n\n");}
    	else
    	    printf("The sphere will fit in the pipe tightly.\n\n\n\n");}
    }
    Last edited by drinu1232; 10-30-2010 at 09:50 AM.

  2. #2
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Hi Drinu1232, fist use code tags... read the link you went by before this forum...

    You'll get help quickly that way. Also, If you should be able to "edit" your post,
    insert the code tags, then go on from there. Welcome to the board!

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by drinu1232 View Post
    Hi all,
    I am new in programming with C. Will you please help me to solve the following problem:

    I want a program whose function is to enter the volume of a sphere 'x' and the program has to find out the unknown radius of the sphere and when the program finds radius, the program has to tell you if the sphere can go through the pipe of not.
    From your posted source code...

    You need to look up the caret ( ^ ) operator I don't think it does what you think it does.

    Move the calculation of b above your first printf() and have printf print b instead of doing the math twice. It will also give you confirmation that b is calculated correctly.

    There's no need for multiple ifelse structure. The sphere either fits or it doesn't...
    Code:
    if (b < 5)
      printf("it fits");
    else
      printf(it don't fit");
    Also please use code tags when posting your code to the forum.
    Last edited by CommonTater; 10-30-2010 at 07:00 AM.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    From your posted source code...

    You need to look up the caret ( ^ ) operator I don't think it does what you think it does

    so what can i use instead of ^ ??

    Can someone please fix the error for me since i can hardly understand a thing from what you are saying :O (im still a beginner)

    thx in advance

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by drinu1232
    so what can i use instead of ^ ??
    pow from <math.h>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    Quote Originally Posted by laserlight View Post
    pow from <math.h>
    so is have to write '#include <math.h>' under '#include <stdio.h>' ??
    ... and type 'pow' instead of '^' ??

    thx

  7. #7
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Smile

    Quote Originally Posted by drinu1232 View Post
    so is have to write '#include <math.h>' under '#include <stdio.h>' ??
    ... and type 'pow' instead of '^' ??

    thx
    Yes, you need to include the math.h file. The pow(x,y) as laserlight suggested,
    in most compilers, expect type doubles.

    Btw, I modified your program and got it working aprox. 10 mins after your post.

    You have to show some effort in this. There are many people here that can
    help you, but you should look at some of the FAQ's on this board. There is
    a wealth of information there. :-)

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    i wrote #include <math.h> under #include <stdio.h> and pow instead of ^ , and the program is still giving me an error :S pls need help!!

  9. #9
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by drinu1232 View Post
    i wrote #include <math.h> under #include <stdio.h> and pow instead of ^ , and the program is still giving me an error :S pls need help!!
    1) No mention of what error you are getting.
    2) No update of what your code looks like.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    5
    These are the errors/warnings that i am getting:

    (11) : error C2143: syntax error : missing ';' before 'constant'
    (11) : warning C4550: expression evaluates to a function which is missing an argument list
    (11) : error C2059: syntax error : ')'
    (12) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    (12) : error C2143: syntax error : missing ';' before 'constant'
    (12) : warning C4550: expression evaluates to a function which is missing an argument list

    My problem is that i never used the <math.h> before.... and i dont know what the above errors are referring to.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void main ()
    
    {
    float a,b;
    printf("If you have a sphere with a volume of 'x', will it fit in a pipe with a radius of 5cm ??\n\n");
    printf("Enter volume 'x': ");
    scanf_s("%f",&a);
    printf("The radius of the shpere is: %f\n\n",((a*3)/(4*3.142))pow 0.333);
    b=((a*3)/(4*3.142))pow 0.333;
    
    if (b>5)
    {printf("The sphere will not fit in the pipe.\n\n\n\n");}
    else if (b<5)
    {printf("The sphere will fit in the pipe.\n\n\n\n");}
    else
    {printf("The sphere will fit in the pipe tightly.\n\n\n\n");}
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    pow() is a function.
    Call it like you call every other function.
    math.h man page
    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.

  12. #12
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by drinu1232 View Post
    i wrote #include <math.h> under #include <stdio.h> and pow instead of ^ , and the program is still giving me an error :S pls need help!!

    How To Use pow(x,y)

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Aaaannnnnddd...another selfish crossposter.

  14. #14
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

    Thumbs down

    Quote Originally Posted by rags_to_riches View Post
    LMAO, RtR you mean all this time? I need to be demoted from the cross post
    team here - or at least get a demerit. Also, I'll have to bring this up with my priest during my next confession.

  15. #15
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    Quote Originally Posted by Salem View Post
    pow() is a function.
    Call it like you call every other function.
    math.h man page
    I just noticed this link. Bookmarked. So it turns out now, this thread
    wasn't a complete waste of my time.

Popular pages Recent additions subscribe to a feed