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:

#include<iostream>

using namespace std;


int main()

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

    for(int i=0;i<n;i++)
    {
        cin>>column;
      
        for(int j=0;j<column;j++)
        {
            cin>>dummy;
             a[i][j]=dummy;
        }
    }


      for(int l=0;l<q;l++)
      {
          cin>>b;
          cin>>f;
          cout<<a[b][f]<<endl;
      }


}
Posted
Updated 25-Apr-18 23:57pm
v3
Comments
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
 
Comments
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.

[EDIT]
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:
C++
if (column < 0 || column >= 1000)
{
    cout << "Invalid column value";
    return;
}
[/EDIT]
 
Share this answer
 
v2
Comments
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.
C++
cout << "column value:";
cin>>column;
cout << "row value:";
cin>>row;
//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
 
Quote:
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
C++
int a[n][1000];

but the requirement clearly state that a row can be up to 3.10^5 (300000) elements.
 
Share this answer
 
Comments
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