Question

Using C++ 1. Create a program that asks the user to create 5 triangles, asking for...

Using C++

1. Create a program that asks the user to create 5 triangles, asking for 5 bases and 5 heights (you can use an array), have a function to print the triangle bases, heights, and areas

2. Create a structure for the Triangle that holds the base and height of the triangles. Ask the user for 5 triangles, and print them.

3. Create an array of 5 structures - ask the user for 5 triangles, and then print the array.

Homework Answers

Answer #1

1.

#include <iostream>
using namespace std;
void print(int base[5], int height[5]) // function to print base, height and area
{
for(int i = 0; i < 5; i++)
{
cout << "Triangle " << i+1 << ":" << endl;
cout << "Base: " << base[i] << endl;
cout << "Height: "<< height[i] << endl;
cout << "Area: "<< (base[i]*height[i])/2 << endl; // area is (1/2)*(base*height)
}
}
int main()
{
int base[5], height[5]; // array of size 5 for base and height
cout << "Enter the base of 5 triangles: ";
for(int i = 0; i < 5; i++)
{
cin >> base[i];
}
cout << "Enter the height of 5 triangles: ";
for(int i = 0; i < 5; i++)
{
cin >> height[i];
}
print(base,height); // function call
return 0;
}

2.

#include <iostream>
using namespace std;
struct Triangle{ // Triangle structure
int base;
int height;
};
void print(struct Triangle triangle) // function to print base and height
{
cout << triangle.base << " " << triangle.height << endl;
}
int main()
{
struct Triangle triangle1, triangle2, triangle3, triangle4, triangle5; // 5 Triangle objects
// Input for 5 triangles
cout << "Enter base and height for first triangle: ";
cin >> triangle1.base >> triangle1.height;
cout << "Enter base and height for second triangle: ";
cin >> triangle2.base >> triangle2.height;
cout << "Enter base and height for third triangle: ";
cin >> triangle3.base >> triangle3.height;
cout << "Enter base and height for fourth triangle: ";
cin >> triangle4.base >> triangle4.height;
cout << "Enter base and height for fifth triangle: ";
cin >> triangle5.base >> triangle5.height;
cout << "Triangle 1 base and height: ";
print(triangle1);
cout << "Triangle 2 base and height: ";
print(triangle2);
cout << "Triangle 3 base and height: ";
print(triangle3);
cout << "Triangle 4 base and height: ";
print(triangle4);
cout << "Triangle 5 base and height: ";
print(triangle5);
return 0;
}

3.

#include <iostream>
using namespace std;
struct Triangle{ // Triangle structure
int base;
int height;
};
void print(struct Triangle obj[5]) // function to print base and height
{
cout << "Base and height of 5 triangles are: " << endl;
for(int i = 0; i < 5; i++)
{
cout << obj[i].base << " " << obj[i].height << endl;
}
}
int main()
{
struct Triangle obj[5]; // array of 5 triangle objects
cout << "Enter base of 5 triangles: ";
for(int i = 0; i < 5; i++)
{
cin >> obj[i].base;
}
cout << "Enter height of 5 triangles: ";
for(int i = 0; i < 5; i++)
{
cin >> obj[i].height;
}
print(obj); // function called
return 0;
}

Please give an upvote if you liked my solution. Thank you :)

Know the answer?
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
Similar Questions
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints...
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints the highest grade. [6 Marks] Question2: Write a Python Program that does the following tasks. [8 Marks] Create an empty list Ask the user for 5 positive numbers and append them to the list Find and print the sum of all the numbers in the list Delete the last element from the list and then print the list Question3: Consider you have the following...
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...
Create a program to ask the user for an integer number, positive only. If the number...
Create a program to ask the user for an integer number, positive only. If the number is between 0 and 255, printout the number in binary format to the screen. This will involve checking the bits, one by one, starting from the most significant place: 128. Use c++ 1. Modify the program to find the binary number, using a FOR loop, instead of the manual checking of each bit separately 2. What are the bitwise XOR and INVERSION operators? Find...
create a basic python program that converts binary to number (using if statements etc) Asking user...
create a basic python program that converts binary to number (using if statements etc) Asking user for input of binary number converting/calculating printing
complete a C++ program that asks the user to make a choice off a menu that...
complete a C++ program that asks the user to make a choice off a menu that will be displayed to them. The user can make as many selections as they wish. The last choice from the menu will give them an option to stop. The menu choices will be the following four calculations: Find the volume of a cone Find the volume of a sphere Find the area of octagon Find the distance between two points Stop For the different...
in c++ Write a C++ program that asks the user to enter an integer number and...
in c++ Write a C++ program that asks the user to enter an integer number and prints it back "vertically" to the screen. Use recursion in your solution. As an example of how the program will work: Please enter an integer: 12345 The integer you entered will print vertically as: 1 2 3 4 5
Create pseudocode to represent the logic of your program that: Asks the user how many Darth...
Create pseudocode to represent the logic of your program that: Asks the user how many Darth Vader Check Print Silk Ties they want to buy. Each Darth Vader Check Print Silk Tie costs the user $64.99. Calculate how much the user will spend. If the user spends over $200.00, they are a big spender. You will set the value of a character variable to indicate whether they are a big spender or not. Display a message stating how much the...
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure...
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure contains the following fields: user name, number of posts, then write a sub to read from the keyboard values into the structures fields and write a function to find and return the average of number of posts for all users and write a sub to print the user name that has zero number of posts.[ number of posts could be from 0 or more]
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT