Thread: Recreate program so everything is done in main()

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    20

    Recreate program so everything is done in main()

    For my programming course. I need to find a away to convert the following code so that everything is done in main(). The program is in C just for clarification. Please help. Thanks

    /*
    Description: The user is asked to type in the BASE
    and HEIGHT of a TRIANGLE, and the program
    will output the HEIGHT, BASE and AREA
    of the TRIANGLE.
    */

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include "assign3header.h"
    
    #define area_tri(b,h) (1.0/2.0)*(b)*(h)
    
    float tri_area(float base, float height);
    void explain_prog(void);
    float get_base(void);
    float get_height(void);
    void display(float b, float h, float a);
    
    main()
    {
        float t_base, t_height, t_area;
    
        system("cls");
    
        explain();
    
        t_base = get_base();
        t_height = get_height();
        t_area = area_tri(t_base,t_height);
        answer = 0.5*base*height;
        display(t_base,t_height,t_area);
    
        puts("Press any key to exit this program...");
    
        system("pause");
    }
    
    void explain_prog(void)
    {
        puts("\nThe user is asked to type in the BASE");
        puts("and HEIGHT of a TRIANGLE, and the program");
        puts("will output the HEIGHT, BASE and AREA");
        puts("of the TRIANGLE");
    }
    
    float get_base(void)
    {
        float base;
    
        printf("\nPlease input BASE of your triangle: >");
        scanf("%f", &base);
    
        return(base);
    }
    
    float get_height(void)
    {
        float height;
    
        printf("\nPlease input HEIGHT of your triangle: >");
        scanf("%f", &height);
    
        return(height);
    }
    
    void display(float b, float h, float a)
    {
        printf("\nFor BASE %.2f and HEIGHT %.2f, the AREA is %.2f", b,h,a);
    }
    
    float tri_area(float base, float height)
    {
        float answer;
    
        answer = 0.5*base*height;
        return(answer);
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do you understand what happens when a function is called? And after it completes?

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Not entirely. My professor isn't a very good teacher. Im completely new at this and really have no idea whats going on here

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Good or poor, professors can only guide you on your journey of programming. A bulk of the work and experience has to come from yourself.

    If you have course material and/or text book, read up about functions. You can also find tutorials on the net (such as here), but nothing beats a good book in my opinion.

    Then do a few sample programs using functions to get the hang of it.

    This assignment is actually pretty easy once you understand what happens when functions are called. Pretty much all the code you need is present.

    I do not wish to type out an entire lesson on functions (which is why I suggest reading up on it from your own resources), but the general idea is - when a function is called, program execution jumps to that function, the code in that function is executed, and then program execution resumes from the point that the function was originally called. You need to know a bit more about functions to do this assignment, but that is the basic idea.

    I really can't give much more help without just giving away the answer. Do some reading, do some practice programs, and give it a shot. Post your attempt at the solution and any questions you have. Or even if you're still unsure, post some specific questions about what you're unclear on and we can give more focused advice.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    First, Your code doesn't even build, as-posted. Do you know why?

    Second, do you know what functions are for? Why they exist?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by raven2313 View Post
    I need to find a away to convert the following code so that everything is done in main(). The program is in C just for clarification.
    As mentioned we can't do your assignment for you but it may help to consider a minimal example with a similar structure. When you don't understand something this kind of "minimalist example" is usually a good way to get some clarity.

    Step 1 is a program that has functions. It looks similar to your example, but all it does is ask for the user's favorite number and then display it back. Each function does just one thing - get_favnum asks for the number and display(favnum) displays it back.

    Code:
    int get_favnum();
    void display(int favnum);
    
    int main(void)
    {
        int n;
        n = get_favnum();
        display(n);
    }
    
    int get_favnum()
    {
        int nn;
        printf("Please enter your favorite number: ");
        scanf("%d", &nn);
        return nn;
    }
    
    void display(int favnum)
    {
        printf("Your favorite number is %d\n", favnum);
    }
    In step 2, we replace each function call with the verbatim code from that function. Go to each line where the function is called and just replace it, including the braces. Notice that I've given each variable a unique name (e.g. nn and n are different) to illustrate what the function call it replaces is conceptually doing.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n;
        {
            int nn;
            printf("Please enter your favorite number: ");
            scanf("%d", &nn);
            n = nn;
        }
        {
            int favnum = n;
            printf("Your favorite number is %d\n", favnum);
        }
    }
    In step 3 we simplify the above. For example, notice how n, nn, and favnum all really just the same thing? Well, let's get rid of that and just call it one thing since it represents only one thing. Next, we can remove the braces because we don't need the limited scope in this instance. That leaves us with this equivalent code:

    Code:
    int main(void)
    {
        int favoriteNumber;
        printf("Please enter your favorite number: ");
        scanf("%d", &favoriteNumber);
        printf("Your favorite number is %d\n", favoriteNumber);
    }
    It's shorter in this case, but we lose the property of divided responsibilities in the first version. Normally it's good practice to separate responsibilities when it makes sense for the application. For example, what if you want to ask another user her favorite number later, and you want to do it in the same way? Without a function, you're going to be copy/pasting code and that's no good.

    Also, notice how in the first program, I didn't do any error checking? Well, it's pretty easy to add some error checking to the version with functions. For example, what if the user doesn't enter a number?? With functions you could add this check to ask_favnum, then the error checking becomes his responsibility and main can just trust him to give him a valid number back.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    20
    Ok i see what your saying. So if the questions asked me to recreate the program so that everything is done in main(), would this be essentially satisfying the requirements?
    Note: I attached the full code i had with the changes.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    #define tri_area (1.0/2.0)*(base)*(height)
    
    /*
    void explain_prog(void);
    float get_base(void);
    float get_height(void);
    float tri_area(float base, float height);
    void display(float b, float h, float a);
    */
    
    main()
    {
        float base, height, area;
    
        system("cls");
    
        puts("\nThe user is asked to type in the BASE");
        puts("and HEIGHT of a TRIANGLE, and the program");
        puts("will output the HEIGHT, BASE, and AREA");
        puts("of the TRIANGLE.");
        puts("");
    
        printf("\nPlease input BASE of your triangle: >");
        scanf("%f", &base);
    
        printf("Please input the HEIGHT of your triangle: >");
        scanf("%f", &height);
    
        area=tri_area;
    
        printf("\nFor base %.2f and Height %.2f, the AREA is %.2f", base,height,area);    
    
        getch();
        return 0;
    }
    
    
    /*
    main ()
    {
        float t_base, t_height, t_area;
    
        system("cls");
    
        explain_prog();
        get_base();
        get_height();
        tri_area();
            
        
        //GET USER INPUT
        t_base=get_base();
        t_height=get_height();
    
        //CALCULATE AREA
        t_area=tri_area(t_base, t_height);
        
    
        //DISPLAY RESULTS
        display(t_base, t_height, t_area);
    
        puts("");
        system("pause");
    }
    
    void explain_prog(void)
    {
        puts("\nThe user is asked to type in the BASE");
        puts("and HEIGHT of a TRIANGLE, and the program");
        puts("will output the HEIGHT, BASE, and AREA");
        puts("of the TRIANGLE.");
        puts("");
    }
    
    float get_base(void)
    {
        float base;
    
        printf("\nPlease input BASE of your triangle: >");
        scanf("%f", &base);
    
        return(base);
    }
    
    float get_height(void)
    {
        float height;
    
        printf("Please input the HEIGHT of your triangle: >");
        scanf("%f", &height);
    
        return(height);
    
    }
    
    float tri_area(float base, float height)
    {
        float answer;
    
        answer=0.5*base*height;
    
        return(answer);
    }
    
    void display(float b, float h, float a)
    {
        printf("\nFor base %.2f and Height %.2f, the AREA is %.2f", b,h,a);
    }
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-04-2011, 11:08 AM
  2. Recreate +operator
    By arcaine01 in forum C++ Programming
    Replies: 12
    Last Post: 08-01-2009, 03:23 AM
  3. To recreate strcpy
    By svaidya in forum C Programming
    Replies: 25
    Last Post: 07-10-2007, 09:27 PM
  4. main() at bottom of program
    By jamie85 in forum C Programming
    Replies: 4
    Last Post: 02-27-2005, 01:57 PM
  5. C program without main()
    By satish_ram in forum C Programming
    Replies: 5
    Last Post: 09-18-2001, 06:24 AM