Question

Write a basic c++ program to check whether a triangle is valid or not if the...

Write a basic c++ program to check whether a triangle is valid or not if the three sides are given:
A triangle is valid if the sum of its two sides is greater than the third side.
Let’s say that a, b, c is the sides of the triangle. A valid triangle must satisfy all these conditions:
a + b > c
a + c > b
b + c > a

(2 points) Generate random numbers 1-15 inclusive for each of a, b and c.
Display if the three sides do not make a valid triangle.

(2 points) If the three sides do make a valid triangle, determine whether the triangle is equilateral, scalene or isosceles:
- Equilateral triangle is a triangle in which all three sides are equal.
- Isosceles triangle is a triangle that has two sides of equal length.
- Scalene triangle is a triangle that has three unequal sides.

(2 points) Loop 10 times, each time generate three random sides 1-15 inclusive
Print whether the numbers make an invalid, an equilateral, isosceles or scalene triangle.

(2 points) Print the sum of all a, all b and all c and the resulting triangle.
Print how many of each at the end.

(2 points) Your name, correct indentation, and no redundant (unnecessary) code.

14   2 12 not a valid triangle
11   1   9 not a valid triangle
8   1 10 not a valid triangle
4   5   9 not a valid triangle
14 13   6 is scalene
11 10   6 is scalene
10   8   2 not a valid triangle
7   7   7 is equilateral
1 11 13 not a valid triangle
14   5 14 is isosceles

Total a = 94, b = 63, c = 88
94 63 88 is scalene
6 not valid
1 equilateral
1 isosceles
3 scalene

Homework Answers

Answer #1

The Code is well explained in comments itself

#include <iostream>
#include <stdlib.h>   
//importing necessary libraries 
using namespace std;

int equi=0,scal=0,nottrain=0,isoc=0;//global varaible for counting traigle types
string traingleType(int a,int b,int c) //function which returns traingle type
{
    if(a==b && b==c && a==c) {equi++; return " is equilateral";} //condition for equilateral traingle 
    else if(a==b || b==c || a==c) {isoc++;return " is isosceles ";} //condition for isoceles traingle
     else{scal++; return " is scalene ";} //condition for scalene traingle
}
int main()
{
    int A=0,B=0,C=0,a,b,c; // declaring varaibles for total sum sides and separate sides
    for (int i=0;i<10;i++)
    {
        a=rand() % 15 + 1; // random number for side 1 between 1 to 15 inclusive
        b=rand() % 15 + 1; // random number for side 2 between 1 to 15 inclusive
        c=rand() % 15 + 1; // random number for side 2 between 1 to 15 inclusive
        A=A+a;B=B+b;C=C+c; //for sum of all sides
        if(a+b>c && b+c>a && a+c>b) // condition for valid traingle
        {
          cout<<a<<" "<<b<<" "<<c<<traingleType(a,b,c)<<"\n"; // printint traingle type
        }
        else{
            nottrain++; //updating not valid traingle count
            cout<<a<<" "<<b<<" "<<c<<" not a valid triangle"<<"\n";
        }
    }
    cout<<"\nTotal a = "<<A<<" b = "<<B<<" c = "<<c<<"\n";
    //printing sum of all sides
    if(A+B>C && B+C>A && A+C>B) // condition for valid traingle
        {
          cout<<A<<" "<<B<<" "<<C<<traingleType(a,b,c)<<"\n";
        }
        else{
            cout<<A<<" "<<B<<" "<<C<<" not a valid triangle"<<"\n";
        }
   cout <<nottrain<<" not valid"<<"\n";
   cout <<equi<<" equilateral"<<"\n";
   cout <<isoc<<" isoceles"<<"\n";
   cout <<scal<<" scalene"<<"\n";
   //printing respective counts for traingle types
    return 0;
}

In case of any doubts/queries or if you want something else/improvement in the answer please write them in the comments.
Also, provide your valuable feedback.
Thanks

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
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea,...
Create a class named MyTriangle that contains the following three methods: public static boolean isValid(double sidea, double sideb, double sidec) public static double area(double sidea, double sideb, double sidec) public static String triangletType(double a, double b, double c) The isValid method returns true if the sum of the two shorter sides is greater than the longest side. The lengths of the 3 sides of the triangle are sent to this method but you may NOT assume that they are sent...
Negate the following statements. In each case, determine which statement is true, the original statement or...
Negate the following statements. In each case, determine which statement is true, the original statement or its negation. a) An angle inscribed in a semicircle is a right angle. b) Every triangle has at least three sides. c) Every rectangle is a square. d) There are exactly three points on every line. e) Through any two distinct points there is at least one line. f) Every rectangle has three sides, and all right triangles are equilateral. g) Triangle XYZ is...
Three charges, each of charge 5.0 ""C are located at the vertices of an equilateral triangle...
Three charges, each of charge 5.0 ""C are located at the vertices of an equilateral triangle whose sides are 10 cm in length. a) What is the total force, magnitude and direction, on charge I? b) What is-the total potential energy of the three charges? c) Charges 2 and 3 remain fixed, but charge one is now allowed to move. If it starts from rest, what i.s its velocity when it is very far away from charges 2 and 3?...
Extension/ Intension Name three things connoted by the following: 1. country 2. game 3. planet 4....
Extension/ Intension Name three things connoted by the following: 1. country 2. game 3. planet 4. city 5. mammal Name three things denoted by each of the following: 6. movie 7. U.S. Senator 8. Philosopher 9. Nobel Prize winner 10. jazz musician 11. Put the following in order of increasing intension shrub, Portland rose, plant, perennial, rose 12. Put the following in order of increasing extension: polygon, equilateral triangle, isosceles triangle, convex polygon, triangle 13. Put the following in order...
1) How many permutations of the letters a, b, c, d, e do not contain the...
1) How many permutations of the letters a, b, c, d, e do not contain the string bde. 2) Of any five points chosen within an equilateral triangle whose sides have length 1, show that at least two of them are within a distance of 1/2 of each other.
C++ Write a program that calculates and prints the total grade for n assignments as a...
C++ Write a program that calculates and prints the total grade for n assignments as a percentage. Prompt the user to enter the value of n, followed by the number of points received and number of points possible for each assignment . Calculate and print the total number of points received, total number of points possible, and the overall percentage: (total points received / total points possible) * 100. Output: Enter·number·of·assignments·to·input:3↵ Enter·number·of·points·received·for·assignment·1 :10↵ Enter·number·of·possible·points·for·assignment·1 :10↵ Enter·number·of·points·received·for·assignment·2 :7↵ Enter·number·of·possible·points·for·assignment·2 :12↵ Enter·number·of·points·received·for·assignment·3...
5. Suppose that the incenter I of ABC is on the triangle’s Euler line. Show that...
5. Suppose that the incenter I of ABC is on the triangle’s Euler line. Show that the triangle is isosceles. 6. Suppose that three circles of equal radius pass through a common point P, and denote by A, B, and C the three other points where some two of these circles cross. Show that the unique circle passing through A, B, and C has the same radius as the original three circles. 7. Suppose A, B, and C are distinct...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc