Code Blocks crashes

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  #include <iostream>
#include <iomanip>
#include <cmath>
using std::setw;
const int n=2;
int area(int a, int b, int c)
{
    int s,p;
    p=(a+b+c)/2;
    s=sqrt(p*(p-a)*(p-b)*(p-c));
    return s;
}
bool exist (int q, int w, int e)
{
    if (!(q+w>e || q+e>w || e+w>q))
    {
        return false;
    }
    return true;
}
int main()
{
    int m,n;
    std::cin>>m>>n;
    int**tr=new int*[m];
    int*s=new int[m];
    for (int i=0; i<m; i++)
    {
        tr[m]=new int[n];
        for (int j=0; j<=2; j++)
        {
            std::cin>>tr[i][j];
        }
    }
      for (int k=0; k<m; k++)
    {
        s[k]=area(tr[k][0], tr[k][1], tr[k][2]);
    }
    for (int l=1; l<m; l++)
    {
        if (s[0]<s[l])
        {
            s[0]=s[l];
        }
    }

    std::cout<<s[0];
    return 0;
}
What is the significance of variable / constant n?
Line 5
 
const int n=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]=new int[n];
should be:
 
        tr[i] = new int[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.
Last edited on
OP: don't forget to delete []
Shouldn't tr[i] = new int[3]; be 2 instead since an array with size 2 has exactly 3 elements? Also, the exist function doesn't perform. Why?
Last edited on
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] = new int[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].
Topic archived. No new replies allowed.