Thread: need help to logically lay out program

  1. #1
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    need help to logically lay out program

    I need to have the user input a degree calculate the sin, cos, and tan, then increment the user input degree by 15 for a total of 225 or 15 increments. The code below works fine, my concern lies with my numerous functions for the 16 lines of output I need to generate to the screen. I think an array would be the answer but I am not sure how to populate the array with inc or even how to call the array element to the function, but I am sure I can figure out how to do that, if indeed this is the best solution. Any suggestions would be grateful. I do not want this to be down for me, I do enjoy a challenge, but I seem to have just frustrated myself into submission. Thanks in advance for taking time to read this.

    //-------------------------Preprocessor Directives------------------------------
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #define RAD 57.295779

    //---------------------Function Prototype Declaration---------------------------
    int getfdeg(void);
    float getfrad(int deg);
    float getfsin(float rad);
    float getfcos(float rad);
    float getftan(float rad);
    int getfinc (int deg);
    float getfincsin (int inc);
    float getfinccos (int inc);
    //----------------------------Function Main-------------------------------------

    main()
    {




    //--------------------------Declare Variables-----------------------------------

    int deg;
    float rad;
    float sin;
    float cos;
    float tan;
    int inc;


    //---------------Input Variables-------------

    clrscr();



    //----------Data Manipulation Variables-------

    //------------Function Calls----------------

    deg = getfdeg();
    printf(" %d", deg);

    rad = getfrad(deg);
    printf(" %f",rad);

    sin = getfsin(rad);
    printf(" %f",sin);

    cos = getfcos(rad);
    printf(" %f", cos);

    tan = getftan(rad);
    printf(" %f", tan);

    inc = getfinc (deg);
    printf("\n %d", inc);

    rad = getfrad(inc);
    printf(" %f",rad);

    sin = getfsin(rad);
    printf(" %f",sin);

    cos = getfcos(rad);
    printf(" %f", cos);

    tan = getftan (rad);
    printf(" %f", tan);

    //--------------Output Variables--------------




    //-----------------------User Input Statements----------------------------------





    //-------------------------Output Statements------------------------------------



    getch();
    }

    //----------------------End of Function Main()----------------------------------
    //************************************************** ****************************



    //****************User Defined Function Definitions*****************************
    //************************************************** ****************************


    //*************************user definition of getfdeg()************************

    int getfdeg(void)
    {
    //purpose: to have user input a degree to be evaluated
    //argument: void
    //return type: int

    int getfdeg;
    printf("Please enter the degree to be evaluated then press enter.\t");
    scanf(" %d", &getfdeg);
    return getfdeg;
    }


    //*********************user defintion of getfrad(int deg)************************

    float getfrad(int deg)
    {
    //purpose: to covert degrees into radians for trig functions
    //argument: deg
    //return type: float

    float getfrad;
    getfrad = deg/RAD;
    return getfrad;
    }

    //*******************user definition of getfsin(float rad)**************************

    float getfsin(float rad)
    {
    //purpose: to evaluate the sine of rad
    //argument: rad
    //return type: float

    float getfsin;
    getfsin= sin(rad);
    return getfsin;
    }

    //***********************user definition of getfcos(rad)*****************************

    float getfcos(float rad)
    {
    //purpose: to evaluate the cosine of rad
    //arguement: rad
    //return type float

    float getfcos;
    getfcos = cos(rad);
    return getfcos;
    }

    //******************************user definition of getftan(rad)************************

    float getftan(float rad)
    {
    //purpose: to evaluate the tan of rad
    //arguement: rad
    // return type: float

    float getftan;
    getftan = tan(rad);
    return getftan;
    }
    //***************************user definiton of gfloop(deg)******************************

    int getfinc (int deg)
    {
    //Purpose: to increment the user inputed degree by 15
    //argument: deg
    //return type: int

    int getfinc;
    getfinc = deg + 15;
    return getfinc;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Most of those functions can be shortened by removing the temporary variable and simply returning the result of the expression, like so:
    Code:
    float getfsin(float rad) 
    {  
      return (float)sin(rad);
    }
    From what I can see, getfdeg does nothing but read a value and return it. That's hardly an operation that warrants a function to perform so you could get away with doing that in main.
    Code:
    #include <stdio.h> 
    #include <math.h> 
    #define RAD 57.295779f
    
    float getfrad(int deg){return deg/RAD;} 
    float getfsin(float rad){return (float)sin(rad);} 
    float getfcos(float rad){return (float)cos(rad);}
    float getftan(float rad){return (float)tan(rad);}
    int getfinc (int deg){return deg + 15;}
    
    int main ( void ) 
    {
      int deg; 
      float rad; 
      float sin; 
      float cos; 
      float tan; 
      int inc;  
    
      printf("Please enter the degree to be evaluated then press enter.\t"); 
      scanf(" %d", &deg);  
      rad = getfrad(deg);
      sin = getfsin(rad); 
      cos = getfcos(rad); 
      tan = getftan(rad);
      printf(" %d %f %f %f %f\n", deg, rad, sin, cos, tan);
    
      inc = getfinc (deg);  
      rad = getfrad(inc); 
      sin = getfsin(rad); 
      cos = getfcos(rad); 
      tan = getftan (rad);
      printf(" %d %f %f %f %f", inc, rad, sin, cos, tan);  
    
      getchar();
      return 0;
    }
    If you really want to decrease the number of functions, you can write one processing function and send pointers to it. This is always another alternative and shortens the program drastically, but you lose a little bit of readability with a long parameter list for the function:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define RAD 57.295779f
    #define DEGREES 15
    
    void getf ( int deg, float *prad, float *psin, float *pcos, float *ptan )
    {
      *prad = deg / RAD;
      *psin = (float)sin(*prad);
      *pcos = (float)cos(*prad);
      *ptan = (float)tan(*prad);
    }
    
    int main ( void )
    {
      int deg;
      float rad, sin, cos, tan;
    
      printf("Please enter the degree to be evaluated then press enter.\t");
      scanf(" %d", &deg);
      getf ( deg, &rad, &sin, &cos, &tan );
      printf(" %d %f %f %f %f\n", deg, rad, sin, cos, tan);
      getf ( deg += DEGREES, &rad, &sin, &cos, &tan );
      printf(" %d %f %f %f %f\n", deg, rad, sin, cos, tan);
    
      getchar();
      return 0;
    }
    What you choose to do depends on what you want out of the program, but all of the above work exactly the same as the code you posted.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Prelude, thanks for the help

    I do appreciate the time you took to reply to the post. That was exaclty what I was looking for. I have a tendency to over-use functions, as you correctly pointed out. I am having a difficult time really grasping arrays, which I believe furthers my dependency on fuctions. Well thanks again.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    Another tip you can use to decrease the amount of functions used [and lines coded too by the way] is using define directives as token replacements. For example

    Code:
    float getfrad(int deg){return deg/RAD;} 
    float getfsin(float rad){return (float)sin(rad);} 
    float getfcos(float rad){return (float)cos(rad);}
    float getftan(float rad){return (float)tan(rad);}
    int getfinc (int deg){return deg + 15;}
    
    //  would be changed to...
    
    #define GETFRAD(deg) ((deg)/RAD) 
    #define GETFSIN(rad) ((float)sin(rad))
    #define GETFCOS(rad) ((float)cos(rad))
    #define GETFTAN(rad) ((float)tan(rad))
    #define GETFINC(rad) ((rad) + 15)
    These would be declared at the preprocessors. It helps by eliminating the overhead of the function call. Keep in mind that it's a simple substitution, so there are no type checking. Keeping the parenthesis is very important to supercede the order of operations, and notice too that there are no semicolons in the macro replacement, so you can use it as an expression.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM