Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code that i'm supposed to give a 2d array of ints. Then, it should output how many numbers are bigger than all their neighbors, tell me how many "Perfect Squares" are there, and then tell me the biggest one. A Perfect Square is a square that has Perfect numbers in all 4 corners. The problem can be found here in Romanian. I designed the code, but it gives me "Caught Fatal Signal 11" in some cases. I know what it means, but I dont know where is the problem.
This is the code:
<pre lang="C++"><pre>#include <fstream>

using namespace std;
ifstream fin("patrat2.in");
ofstream fout("patrat2.out");

struct star
{
    int in;
    bool st=0;
};

int main()
{

    ///int s[201][201]={NULL};
    ///int ss[201][201]={NULL};
    struct star s[201][201];
    int cont=0,m,n;
    fin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        for(int j=1;j<=m;j++)
        {
            fin>>s[i][j].in;
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int ok=1;
            if(s[i][j].in<=s[i+1][j].in)
                ok=0;
            if(s[i][j].in<=s[i-1][j].in)
                ok=0;
            if(s[i][j].in<=s[i][j+1].in)
                ok=0;
            if(s[i][j].in<=s[i][j-1].in)
                ok=0;
            if(s[i][j].in<=s[i+1][j+1].in)
                ok=0;
            if(s[i][j].in<=s[i-1][j-1].in)
                ok=0;
            if(s[i][j].in<=s[i+1][j-1].in)
                ok=0;
            if(s[i][j].in<=s[i-1][j+1].in)
                ok=0;
            if(ok==1)
            {
                s[i][j].st=1;
                cont++;
            }
        }
    }
    fout<<cont<<endl;
    int k=0,maxx=0,contt=0,ok;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(s[i][j].st==1)
            {
                cont=0;
                ok=0;
                for(int jc=j+1;jc<=m;jc++)
                {
                    cont++;
                    if(s[i][jc].st==1)
                        {
                            if(s[i+cont][j].st==1&&s[i+cont][jc].st==1)
                            {
                                contt++;
                                if(cont>maxx)
                                    maxx=cont+1;
                            }
                        }
                }

            }
        }
    }
    fout<<contt<<endl<<maxx;
    return 0;
}


What I have tried:

I tried to use two arrays instead of the struct, but it's the same thing.
Posted
Updated 12-Jan-22 10:56am
Comments
jeron1 12-Jan-22 13:40pm    
Are you sure you are staying within the acceptable index bounds of your arrays?

The link from raddevus is probably exactly the right explanation (my 5 for it).

A glance at the beginning is enough to accept the suspected error.
C++
struct star s[201][201];
int cont = 0, m, n;
fin >> n >> m;
for (int i = 1; i <= n; i++) {
  for (int j = 1; j <= m; j++) {
    fin >> s[i][j].in;
  }
}

1.Without the content of the file "patrat2.in" nothing happens. What is the content of the file?

2.Obviously a matrix is being filled. In which dimension and which data does the problem arise? I tested small values ​​with no problems ...

3.Arrays start at 0 in C and C ++. Then why do the for loops start at 1?

4.Reading files without recognizing errors doesn't make a lot of sense.

5.If the dimensions of the matrix are known, it would make sense to create the required memory dynamically.
 
Share this answer
 
Comments
Luc Pattyn 12-Jan-22 21:25pm    
3) it is mostly OK the way it is: he needs access to previous and next row/col and did not want to treat edge cells special, so he provided for two extra rows and cols with dummies. However, he forgot to properly initialize them.
I believe the answer is here: c++ - Caught Fatal signal 11 just on a test of an exercise - Stack Overflow[^]

Exceeded max stack size.

The easiest way to discover is put a counter in the loop.
Increase each time through the loop and then output the counter value.
You'll have a good idea when it occurs.
 
Share this answer
 
Comments
merano99 12-Jan-22 16:59pm    
When the Program crashes bevor the loop the static array
struct star s[201][201];
and other local stuff is possibly to much for your Stack.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900