No output No error whats wrong

Discussion in 'Programmer's Corner' started by mkbutan, Aug 2, 2012.

  1. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    hi sir is this code correct? pl help.
    Code ( (Unknown Language)):
    1.  
    2. /*
    3.   Name: mkbutan
    4.   Copyright:
    5.   Author:
    6.   Date: 02/08/12 23:06
    7.   Description: exec 1.5 pg21
    8. */
    9. #include<stdio.h>
    10. #include<conio.h>
    11. void main()
    12. {
    13.      int r,x;
    14.      float pi;
    15.      printf("Enter the Radius : \n",r);
    16.      scanf("%d",r);
    17.      x = (2*pi*r)*(2*pi*r);
    18.      printf("The Area of a Circle is : %d",x);
    19.      getch();
    20.      }
    21.  
     
  2. bretm

    Member

    Feb 6, 2012
    152
    24
    You haven't assigned a value to the pi variable.

    You're passing in "r" to the first printf, but you don't need to print r. I don't know why you're getting "no output" but it might be related to this.

    The formula for circle area is pi * r * r. Your result will be 4*pi times too large.

    x is int, not float, so the fractional portion of the answer will be truncated. If you change it to float be careful to also change the %d in the last printf.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    What do you mean when you say "No output", do you mean that it isn't outputting anything at all, not even your "The Area of a Circle is : " string?

    printf("Enter the Radius : \n",r);

    Here you pass it an argument but there is no format specifier in the format string. The messes up the stack. In this case, you are probably okay. But don't pass 'r' if you aren't going to do anything with it and don't do anything with it prior to initializing it.

    What is pi supposed to be? The compiler can't read your mind and make it what you would like it to be. You have to initialize it. If it is being initialized to a real small value (roughly a 50% change of this, since it is a float), you are probably ending up with a value in x that is getting truncated to zero (since it is an int).

    Why are you squaring both the 2 and pi?

    Why are you making the radius and the area integers?
     
  4. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    its a Question in the book of
    PROGRAMMING IN ANIC C
    the question is :-

    Given the radius of a circle, write a program to compute and display its area.
    Use a symbolic constane to define the ∏(pi) value and assume a suitable value for radius.


    my Answer

    Code ( (Unknown Language)):
    1. /*
    2.   Name: mkbutan
    3.   Copyright:
    4.   Author:
    5.   Date: 03/08/12 13:50
    6.   Description: exec 1.5 pg 21 New
    7. */
    8. #include<stdio.h>
    9. #include<conio.h>
    10. void main()
    11. {
    12.      int r,x;
    13.      float pi;
    14.      /*printf("Enter the Radius : \n",r);
    15.      scanf("%d",r);*/
    16.      pi = (22/7);
    17.      r = 5;
    18.      x = (pi*r*r);
    19.      printf("The Area of a Circle is : %d",x);
    20.      getch();
    21.      }
    but i dont get any output and there is no error

    Use a symbolic constane to define the ∏(pi) value
    how to write the symbol of pi from the keyboard

    pl help
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    mkbutan likes this.
  6. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    Your screen shots don't jive. Your Console in the upper right shows it printing out the prompt for the radius, yet you have that code commented out in the source code listing to the left.

    We need to see the code and the result that is produced by that same code.

    Please don't use big fonts and colors in your posts; most people find it annoying.

    To put the pi symbol in a post, use Alt-227 (using the numbers on the keypad and NOT the numbers along the top row of the keyboard). It will look like: π, which is a pretty poor pi symbol. I usually just type pi or PI.

    If you are asking how to enter that greek letter symbol into your source code, you can't. Source code is ASCII text.

    You are asked to use a symbolic constant. The more correct term (at least in the C world) is a "non-parameterized macro", but they are also commonly called just a #define constant or even just a #define. Both are a bit sloppy as far as terminology goes, but they get the point across.

    So you want to do something like the following:

    #define FRED (4.3827317)

    FRED is now a symbolic constant and you can have things like:

    y = 2*FRED + 3;

    When you go:

    pi = (22/7);

    The variable pi will be set equal to 3. That is because the expression (22/7) must be evaluated before the compiler considers the type of variable that it will eventually be stored in. For a variety of reasons, when given the option of using integer division, the compiler will do so. This means that 22/7 is truncated and the fractional part is discarded. To avoid this, get in the habit of including ".0" on all literal constants when you want them to be treated as reals (floating point values). So use

    pi = (22.0/7.0);

    You should use either

    int main(void)

    or

    int main(int argc, char* argv)

    for your main() declaration. Older texts taught the use of void main() but this is deprecated and can get you into trouble. So just get in the habit of always using one of the two recommended and have main() return a value of 0 (or, even better, use

    return EXIT_SUCCESS;

    to exit main. Note that I don't know if these recommendations apply for non-hosted environments, such as code targeted for a microcontroller.
     
    mkbutan likes this.
  7. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,810
    834
    I think WBahn may have found the problem...

    The "/*" just before your printf statement is the start of a comment. It looks like the remainder of your program is commented out, hence it is not doing anything.

    Code ( (Unknown Language)):
    1. /*
    2. Name: mkbutan
    3. Copyright:
    4. Author:
    5. Date: 03/08/12 13:50
    6. Description: exec 1.5 pg 21 New
    7. */
    8. #include<stdio.h>
    9. #include<conio.h>
    10. void main()
    11. {
    12. int r,x;
    13. float pi;
    14. [B][COLOR=red]/*[/COLOR][/B]printf("Enter the Radius : \n",r);
    15. scanf("%d",r);*/
    16. pi = (22/7);
    17. r = 5;
    18. x = (pi*r*r);
    19. printf("The Area of a Circle is : %d",x);
    20. getch();
    21. }
     
  8. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    This will set pi to 3, because division of integer numbers results in integer. You need to use pi=22.0/7 to get a (more) correct answer.
    Best way is to #include <math.h> and use M_PI macro instead.
     
    Last edited: Aug 11, 2012
  9. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Here's what's wrong:

    Code ( (Unknown Language)):
    1.  
    2. Date: 02/08/12 23:06
    3.  
    Is this 2002 Aug 12?
    Feb 08, 2012?
    02 Aug 2012?

    Programmers should set the standard and write unambiguous code.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    pi is not 22/7. This is a crude approximation for pi.
    p = 4 *atan(1) is more precise.
    If you wish to avoid using a transcendental function
    355.0/113.0 is good to 6 decimal places.
    Or just enter
    pi = 3.14159265358;
    but use #define pi 3.14159265358
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I am pretty sure that 02/08/12 meaning 02 Aug 2012 is the standard. Unfortunately not in the USA.
    http://en.wikipedia.org/wiki/Date_format_by_country
    And by the way the header looks like it is automatically generated, so don´t blame the programmer for this one.
     
  12. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    So according to this there is NO standard.

    02012.08.02 23:06 UTC is totally unambiguous.

    btw, check out http://longnow.org/
     
  13. mkbutan

    Thread Starter Senior Member

    Sep 30, 2008
    270
    16
    hi
    its working

    Code ( (Unknown Language)):
    1. #include<stdio.h>
    2. #include<conio.h>
    3. #include<math.h>
    4. void main()
    5. {
    6.      int r;
    7.      float a,pi;
    8.      printf("Enter the Radius :\n");
    9.      scanf("%d",&r);
    10.      pi = 3.1416;
    11.      a = (pi)*(r*r);
    12.      printf("The Area of a Circle is :%f",a);
    13.      getch();
    14.      }
    15.  
     
  14. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Since you´re using math.h you can change it to a= M_PI * r*r;
    You don´t need the parenthesis around multiplication.
     
Loading...