Click here to Skip to main content
15,936,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to solve a hacker rank problem ( link given) -
I have understood the problem and created a program to solve the problem. But the problem occurs when a large amount of data is given as input hence the compiler gives "segmentation fault" i.e a violation fault.It works with small amount inputs without any error.

Kindly help me with this.I am trying to stick with basic language features of C++ and not using pointer or vectors (to be precise) to solve the problem. Kindly have a look at the code.


What I have tried:


using namespace std;

int main()

    int n;
    int q;
    int column;
    int dummy;
    int b;
    int f;
     int a[n][1000];

    for(int i=0;i<n;i++)
        for(int j=0;j<column;j++)

      for(int l=0;l<q;l++)

Updated 25-Apr-18 23:57pm
Richard MacCutchan 26-Apr-18 3:13am    
That code will not even compile, you have not declared variable a. It would also be easier to understand if you used meaningful names for your variables rather than single letters.

If column > 100, your code will access a[i][100], which causes the error. The 100 should not be hard coded, but allocated based on column.
Share this answer
Shubham Kumar 26-Apr-18 3:18am    
How can we dynamically allocate column. Like if I allocate it in first for loop block it will be local to it only??
Your posted code would not even compile because the array a is nowhere defined.

The error is most probably sourced by an out of bound write access to that array. That means that the used indexes (i and or j here) are greater or equal than the corresponding array sizes.

The missing definition for a has been added meanwhile:
int a[n][1000];
The error occurs when column read from user input is greater or equal than the used size of 1000 or negative. Add a check for that value:
if (column < 0 || column >= 1000)
    cout << "Invalid column value";
Share this answer
Shubham Kumar 26-Apr-18 3:29am    
thanks for replying.I need a follow up
actually the input for column can vary it is not in bound
so how can i allocate column dynamically within first for loop and make it global for second block of for loop
Mohibur Rashid 26-Apr-18 3:37am    
how about using a class or struct?
Jochen Arndt 26-Apr-18 3:37am    
You should read the (maximum) number of columns at program start (just after reading the number of rows). Then you can allocate an array.

Do you even have differing number of columns for the rows?
If not: Why do you read the column number instead of just looping (and printing what should be entered like "Enter value for ["<<i<<"]["<<j"]")?
Your code has some error like, the n, but also some logic error like the column input for every row.

Additonal: for allocation at runtime you can use memory allocations.
cout << "column value:";
cout << "row value:";
//I would use a better name like piArray (pointer to int Array)
int *a = new int[column*row];//allocate int array is useable

//at the ende
delete a;
Share this answer
But the problem occurs when a large amount of data is given as input

The problem may be linked to the fact that you hard coded the row length to 1000 elements
int a[n][1000];

but the requirement clearly state that a row can be up to 3.10^5 (300000) elements.
Share this answer
Shubham Kumar 26-Apr-18 6:51am    
I did changed it to 3.10^5 but it's not working . pls try it yourself if u can

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