Like Tree1Likes
  • 1 Post By CommonTater

Calculating hypotenuse, missing remainder.

This is a discussion on Calculating hypotenuse, missing remainder. within the C Programming forums, part of the General Programming Boards category; Hello I'm a noob and I wrote this piece of code: Code: #include <stdio.h> #include <math.h> int CalcHyp(int cat1, int ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Calculating hypotenuse, missing remainder.

    Hello I'm a noob and I wrote this piece of code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int CalcHyp(int cat1, int cat2);
    int CalPer(int l1, int l2, int l3);
    
    int main()
    {
        int l1,l2,l3,cat1,cat2;
        int answer;
        printf("Choose options in calculator: \n1 - perimeter \n2 - hypotenuse\n"
                "Exit (any other key or letter)\n");
        scanf("%d", &answer);
        switch(answer)
        {
            case 1:
                printf("Insert the three lenghts:\n");
                scanf("%d %d %d", &l1,&l2,&l3);
                printf("This is the perimeter: %d\n\n",CalPer(l1,l2,l3));
                main();
                break;
            case 2:
                printf("Insert the two catheti:\n");
                scanf("%d %d", &cat1,&cat2);
                printf("This is the hypotenuse: %f\n", CalcHyp(cat1,cat2));
                main();
                break;
            default:
                printf("Exit\n");
    
        }
    
    }
    
    int CalcHyp(int cat1, int cat2)
    {
        float Hyp;
        Hyp = sqrt(pow(cat1,2)+pow(cat2,2));
        return Hyp;
    }
    
    int CalPer(int l1, int l2, int l3)
    {
        int perimeter;
        perimeter = l1+l2+l3;
        return perimeter;
    }
    When I want to calculate hypotenuse, I have the integer value of it and no remainder. I tried to make Hyp variable float type but I can't get it.

    Hope you understand what's written in printfs cause I'm not a native speaker. Thanks for help.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Code:
    int CalcHyp(int cat1, int cat2)
    The compiler casts Hyp back to an int because that's what the function returns. Try making your function return a float (and the prototype at the top too, or course).

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    In addition to anduril's comment, here are some other things to consider:

    - Don't use variable names that start with digits
    - The "pow()" and "sqrt()" functions expect the double data type, not integer

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
                printf("This is the perimeter: %d\n\n",CalPer(l1,l2,l3));
                main();
                break;
    Calling main() from inside a function is not a good idea. You end up with a recursive program that keeps tunnelling deeper and deeper until it runs out of stack space and crashes.

    If you want your program to start over... use loops.

    Also... the correct form of main is int main (void) and it returns a value (usually 0)...
    Code:
    int main (void)
      { 
    
         // your code here
    
       return 0; 
    }
    anduril462 likes this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by Matticus View Post
    - Don't use variable names that start with digits
    They start with a lower case ell, not a 1, though I agree it's difficult to tell. It wouldn't compile otherwise.

    The "pow()" and "sqrt()" functions expect the double data type, not integer
    The operands will be automatically promoted to doubles, so there's no problem there, though there is lost precision when it tries to stuff the double result into a single float.

    @Blacklukes: You should change all your floats to doubles. Also, you said main would return an int, so return one. Zero is common for success.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Thank you very much for your answers, I modified the CalcHyp function and its prototype. I tried float and double and it works with both. But I still can't figure out how to start over the program. Can I do it keeping the switch/case or do I have to change it?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Quote Originally Posted by CommonTater
    Also... the correct form of main is int main (void) and it returns a value (usually 0)...
    Blacklukes's definition of main is also valid though, especially with respect to C99. (But you knew that, didn't you )
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    I know float and double both work, but I recommend double for precision. As for starting the program over, look into loops. We have a tutorial here, and I'm sure any class notes or textbooks you have will cover them too. I would use a do while loop (since it always executes at least once, unlike while or for loops). Try something like:
    Code:
    do
        prompt user for calculation type
        get answer
        switch (answer)
    while answer is 1 or 2
    EDIT: Tutorial link: http://www.cprogramming.com/tutorial.html#ctutorial

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    Thanks anduril462 now it loops. Thank all for support this was a great first time asking

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Blacklukes View Post
    Thank you very much for your answers, I modified the CalcHyp function and its prototype. I tried float and double and it works with both. But I still can't figure out how to start over the program. Can I do it keeping the switch/case or do I have to change it?

    Code:
    do 
      { 
        // your code here
      }
      while (not_exit_condition);
    Loops are your friend.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Blacklukes's definition of main is also valid though, especially with respect to C99. (But you knew that, didn't you )
    Well, after all the talk about portability and compatibility here... it makes far more sense to use int main (void) as a standard that works everywhere.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,374
    Quote Originally Posted by anduril462 View Post
    They start with a lower case ell, not a 1, though I agree it's difficult to tell. It wouldn't compile otherwise.
    My mistake.

    The operands will be automatically promoted to doubles, so there's no problem there, though there is lost precision when it tries to stuff the double result into a single float.
    I understand this, I just thought it would be in the interest of the OP to be consistent with data types while on the learning curve, so they can fully understand their code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2010, 10:39 AM
  2. Replies: 7
    Last Post: 10-02-2010, 03:44 PM
  3. Hypotenuse Problem
    By dmkanz07 in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 10:07 AM
  4. how to get remainder of a value
    By seal in forum C Programming
    Replies: 2
    Last Post: 09-22-2005, 07:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21