Mary has three sticks of lengths a, b, and c, and wants to determine whether she
ID: 3854206 • Letter: M
Question
Mary has three sticks of lengths a, b, and c, and wants to determine whether she can use their to form a non-degenerate triangle. For example, the stick lengths a = 3, b = 4, and c = 5 form a valid triangle, but the stick lengths a = 7, b = 2, and c = 5 do not. Complete the function in the editor below. It has the following parameters. The function must return an array on n strings where the value at each index i is Yes if a_y b_y and c_ can form a non-degenerate triangle: otherwise, it's No. The first line contains an integer, n, denoting the number of elements in a. Each line i of the n subsequent lines (where 0 lessthanorequalto iExplanation / Answer
//please rate if my solution helps
#include<iostream>
#include<string>
using namespace std;
int main()
{
//n1,n2,n3 to hold size of arrays a, b, c resply
int n1,n2,n3;
//declare three pointer a,b, c which points to integer
int *a, *b, *c;
cout << "Enter number of elements in a: n = ";
cin >> n1;
//check bondary 1<n<10^5
if (n1 < 1 || n1 > 100000)
{
cout << "n1 is outof range " << endl;
return -1;
}
//now allocate memory for n number of elements for array a
a = new int[n1];
//take elements for array a
for (int i = 0; i < n1; i++)
{
cout << "Enter elements of array a: ";
cin >> a[i];
//boundary check for elements of array
if (a[i] < 1 || a[i] > 1000)
{
cout << "Elements of array outof range,please eneter once again" << endl;
i = i-1;
}
}
cout << "Enter number of elements in b: n = ";
cin >> n2;
//check bondary 1<n<10^5
if (n2 < 1 || n2 > 100000)
{
cout << "n2 is outof range " << endl;
return -1;
}
//now allocate memory for n number of elements for array a
b = new int[n2];
//take elements for array a
for (int i = 0; i < n2; i++)
{
cout << "Enter elements of array b: ";
cin >> b[i];
//boundary check for elements of array
if (b[i] < 1 || b[i] > 1000)
{
cout << "Elements of array outof range,please eneter once again" << endl;
i = i - 1;
}
}
cout << "Enter number of elements in c: n = ";
cin >> n3;
//check bondary 1<n<10^5
if (n3 < 1 || n3 > 100000)
{
cout << "n3 is outof range " << endl;
return -1;
}
//now allocate memory for n number of elements for array a
c = new int[n3];
//take elements for array a
for (int i = 0; i < n3; i++)
{
cout << "Enter elements of array c: ";
cin >>c[i];
//boundary check for elements of array
if (c[i] < 1 || c[i] > 1000)
{
cout << "Elements of array outof range,please eneter once again" << endl;
i = i - 1;
}
}
//triangle is nondegenerate if some of two sides is greater than third side
int n;
if (n1 < n2 && n1 < n3)
{
n = n1;
}
else
{
if (n2 < n3)
{
n = n2;
}
else
n = n3;
}
//declare string to hold result yes or no in string array
string *result;
result = new string[n];
for (int i = 0; i < n; i++)
{
if ((a[i] + b[i]) > c[i])
{
if ((b[i] + c[i]) > a[i])
{
if ((a[i] + c[i]) > b[i])
{
result[i] = "Yes";
}
else
result[i] = "No";
}
else
result[i] = "No";
}
else
{
result[i] = "No";
}
}
//display results
for (int i = 0; i < n; i++)
{
cout << "a = " << a[i] << ", b = " << b[i] << ", c = " << c[i] << " non degenerate = "<<result[i] << endl;
}
}
-----------------------------------------------------------------------------------------------
//output
Enter number of elements in a: n = 2
Enter elements of array a: 7
Enter elements of array a: 5
Enter number of elements in b: n = 2
Enter elements of array b: 10
Enter elements of array b: 8
Enter number of elements in c: n = 3
Enter elements of array c: 5
Enter elements of array c: 3
Enter elements of array c: 3
a = 7, b = 10, c = 5 non degenerate = Yes
a = 5, b = 8, c = 3 non degenerate = No