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...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
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...
Q.1. Write a program that accepts the lengths of three sides of a triangle as inputs....
Q.1. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Q.2. Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the...
(Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether...
(Sides of a Right Triangle) Write a function that reads three nonzero integers and determines whether they are the sides of a right-angled triangle. The function should take three integer arguments and return 1 (true) if the arguments comprise a right-angled triangle, and 0 (false) otherwise. Use this function in a program that inputs a series of sets of integers. Hint: a^2+b^2=C^2 in c programming
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...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
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.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT