Hey, I've written a program for my uni and whenever I run it, input couple numbers the program crashes. The idea of the following program is to have 2 functions one of which calculates the are of some triangle by 3 sides, other bool that checks if such triangle exists. In the main function I declare and initialize an 2d dynamic array, the second dimension of which should be constant (3 since a triangle has 3 sides) other of which shows how many triangles I'll deal with. The second loop calculates the area of each triangle and initializes it with some element of the s array. the 3rd loop checks which one of the elements in the s array is biggest, after which I output it and terminate the program. Any advice would be in great help, thank you in advance!
What is the significance of variable / constant n?
Line 5
constintn=2;
Line 23, 24
1 2
int m,n;
std::cin>>m>>n;
Assuming a triangle must have three sides (which is consistent with the intention of the rest of the program)
Line 29
tr[m]=newint[n];
should be:
tr[i] = newint[3];
While we're considering the code, I'd recommend variable p (line 9) should be type double, not int. Also use 2.0 instead of 2 on that line, to ensure floating-point rather than integer division is performed.
Arguably variable s and the return value of the area() function should also be type double as well. Which would imply that the array s (line 26) should also be type double.
be 2 instead, since an array with size 2 has exactly 3 elements?
No.
An array with size 2 has exactly 2 elements.
int arr[2]; // define array of size 2.
The subscripts start from zero, so the first element is arr[0] and the second is arr[1].
If you try to access arr[2], that would be the third element, which doesn't exist, and can give rise to undefined behaviour (for example corruption of other data, program crash ... anything at all).
the exist function doesn't perform
The function isn't called from anywhere. There may also be logic error(s).
Shouldn't tr[i] = newint[3]; be 2 instead since an array with size 2 has exactly 3 elements?
An array with size 2 has exactly 2 elements, with valid indexes in the range [0,1].
An array with size 3 has exactly 3 elements, with valid indexes in the range [0,2].