0
#include<bits/stdc++.h>
using namespace std;
struct Router
{
   float sum;
   int ind;
};
bool comp(Router r1, Router r2)
{
   return r1.sum < r2.sum;
}
int main()
{
   while(1)
   {
       int n;
       cin>>n;

       if(n==0)
           break;

       float matrix[n][n];

       for(int i=0;i<n;i++)
           for(int j=0;j<n;j++)
               cin>>matrix[i][j];

       int newRouters[n];

       for(int i=0;i<n;i++)
           cin>>newRouters[i];

       Router routers[n];

       for(int i=0;i<n;i++)
       {
           routers[i].ind = i;
           routers[i].sum = 0.0;
       }

       for(int i=0;i<n;i++)
       {
           float trafficSum = 0.0;

           for(int j=0;j<n;j++)
               trafficSum += abs(matrix[i][j]);

           routers[i].sum = trafficSum;
       }

       sort(routers, routers+n, comp);

       sort(newRouters, newRouters + n);

       int assign[n];

       for(int i=0;i<n;i++)
           assign[routers[i].ind] = newRouters[i];

       for(int i=0;i<n;i++)
           cout<<(i+1)<<"-"<<assign[i]<<" ";

       cout<<endl;
   }
}

I can't seem to find the issue that is causing Command terminated by signal 11 in my code. Hence i am a newbie with coding so, i can't really find the error in the codes. BTW this is in c++ code. Just trying to learn from mistake right now . I don't have experience using debugger does debugger helps to find the issue on this ?

esportboss
  • 1
  • 1
  • 1
  • 1
    Signal 11 is *segmentation fault* and is a crash due to bad behavior in your program. You should learn how to use a *debugger* to catch such crashes and locate when and where they happen in your code. – Some programmer dude Apr 22 '21 at 02:18
  • With that said, don't use so-called "competition" sites to learn C++. All such sites really teach are really bad habits, which if they stick will make you basically unemployable. Please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over from scratch. Using [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) is one such bad habit, since they don't really exist in C++. – Some programmer dude Apr 22 '21 at 02:19
  • 1
    I would guess that your segfault is caused by a large value of `n` blowing your stack apart. Stop using variable-length arrays. Use `std::vector` or similar. For 2D arrays, you'll need to use your own indexing: _e.g._ `row * n + col` – paddy Apr 22 '21 at 02:29
  • @paddy A vector of vectors is fine for most cases. – Some programmer dude Apr 22 '21 at 02:30

0 Answers0