Question

Write a C++ program using user defined functions that will read in the lengths of two...

Write a C++ program using user defined functions that will read in the lengths of two side of a right triangle and then calculate the length of the hypotenuse. The program should do this twice.

The program should the following user defined functions:

Function readA will read in a value for side A from the user and return it to main. (it will not receive any input data from main)

Function readB will read in a value for side B from the user and return it to main. (it will not receive any input data from main)

Function calchyp will receive the values of A and B from main, calculate the hypotenuse and return that value to main.

Print the results to the console screen in a table containing all three columns (a, b, and c).

           

Test the program with a=3, b=4 and a=6, b=8.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program:

#include<iostream>

#include<cmath>

using namespace std;

int readA() //function that reads value of a from user

{

int a;

cin>>a;

return a;

}

int readB() //function that reads value of b from user

{

int b;

cin>>b;

return b;

}

int calcHyp(int a,int b) //function to calculate hypotenuse and return the value

{

int c;

c = a*a + b*b; //formula (hypotenuse)2 = a2 + b2

c = sqrt(c); //=>hypotenuse = sqrt(a2 + b2)

return c;

}

int main()

{

int a,b,c,i;

for(i=0;i<2;i++)

{

cout<<"Enter value of side a :";

a = readA();

cout<<"Enter value of side b :";

b = readB();

c = calcHyp(a,b);

cout<<"a b c"<<endl; //printing the values of a, b, c in a tabular format

cout<<a<<" "<<b<<" "<<c<<endl;

}

return 0;

}

output :

shyan@shyam:-$ g++ -Wall -o cppprog cppprog.cpp shyam@shyam./cppprog Enter value of side a :3 Enter value of side b :4 Enter

Add a comment
Know the answer?
Add Answer to:
Write a C++ program using user defined functions that will read in the lengths of two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Write a program that prompts a user to enter the lengths of sides and angles...

    2. Write a program that prompts a user to enter the lengths of sides and angles for the two triangles described below. The program should calculate the length of the side of the triangle indicated below. Print the two answers to 3 decimal places onto the console screen Triangle 1 Read in the value of the angle, alpha, and the length, a, of the side indicated in the picture below. Calculate the length of the hypotenuse, c, of this right...

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays,...

    Develop a system flowchart and then write a menu-driven C++ program that uses user-defined functions arrays, and a random number generator. Upon program execution, the screen will be cleared and the menu shown below will appear at the top of the screen and centered. The menu items are explained below. Help Smallest Quit H or h ( for Help ) option will invoke a function named help() which will display a help screen. The help screen(s) should guide the user...

  • Program: Write a complete C++ program that is made of functions main() and rShift(). The rShift()...

    Program: Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with the first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place...

  • hello please i need help ASAP QUICKLY Write down a complete C++ program that will use...

    hello please i need help ASAP QUICKLY Write down a complete C++ program that will use the following functions related with a triangle; a) a function that will let the user to input the and to return the sizes of the three sides, let say a, b, c. b) a function that will calculate and return the area of the triangle; Area=(1/2)(a+b+c) c) a function that will print the sizes of the triangle and its area on the screen. Then...

  • MATLAB question Triangle Calculations (script with two anon functions) 0 solutions submitted (max: Unlimited) Consider a general triangle with side lengths a, b, and c and angles A, B, and C as shown...

    MATLAB question Triangle Calculations (script with two anon functions) 0 solutions submitted (max: Unlimited) Consider a general triangle with side lengths a, b, and c and angles A, B, and C as shown below Write a script that does the following Define the following two anonymous functions and assign to the indicated Function Handles: 1. The function ThirdSide should accept the lengths of sides a and b as well as angle C in that order and use the Law of...

  • Write a C – program that calls a user-defined function from within main() that determines the...

    Write a C – program that calls a user-defined function from within main() that determines the minimum value from three positive numbers received from the user. Your function should be called minimum.  You must use a loop to receive the numbers. If the user enters a negative number, you must ask the user to re-enter the number. The function should also print out the positive numbers and the smallest number.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT