SIGSEGV error details and removal steps [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
for the question: http://www.codechef.com/FEB14/problems/TWODOGS
i wrote the following program but on the inline judge codechef i am getting sigsegv error. I want to know specifically which instruction is creating this error in my program so that i try and remove that.
here is the code:
#include<iostream>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int arr[n+1];
for(int i=1;i<n+1;i++)
arr[i]=n+1;
for(int i=0;i<n;i++)
{
int p=min(i+1,n-i);
arr[a[i]]=min(arr[a[i]],p);
}
int time=n+1;
for(int i=0;i<n;i++)
{
int p=a[i];
if(p!=k-p&&p<k)
time=min(time,max(arr[p],arr[k-p]));
}
if(time==n+1)
cout<<"-1";
else
cout<<time;
}

Given sample input 2:
Sample Input 2:
5 5
2 4 9 2 5
You cannot do this
arr[a[i]]=min(arr[a[i]],p);
because when i == 2, arr[a[i]] will be evaluated as arr[9], which is out of bounds and results modification of memory which you do not own, that is, undefined behavior. This is where your program is failing.

You'd have an easier time if your identifiers were more meaningful. That said:
cin>>a[i];
....
arr[a[i]]=min(arr[a[i]],p);
You are indexing arr[] by user input. If you are getting a segfault, it's best to put in asserts for these situations in case the assumption is invalid, something like:
assert(a[i] < n-1);
Next:
time=min(time,max(arr[p],arr[k-p]));
again, indexing array based on user input. Add another assert to test that arr[p] and arr[k-p] are valid.
Finally, for this line:
if(p!=k-p&&p<k)
Did you mean:
if(p!=k-p&&p<n)
a is allocated based on n, not k.

If you read the question carefully ,
the element in a[i] <= 10 ^ 6 .
You have declared an array arr of size n+1 .
Constraint on n : n <= 500000
Also you are trying to access arr[ a[i] ] which may go beyond the limit of array arr.
a[i] may be greater than 5 * 10^5 .
This is leading to segmentation fault error.

Related

Want to see if a one digit of a 2 digit number is divisible by the other digit [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
The problem I am having is how to say if(//code=Whole) I dont know how to tell the program if the number equals whole number because if its a number like 39 9 divided 3 equals a whole number if a number is not divisible it will go to the decimals which is how I am willing to solve the problem by saying if digit 1 or 2 divisible by the other digit is whole then cout<<Divisible
I tried searching up didnt work heres my code
#include <iostream>
using namespace std;
int main()
{
int x,z,n;
cin>>x;
x%10=z;
x/10=n;
if(z/n==int)
{
cout<<"YES";
}
else if(n/z==int)
{
cout<<"YES"
}
else
{
cout<<"NO";
}
}
I dont know how to say if it equals whole So i dont know what to put there plus there might be something wrong in the program other than that.
This is your code.
int main()
{
int x,z,n;
cin>>x;
x%10=z;
x/10=n;
if(z/n==int)
{
cout<<"YES";
}
else if(n/z==int)
{
cout<<"YES"
}
else
{
cout<<"NO";
}
}
A few things. First, please use whitespace to make your code readable. It's going to safe you from a wide variety of bugs over the years.
Next, this is bad code:
x%10=z;
x/10=n;
So is this:
if(z/n==int)
The first few lines of your main method should be like this:
int x;
cin >> x;
int z = x % 10;
int n = x / 10;
Note that I've done two things. First, I moved where the variables are declared to as late as possible so that they aren't hanging around, uninitialized for a while. This is just good practice. Define them and assign them at the same time.
Next, the way you wrote that code doesn't make sense. An assignment statement can't be written backwards the way you did it.
In your head, you could say, "z becomes x % 10" if you have to.
This line won't compile:
if(z/n==int)
If you want to see if z divides evenly by n:
if ( z % n == 0 )
You have the same problem in the else-if a few lines later.

Run time error: Determinant of matrix using c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'am trying to write a program which can calculate determinant of 2x2 matrix entered by user.
My code is as follows -
//Program to calculate determinant of matrices
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[1][1];
int i,j,p,q,v,b,c,k,determinant;
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
p=i;
q=j;
cout<<"Enter element :- ";
cin>>arr[p][q];
cout<<"You are in "<<i<<j<<endl;
}
}
v=arr[1][1];
b=arr[1][2];
c=arr[2][1];
k=arr[2][2];
cout<<"Entered elements are = "<<v<<" "<<b<<" "<<" "<<c<<" "<<k<<endl;
determinant= ((v*k)-(c*b));
cout<<"Determinant of given matrix is = "<<determinant;
getch();
}
And this is the error I'am facing with this program -
Enter element :- 4
You are in 11
Enter element :- 9
You are in 12
Enter element :- 8
You are in 21
Enter element :- 7
You are in 22
Entered elements are = 4 8 8 7
Determinant of given matrix is = -36
At the position arr[1][2] I have entered '9' as the element, but instead it is printing the element assigned to position arr[2][1].
So, if anybody can help me resolve this error then I will be very much grateful to you.
The int arr[1][1] does not declare an matrix of two elements, it declares a 1x1 matrix.
Declare it instead as
int arr[2][2];
That being said, indices in C++ start at 0 so
v=arr[1][1];
b=arr[1][2];
c=arr[2][1];
k=arr[2][2];
should be
v=arr[0][0];
b=arr[0][1];
c=arr[1][0];
k=arr[1][1];
same goes for your for-loops
Array starts from index 0. The size of array should be a[2][2] since it is a 2x2 matrix.
Modify the loops accordingly for the array elements.
#include<iostream>
using namespace std;
int main()
{
int arr[2][2];
int i,j,p,q,v,b,c,k,determinant;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
p=i;
q=j;
cout<<"Enter element :- ";
cin>>arr[p][q];
cout<<"You are in "<<i+1<<j+1<<endl;
}
}
v=arr[0][0];
b=arr[0][1];
c=arr[1][0];
k=arr[1][1];
cout<<"Entered elements are = "<<v<<" "<<b<<" "<<" "<<c<<" "<<k<<endl;
determinant= ((v*k)-(c*b));
cout<<"Determinant of given matrix is = "<<determinant;
}

