Thread: Need help with relatively basic program in C

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Need help with relatively basic program in C

    So I'm writing a program in C, and am trying to figure something out. This particular assignment was easy until I got to part 5. The instructions are as follows:
    Part 5 - Write a main function that uses get_value() in order to querythe user for the width and height of a right triangle. Then, if the width andheight are valid values, the program displays 3 choices:1. Calculate length of hypothesis.2. Calculate area of the triangle.3. Calculate the perimeter of the triangle.Then, based on the choice entered by the user (1-3) the program calls theappropriate function and displays the results. If the inputs are not correctthen an error message is displayed.
    These instructions are supposed to be applied to the code I've already written so far, which is
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double get_value();
    double calc_hypotenuse(double h, double w);
    double calc_area(double h4, double w4);
    double calc_perimeter(double h3, double w3, double hypotenuse);
    
    
    int main()
    {
        double hh, ww;
        double hhh, www;
        double hhhh, wwww;
        double hhhhh, wwwww;
    
    
        printf("%.2lf\n\n", get_value());
        
        printf("Enter a height and width for a right triangle to calculate the length of the hypotenuse.\n");
        printf("Height: ");
        scanf("%lf", &hh);
        printf("Width: ");
        scanf("%lf", &ww);
        printf("The length of the hypotenuse is %.2lf.\n\n", calc_hypotenuse(hh, ww));
    
    
        printf("Enter a height and width for a right triangle to calculate the area.\n");
        printf("Height: ");
        scanf("%lf", &hhhh);
        printf("Width: ");
        scanf("%lf", &wwww);
        printf("The area is %.2lf.\n\n", calc_area(hhhh, wwww));
    
    
        printf("Now enter a height and width for a right triangle to calculate the perimeter.\n");
        printf("Height: ");
        scanf("%lf", &hhh);
        printf("Width: ");
        scanf("%lf", &www);
        printf("The perimeter is %.2lf.\n\n", calc_perimeter(hhh, www, calc_hypotenuse(hhh, www)));
        
        return 0;
    }
    
    
    double get_value()
    {
        double value;
    
    
        printf("Enter a value of type double between 0.0 and 10,000.0: ");
        scanf("%lf", &value);
    
    
        if ((value > 0) && (value < 10000))
            return value;
        else
            return -1;
    }
    
    
    double calc_hypotenuse(double h, double w)
    {
        return sqrt(pow(h, 2) + pow(w, 2));
    }
    
    
    double calc_area(double h4, double w4)
    {
        return .5*(h4 * w4);
    }
    
    
    double calc_perimeter(double h3, double w3, double hypotenuse)
    {
        return h3 + w3 + hypotenuse;
    }
    I just don't quite understand how I'm supposed to get the get_value( ) function to essentially perform differently the second time I call on it. Any help is MUCH appreciated. Thanks guys. By the way, I'd appreciate it if you could explain instead of just posting some code.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well have you thought about why this works?
    printf("%.2lf\n\n", get_value());

    It would work for other purposes, like setting the values of hh and ww and so on.

    I think the instructions just want you to replace scanf() calls in main() with this more complex input statement.

    It would help also if you wrote another function for getting the choice in the menu. It isn't quite the same as getting a value because the menu choices are all integers.
    Last edited by whiteflags; 09-22-2016 at 12:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for help on a basic program
    By hamsuite in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2007, 09:56 PM
  2. Help with basic C++ program
    By ladysniper in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2006, 05:15 PM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. Basic Program but I need HELP
    By michigan353 in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 03:41 PM
  5. Help with a Basic C Program!!
    By KIwi_Fella in forum C Programming
    Replies: 10
    Last Post: 11-01-2002, 10:04 AM

Tags for this Thread