Question

In: Computer Science

Part 2 Write a C code to create a user defined function called RecArea that will...

Part 2

Write a C code to create a user defined function called RecArea that will calculate the area of a rectangle. Call the function to print the area of the rectangle on the screen.

Note: The user will enter the length and width of a rectangle (decimal values, float) and you

Part 3 (Continue on part 2)

Create another user defined function called TriArea that will calculate the area of a triangle. Print the area of the rectangle on the screen. Area of a triangle is given by:

Note: The user will enter the height and base of the triangle (decimal values or float)

Part 4 (Continue on part 3)

Sum up the two areas found in parts 2 and 3 and display the result to the user.

Solutions

Expert Solution

The code for given problem statement:

#include<stdio.h>
float RecArea(float l,float w)  //  Calculates area of rectangle, prints the area and return it  
{
    float area=l*w;     //  Area of rectangle= length*width
    printf("Area of the given rectangle= %f\n",area);
    return area;
}
float TriArea(float b,float h)  //  Calculates area of triangle, prints the area and return it  
{
    float area=0.5*b*h;     //  Area of triangle= 1/2*base*height
    printf("Area of the given triangle= %f\n",area);
    return area;
}
int main()
{
    float length,width,height,base,R_Area,T_Area,Total;     //  Declaration of float variables 
    printf("Enter value of length and width of rectangle: \n");
    printf("length= ");
    scanf("%f",&length);
    printf("width= ");
    scanf("%f",&width);
    R_Area=RecArea(length,width);   //  Calls the RecArea function

    printf("Enter value of base and height of triangle: \n");
    printf("base= ");
    scanf("%f",&base);
    printf("height= ");
    scanf("%f",&height);
    T_Area=TriArea(base,height);    //  Calls the TriArea function

    Total=R_Area+T_Area;    //  Adding area of rectangle and triangle
    printf("Total area is= %f\n",Total);
    return 0;
}

The screenshot of the code:

Test running of the program:

Logic of the code:

While taking the values into variables the format specifier used is %f for float values. Once user inputs values of length and width, those values are passed to funcion RecArea to calculate return and print the area. Area is returned to the calling function so that it can be used to find total of areas.

Similar process is done for TriArea using base and height.

Variable Total is used to store addition of area of rectangle and triangle. Which is printed at the end.

The formulas used:

Area of rectangle= length x width

Area of triangle=(1/2) x base x height

Float variabes used:

length To store length of rectangle
width To store width of rectangle
base To store base of triangle
height To store height of triangle
R_Area To store area of rectangle
T_Area To store area of triangle
Total To store sum of area of rectangle and area of triangle

   


Related Solutions

Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function...
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function that will receive the word, use the word to compile a new word in Robbery language and return the new Robbery language word. In the function: Use a while loop and a newline character (‘\0’) to step through the characters in the original string. How to compile a word in Robbery language: Double each consonant and put the letter ‘o’ between the two consonants....
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a...
Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a signed number. Input must be passed by reference to the function and it will return the answer. There is no need to call this function anywhere. You can use following operators only: <<>> ~ & ^ | Decimal
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
Written in C code: Using a structure called person, write a C function called compare_age that...
Written in C code: Using a structure called person, write a C function called compare_age that takes two person structure pointers as input and returns 1 if the first person is older, -1 if the second person is older, or 0 if they have exactly the same birthday. Hint: Compare the years first, then if equal compare months, if they are equal compare days. The structure called person should contain the following; has fields for name (50 char array), dateOfBirth...
Write a user-defined function named printBox that, when called, will output the following sequence of characters:...
Write a user-defined function named printBox that, when called, will output the following sequence of characters: OOOOOOOO OOOOOOOO OO            OO OO            OO OOOOOOOO OOOOOOOO Write a user-defined function named printArrow that, when called, will accept an integer called arrowSize and will output a sequence of *’s such that there are arrowSize number of rows with the number of * incrementing, and then arrowSize-1 rows with the number of * decrementing: If arrowSize = 3, then the output is: * **...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
Write C code that contains a function called triangle_generator that outputs a triangle wave at a...
Write C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi...
Part 2 Write a C code to create 5 random numbers between 90 and 100 (included)...
Part 2 Write a C code to create 5 random numbers between 90 and 100 (included) and print the values on the screen. Part 3 Create a function that will mimic throwing the dice twice. The function will do the following when you call it. The function will generate 2 random numbers between 1 and 6 The function will display the two random numbers to the user The function will display “Winner” if the sum of the two numbers are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT