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...
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
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]
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
C++ project The project will allow you to create a system for tracking the games and...
C++ project The project will allow you to create a system for tracking the games and the scores in terms of graphics, story and replay value and will compute the highest score in each category and the highest overall score. The system will allow you to track 5 games in the following arrays: 1) A string array for the game titles 2) An int array for graphics scores from 1 to 5 3) An int array for replay value from...
Write a program using Matlab that asks the user for their last three grades and final...
Write a program using Matlab that asks the user for their last three grades and final grade and gives them their overall final grade. The three Midterms are worth 70% and Final is worth 30%. The program will display their Final grade with a grade letter corresponding to it. Run your program and verify that it operates correctly (hint: you might need to use “If, else statements”)
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
3. Create a program which allows the user to swap individual words in an input string....
3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove...