Segmentation fault in C++ in macOS in code of spirally transversing matrix - c++

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--){

Related

tried everything still not understanding why am i getting runtime error

#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

Why am I not getting any output(linear search)?

#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";
}
}
}

how to fix run time error 'segmentation fault' in c++

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.

'stack smashing detected abc2 terminated Aborted' shows when looping an array

When I try to compile the below code terminal says stack smashing detected abc2 terminated Aborted (core dumped). This error shows when for loop going through the two dimensional array.
What I want to do is get user input to first column using cin>>arr[0][i]; and 0 for every other columns usingarr[i+1][r]=0;.
#include <iostream>
using namespace std;
void displayArray(int arr[][4],int row,int col);
int main(){
int arr[3][4];
for(int i=0;i<4;i++){
cout<<"enter value ";
cin>>arr[0][i];
for(int r=0;r<3;r++){
arr[i+1][r]=0;
}
}
displayArray(arr,3,4);
return 0;
}
void displayArray(int arr[][4],int row,int col){
for(int i=0;i<row;i++){
for(int r=0;r<col;r++){
cout<<arr[i][r]<<" ";
}cout<<endl;
}
}
You are going beyond the bounds of your array here:
int arr[3][4];
for(int i=0;i<4;i++){
//...
arr[i+1][r]=0; // <-- i+1 when i == 2 is going to give trouble
//...
}
If i >= 2, you're writing to arr[3], arr[4], etc. This is a memory overwrite and the behavior then becomes undefined.
Obviously the fix is either to throttle the loop back so that i is always less than 2, or your array's first dimension needs to be increased from 2 to a bigger number.

Random number displayed before my sorting

I am trying to input 10 numbers and then call the function(sorting) to sort them in ascending order. After sorting it, the main program will also call the function getAvg to average the numbers in the array. However, after putting 10 values into the array. There are some random numbers displayed before my output.
What am I doing wrong?
#include<iostream>
#include<stdio.h>
using namespace std;
void sorting(int array[]) {
int t;
int x=0;
for (x=0; x<10; x++)
{
for (int y=0; y<9; y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
}
double getAvg(int array[]) {
double sum=0;
double avg=0;
for (int j=0;j<10;j++){
sum=sum+array[j];
}
avg=sum/10;
return avg;
}
int main()
{
int input=0;
int array[10];
double avg=0;
printf("%s","Enter the 10 temperatures \n");
for(int i=0;i<10;i++){
scanf("%i",&input);
array[i]=input;
}
sorting(array);
avg = getAvg(array);
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
printf("%s %.2lf %s","The average is ",avg, ".");
}
I think the error is in this code:
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
Notice that on the first iteration of the loop, you'll have k=0, so this tries to print out array index -1. This results in undefined behavior - technically speaking, anything can happen - and in your case it's reading garbage data from before the start of the array.
To fix this, change the loop bounds so that they properly range over the array:
for (int k=0; k < 10;k++){
cout<<array[k]<<" ";
}
My guess is that you realized that you were reading too far and tried to fix this by subtracting one from the array index, which just introduced a new bug. Hopefully this corrects this!
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
You are showing from -1 to 9, which means 11 values. The 1st value (array[-1]) was not initialized.
You need to change the k<=10 by k<10 and print array[k] instead of array[k-1]:
for (int k=0;k<10;k++){
cout<<array[k]<<" ";
}
The problem is that you are printing the elements at positions -1 through 9 of your array. There is no position -1.