#include <iostream>
using namespace std;
int main() {
int T,X,N;
cin>>T;
for (int i=0;i<T;i++){
cin>>N>>X;
string S;
cin>>S;
int arr[N]={};
arr[0]=X;
for(int i=0;i<(S.length());i++){
if(S[i]=='L'){
arr[i+1]=arr[i]-1;
}
else{
arr[i+1]=arr[i]+1;
}
}
int count=0;
for(int i=1;i<N+1;i++){
for(int j=0;j<i;j++){
if (arr[j]==arr[i]){
count++;
break;
}
}
}
cout<<((N+1)-count);
}
// your code goes here
return 0;
}
Why am i getting a runtime error(SIGSEGV) in in it?
I am not getting any error in vs code for it.
could someone explain plzz
I know the error occurs if the program is trying to take the value of an out of bound array but i have given all the necessary conditions in the program.
EDIT:- I just changed the for loop of T to while loop and it solved it.Could someone explain what just happened.
you have allocated an Array size of N
int arr[N]={};
and in for loop you are looping till N ie i < N+1 or i<=N
for(int i=1;i< N+1 ;i++){
accessing index of arr[N] will throw an error as it goes out of boundry
Related
I am a beginner and cannot understand why my code is giving segmentation fault error please tell my mistake there are no other errors in the code. the error occurred in vs code in macOS device.the given code is for spirally transversing matrix
#include<iostream>
using namespace std;
# define n 4
#define m 4
void spiral(int r, int c,int arr[n][m])
{
int last_row=r-1 , last_col =c-1;
int left=0, right = last_col;
int top=0, bottom = last_row;
while(top<=bottom && left<=right)
{
for (int i=left;i<=right ;i++){
cout<<arr[top][i]<<" ";
}
top++;
for(int i=top; i<=bottom;i++){
cout<<arr[i][right]<<" ";
}
right--;
if(top<=bottom){
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
}
bottom--;
}
if(left<=right){
for(int i=bottom; i>=top;i++){
cout<<arr[i][left]<<" ";
}
left++;
}
}
}
int main(){
/*int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}*/
int arr[n][m]={
{1,2,3,4},
{1,2,3,4},
{5,6,7,8},
{5,6,7,8},
};
spiral(n,m,arr);
return 0;
}
You just need to look carefully at your code. For instance look at this loop, half way through your function
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
When you get here right equals 2, and left equals 0. So i starts at 2, and keeps increasing, it's always bigger than left, so this loop continues 'for ever'. Very quickly i will be too big for the array you are accessing so you get an access violation.
I guess you meant to write this
for(int i=right; i>=left;i--){
but I'm not sure.
Sometimes all that you need to do is look carefully at your code and see that what you actually wrote is different from what you thought you wrote.
BTW you have the same mistake here
for(int i=bottom; i>=top;i++){
perhaps, again I'm guessing, you meant
for(int i=bottom; i>=top;i--){
#include <iostream>
using namespace std;
int main() {
int T,D;
long long int N;
long long int a[N];
long long int b[D];
cin>>T;
for(int i=0;i<T;i++)
{
cin>>N>>D;
for(int i=0;i<N;i++)
{
cin>>a[i];
}
for(int i=0;i<D;i++)
{
b[i]=a[i];
}
for(int i=0;i<(N-D);i++)
{
a[i]=a[i+D];
}
for(int i=0;i<D;i++)
{
a[i+N]=b[i];
}
for(int i=0;i<N;i++)
{
cout<<a[i];
}
cout <<endl;
}
return 0;
}
Why is this coding having segmentation fault? I have seen many solution but cann't get it right.On visual studio or any other application it is not working but on gfg it is working. Please help me solve this problem
There are several things that are wrong.
C-style arrays must be set at compile time. So if you want to use int a[N], N must to known at compile time. If you want a flexible array, one C++ way is to use std::vector.
Then the array a goes from 0 to N-1. So going a[N] is going too far. So a[i+N] is way out if bounds and will be segfault.
you declare array a with N element, but use index out of the array range.
long long int a[N]; // declare here, maximum element is N
for(int i=0;i<D;i++)
{
a[i+N]=b[i]; // use index out of array a
}
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This is code to find if a 2nd string is cipher of the 1st one or not.
To find it I am storing the encryption shift in another variable and then comparing all the corresponding letters of the strings to check if they have equal shift or not.
#include<iostream>
#include<string>
using namespace std;
int main(){
int q;
cin>>q;
int cip[q],flag[q];
string s[q],t[q];
for(int i=0;i<q;i++){
cin>>s[i];
cin>>t[i];
}
for(int i=0;i<q;i++){
cip[i]=(s[i][1]+t[i][1])%26;
}
for(int i=0;i<q;i++){
for(int j=0;j<s[i].size();i++){
if((s[i][j]+t[i][j])%26==cip[i]){
flag[i]=1;
}
else flag[i]=0;;
}
}
for(int i=0;i<q;i++){
if(flag[i]==1){
cout<<cip[i];
}
else cout<<flag[i]-1;
}
}
runtime error: segmentation fault
This block of code is not correct:
for(int i=0;i<q;i++){
for(int j=0;j<s[i].size();i++){
if((s[i][j]+t[i][j])%26==cip[i]){
flag[i]=1;
}
else flag[i]=0;;
}
}
the line
for(int j=0;j<s[i].size();i++)
changing it to j++ should fix the segmentation fault.
What is wrong with my program ?
It works fine on my PC but in IDEone it gives the correct output but shows runtime error. Please help.
#include<bits/stdc++.h>
using namespace std;
struct student
{
int vote;
};
int main()
{
int t;
cin>>t;
while(t--)
{
int count=0;
int n;
cin>>n;
vector <int> a(n);
student s[n];
int k;
cin>>k;
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int temp=0;
for(int i=1;i<=n;i++)
{
if(a[i] != i)
{
temp=a[i];
s[temp].vote++;
}
}
for(int i=1;i<=n;i++)
{
if(s[i].vote==k)
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}
This is the error shown in IDEone :-
Error in `./prog': free(): invalid next size (fast): 0x085cca10
student s[n];
This declares an array called s. It contains n values. The values are s[0] through s[n-1] (you can count them all on your fingers, if you'd like, using a small number of n, such as 5).
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
This attempts to initialize values s[1] through s[n]. The only problem is that s[n] doesn't exist. The last value in the array is s[n-1]. This code will corrupt memory on the stack, resulting in undefined behavior.
The same bug also occurs with the a array.
The index values for both vector and array are zero based and goes up to n - 1. So
for(int i = 0; i != n; ++i)
would be better.
Now you write one element too far, which free finds out later when the data after the memory block is invalid.