Absolute difference of sum of two diagonals of a 2d array in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to obtain the absolute difference of the sum of left and right diagonal of the given 2d array.
I have written the following function-
int diagonalDifference(vector<vector<int>> arr) {
int n=arr.size();
int summ1=0,summ2=0,result=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{
if(i==j)
{ summ1=summ1+arr[i][j];}
else if((i+j)==(n-1))
{ summ2=summ2+arr[i][j];}
}
}
result=abs(summ1-summ2);
return result;
}
input array
11 2 4
4 5 6
10 8 -12
Explanation- summ1=11+5+(-12)
summ2=4++5+10 result=|4-19|=|-15|=15
expected output: 15
The output I am getting is 10
Here is a solution in O(n) complexity. reference
for (int i = 0; i < n; i++)
{
summ1 += arr[i][i];
summ2 += arr[i][n-i-1];
}
The else-if should be an if by itself.
The else-if prevents the 5 from being added to summ2. Which means the final calculation is: (11+5-12)-(4+10) = -10. Whose absolute value is 10.

Reducing Run Time C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.

C++ increment operator and sum [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hey I have a problem,
The question is as follows in my book (not homework lol):
Give the value of the variables and continue with the recent gotten value:
int i,j,k;
i=j=2;
k=3;
Expression
1) i = ++j + ++k
Result after expression:
int i=3 (or 7?);
int j= 3;
int k=4;
2) (following the values after first exercise)-> i = ++j + k++
Result after expression:
int i=3 (or also 8, since k is +1 after the expression?)
int j= 4;
int k=5;
3) (following the values from #2) -> i = j++ + ++k
Result after expression
int i=3 (or also 10? since j is +1 after the expression, so we take value from #2 ?)
int j=5;
int k=6;
I am very confused since I can not check it on my code editor program, it could be easier if i was named: int answer; i guess but how do I check if what i got for int i is true?
Hopefully someone can help me!
Cheers
I identified
how do I check if what i got for int i is true?
as being your main question?
You check it by writing a short C program that calculates the values (which is simple copy-pasting) and prints them on the console, i.e. with
printf("After assignment: i=%i j=%i k=%i\n", i, j, k);
(don't forget to #include <stdio>)
Then you simply compile and run it and check its console output.