Thread: iam a noob and i need help please

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    Wink iam a noob and i need help please

    Dear gurus
    iam crying for help with my program. i can not get anything to appear on the screen. can someone explain to me what i did wrong.
    Please someone help me out iam a nob, so i only know basic coding.
    here is my program.

    This program should have 3 functions
    A) explain() explain the program to the user
    B)get_radius() input a radius and return a value to main()
    C)calc_display() calculate the surface and prints the surface area and volume using the volumes #define statement and display this values to 3 decimal places

    Formula
    volume of sphere = (4/3)*3.14*radius^3
    surface of area of spehere = 4*3.14*raidus^2


    /*
    Title: Sphere
    Name: TONY
    Date: feb 21 2008
    Description: calculate the surface area and volume of a sphere.
    The user will input a radius and the program will
    do both calculations
    */

    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<math.h>

    #define radius (diameter/2)
    #define (radius) pow((4/3)* 3.14*pow(radius,3) // formula volume of a sphere
    #define 4*3.14*pow(radius,2) // formula surface area of a sphere

    // Function Prototype
    void explain (void);
    float get_radius (float a);
    void calc_display (float area, float volume);

    // Function Call
    main()
    {
    float answer, t, e;
    system("cls");
    explain();
    e=get_radius(t);
    calc_display(e,t);
    system("cls");
    getch();
    return 0;
    }


    //Function definition
    void explain (void)
    { puts("");
    puts("");
    }

    float get_radius (float a)
    { printf("please give me the radius");
    scanf("&#37;f",&a);
    return(a);
    }

    void calc_display (float area, float volume)
    {
    printf("The area is %.3f and the volume %.3f", area, volume);
    }
    Last edited by joker_tony; 02-22-2008 at 12:07 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    edit your message - add code tags around the code, make sure you have proper indentation (think about people you ask to help you)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Some things I can spot right away are:
    * main should return int.
    * It's better to use getchar() instead of getch()
    And maybe you should also explain a little better what "no output" means. Do you get the typical dos window? And is it just blank?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Basic thoughts: you should really only use #define to define constants. You can use #define to make macros, but it's not something for the faint of heart. None of those things you are #defining are constants. --- Looking at your posted assignment, that makes no sense. Either you or your instructor have lost your mind.

    You should maybe take the time to calculate area and volume before printing them out.

    I don't know what you think you're doing with get_radius.

    Suddenly in the calc_display function, the variable names make sense. You should try to imitate that throughout the program.

    You should post code using the code tags. (Look for the # sign in the editor.)

    Even if something prints, you'll never see it since you immediately clear the screen. Don't do that.

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Instead of using #define statements, and since you seem to understand how to write a function, why not write a function like get_radius, that will be called get_volume and pass in radius as a parameter, and return the formula calculated over that parameter?

    Your entire program looks like you ain't got a clue what you are writing. I don't understand how it even compiles?
    Code:
    #define 4*3.14*pow(radius,2) // formula surface area of a sphere
    Think what you are #defining here if you know that the syntax of #define is:
    #define IDENTIFIER TEXT...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    instead of pow(radius,2) use
    radius*radius

    you do not know if the pow function if optimized to process the square in a special way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    /*
    Title: Spherical Crap
    Name: BELIAL
    Date: 30 AD
    Description: Give the radius, and yee shall receive
      the surface area and volume of a sphere...amen.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    // XXX I removed <conio.h>, getch() does not work on my Linux machine :-(
    
    
    // XXX I got rid of the diameter #define and "fixed" the below #define's
    #define RADIUS_TO_VOLUME ((4/3)* 3.14*pow(radius,3))
    #define RADIUS_TO_AREA (4*3.14*pow(radius,2))
    
    // Function Prototype
    void explain (void);
    float get_radius (float a);
    void calc_display (float radius); // XXX fixed the parameters of this function :-D
    
    
    main()
    {
      float answer, t, e;
      system("cls");
      explain();
      e=get_radius(t);  // XXX 't' and 'e' are the same variables...interesting!
      calc_display(e);
      system("cls");
      return 0; // XXX you returned 0!  good job!  (not many ppl remember this)
    }
    
    
    //Function definition
    void explain (void)
    { puts("");
    puts("");
    }
    
    float get_radius (float a)
    { printf("please give me the radius");
    scanf("%f",&a);
    return(a);
    }
    
    void calc_display (float radius)  // XXX FIxed the parameters ;)
    {
    float volume = RADIUS_TO_VOLUME;  // XXX added this to calculate the volume and area
    float area = RADIUS_TO_AREA;      // from the #define's.
    printf("The area is %.3f and the volume %.3f", area, volume);
    }

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    main should be
    Code:
    int main(void)
    get_radius does not need a parameter
    Code:
    float get_radius (void)
    {
    	float radius;
    	printf("please give me the radius: ");
    	fflush(stdout); /* in case the stdout stream is buffered */
    	if(scanf("&#37;f",&radius) == 1)
    		return radius;
    
    	/* incorrect input */
    	return -1;
    }
    and so on

    macros should be made into functions, compiler will inline them when needed
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    How did we all fall for this? The person who made this post, is obviously joking...
    Check the nickname, and the smiley besides the 'i am a noob'
    And the biggest joke is the posted code.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed