Cant figure out the testcase where I am getting segmentation fault? - c++

I am getting segmentation fault for some unknown test case and I am unable to resolve it.
It runs for most of the cases. I only want to know in which case I am getting segmentation fault.
The code is written for the Question Maximim Rectangular Area in a Histogram. You can check this question here: https://practice.geeksforgeeks.org/problems/maximum-rectangular-area-in-a-histogram-1587115620/1#
Below is the code:
long long getMaxArea(long long arr[], int n)
{
int nsl[n];
int nsr[n];
stack<int>s;
// nsl
for(int i=0;i<n;i++)
{
if(i==0)
{
nsl[i]=-1;
s.push(i);
}
else{
while(!s.empty())
{
if(arr[s.top()]<arr[i])
break;
s.pop();
}
if(s.empty())
nsl[i]=-1;
else
nsl[i]=s.top();
s.push(i);
}
}
stack<int>st;
// nsr
for(int i=n-1;i>=0;i--)
{
if(i==n-1)
{
nsr[i]=n;
st.push(i);
}
else{
while(!st.empty())
{
if(arr[st.top()]<arr[i])
break;
st.pop();
}
if(st.empty())
nsr[i]=n;
else
nsr[i]=st.top();
st.push(i);
}
}
long long ans=0;
for(int i=0;i<n;i++)
ans=max(ans,arr[i]*(nsr[i]-nsl[i]-1));
return ans;
}

The problem got solved by using vector instead of array. I was just using bad c++ syntax for array and hence using vector just solved it.

Related

Program is running but the expected output is not showing [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 last year.
Improve this question
#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class binary_search
{
public:
int a[10],flag;
int n,i,j,index,num,temp,mid,low,high;
void getdata();
void search();
void sort_array();
};
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
void binary_search::getdata()
{
cout<<"number of array "<<"\n";
cin>>n;
cout<<"\nEnter array : "<<"\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort_array();
cout<<"\nSorted Array Elements: ";
for(i=0;i<n;i++)
{
cout<<a[i];
}
}
void binary_search::search()
{
cout<<"\nEnter value to search: ";
cin>>num;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==num)
{
cout<<"\nNumber is found at position "<<mid;
break;
}
else if(a[mid]>num)
{
high=mid-1;
}
else if(a[mid]<num)
{
low=mid +1;
}
else if(a[mid]!=num)
{
flag=false;
}
}
if(!flag)
{
cout<<"\nNumber is not found!!!";
}
}
int main()
{
binary_search b;
b.getdata();
b.search();
getch();
return 0;
}
Program is running but the expected output is not showing.
I'm not getting the message of "not found the number". I think I am missing something if any guidance I will get it will be awesome. Someone refer me to remove conion.h and getch() ; but still the output is not showing as expected.
Actually it's a code for binary search in C++
You must be facing problem in the test cases where, number of elements in array is exactly equal to 10. Because, if you look closely, the sort_array() that you have made have some logical error.
This is your sort_array() function which is using bubble sort technique.
void binary_search::sort_array()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
But, consider the case if n = 10, at this point int the inner loop code will try to access a[10], which is not present. So, it will go out of bound which will lead to undesirable results. So, try changing your function to the one below.
void binary_search::sort_array()
{
for(int i=0;i<n - 1;i++)
{
for(int j=0;j<n - i - 1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
Apart from this remove #include<conio.h> and getch()

i can't get an output from this

#include<iostream>
using namespace std;
class stack
{
int size=10;
int stack[size]={0}, value=0, top;
top=size;
public:
void push(int v)
{
if(top==0)
cout<<"\nstack is full\n";
else
{--top;
stack[top]=v;}
}
void pop()
{
if(top==size)
cout<<"\nstack is empty\n";
else
{top++;
stack[top];
stack[top-1]=0;
}
}
void display()
{
if(top==size)
cout<<"\nstack empty\n";
else
{
for(int i=top;i<size-1;i++)
{
cout<<stack[i];
}
}
}
};
int main()
{
stack s;
char t;
int value,ch;
do
{
cout<<"\n1.push\n";
cout<<"\n2.pop\n";
cout<<"\n3.display\n";
cout<<"enter choice:\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nenter the value to be pushed\n";
cin>>value;
s.push(value);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
default:
cout<<"\nwrong choice\n";
}
cout<<"\ndo u want to retry\n";
cin>>t;
}while(t=='y' || t=='Y');
return 0;
}
Simplest fix to errors occurring is changing int size=10; to static const int size=10;.
After this, apart from occurring warning with stack[top]; being empty statement, there is logical error in display loop in for(int i=top;i<size-1;i++) where it should be either for(int i=top;i<size;i++) or for(int i=top;i<=size-1;i++).
As answered by Tomáš Zahradníček, you need to fix a few things to have your code compile (using -std=c++11).
I used for(int i=top; i<size; ++i) in the display method. I also add that your pop method could simply do top++; without overwriting the stack.
Anyways, regarding your problem of nothing being printed on cout : you obviously tried with 1 item pushed in the stack, but not with 2, which would have pointed to faulty line (the for loop).

Bizzare error in code::blocks

I get an Error in line 1: "Macro names must be identifiers" in this
and I can't fix this nor I can find a solution for this.
I'm not sure why am I getting this error because when I use Dev c++ it's fine, but at the same time the program crashes because arrays are too long, which doesn't happen in Code::blocks with this length for some reason.
#include <iostream>
using namespace std;
int main(){
int n,m,t,l [800] [100],p1[100001],p2[100001],k1[100001],k2[100001],a1[100001],a2[100001],trsa[2],aia[2],swtch0,swtch1,swtch_u,krmbl[2];
cin>>n;
cin>>m;
for(int yi=0;yi<n;yi++){
for(int y=0;y<m;y++){
cin>>l[y] [yi];
}
}
cin>>t;
for(int o=0;o<t;o++){
cin>>p1[o];
cin>>p2[o];
cin>>k1[o];
cin>>k2[o];
cin>>a1[o];
cin>>a2[o];
p1[o]--;
p2[o]--;
k1[o]--;
k2[o]--;
a1[o]--;
a2[o]--;
}
for(int o=0;o<t;o++){
trsa[0]=0;
if(p1[o]>k1[o]){
trsa[0]=p1[o]-k1[o];
}else if(p1[o]<k1[o]){
trsa[0]=p1[o]+k1[o];
}
trsa[1]=0;
if(p2[o]>k2[o]){
trsa[1]=p2[o]-k2[o];
}else if(p2[o]<k2[o]){
trsa[1]=p2[o]+k2[o];
}
for( aia[0]=p1[o];aia[0]<trsa[0];aia[0]++){
krmbl[0]=krmbl[0]+l[aia[0]] [aia[1]];
if(aia[0]==a1[o]){
if(aia[1]==a2[o]){
swtch0=1;
}
}
}
for( aia[1]=p2[o];aia[1]<trsa[1];aia[1]++){
krmbl[1]=krmbl[1]+l[aia[0]] [aia[1]];
if(aia[0]==a1[o]){
if(aia[1]==a2[o]){
swtch0=1;
}
}
}
for( aia[1]=p2[o];aia[1]<trsa[1];aia[1]++){
krmbl[1]=krmbl[1]+l[aia[0]] [aia[1]];
if(aia[0]==a1[o]){
if(aia[1]==a2[o]){
swtch1=1;
}
}
}
for( aia[0]=p1[o];aia[0]<trsa[0];aia[0]++){
krmbl[0]=krmbl[0]+l[aia[0]] [aia[1]];
if(aia[0]==a1[o]){
if(aia[1]==a2[o]){
swtch1=1;
}
}
}
if(krmbl[1]>krmbl[0]){
if(swtch1==1){
cout<<"TAK"<<endl;
}else{
cout<<"NIE"<<endl;
}
}else if(krmbl[1]<krmbl[0]){
if(swtch0==1){
cout<<"TAK"<<endl;
}else{
cout<<"NIE"<<endl;
}
}else{
if(swtch0==1){
cout<<"TAK"<<endl;
}else{
cout<<"NIE"<<endl;
}
}
if(swtch1==1){
cout<<"TAK"<<endl;
}else{
cout<<"NIE"<<endl;
}
}
}
You can make it run in Dev C++ too by declaring all the arrays globally.
Large arrays declared globally (i.e. on heap) is possible,if u declare anything in the main or in any function it goes into the stack which has a smaller size hence your arrays did not work out. Try declaring it globally and it should work.

SPOJ: What is the difference between these two answers for KURUK14

I have solved this problem and got AC. My problem is related to equivalence of following two approaches. The first code got accepted, while the second didn't.
As far as I can discern, both are completely equivalent for all the (valid) test cases any human can think of. Am I wrong? If so, what test case can differentiate them?
Code#1 (Accepted one):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
}
bool f = true;
for(int k=0;k<N;k++)
{
f = f && M[k];
}
return f;
}
int main() {
M=new bool[1002];
int num=0;
scanf("%d",&num);
while(num){
int N=0;
scanf("%d",&N);
if(proc(N))
printf("YES\n");
else
printf("NO\n");
num--;
}
return 0;
}
Code #2 (WA):
#include <cstdio>
bool* M;
bool proc(int N){
for(int j=0;j<=N;j++){
M[j]=false;
}
for(int i=0;i<N;i++){
int a=0;
scanf("%d",&a);
if(a>=N)
return false;
else if(!M[a])
M[a]=true;
else if(!M[N-1-a])
M[N-1-a]=true;
else
return false;
}
return true;
}
int main() {
//Exactly same as code#1
}
The bug has nothing to do with the algorithm itself—it's very possible both the algorithms are correct. But the second implementation is wrong.
When you reach a test case which should return NO, you exit the function prematurely. Which means there are some numbers from the current test case left unread in the input, which of course confuses further reading thoroughly. This means the bug only manifests when T > 1.

Segmentation fault in sorting algorithm

so when I run the following code in Xcode, it works, but gives segmentation fault when I run it in terminal and I'm not sure why.
void Word::arrangeWords(char **&words, int*& pages, int size)
{
char *lowest;
int track;
{
for (int i=0;i<size;i++)
{
lowest=new char[30];
strcpy(lowest, words[i]);;
for(int j=i+1;j<size;j++)
{
int compResult=strcmp(lowest, words[j]);
if (compResult>0)
{
strcpy(lowest, words[j]);
track=j;
}
}
std::swap(words[track],words[i]);
std::swap(pages[track],pages[i]);
delete [] lowest;
}
}
}
Thanks in advance
I can't figure out what's causing the segmentation fault but there is a bug in your code. You are not resetting the value of track after the calls to std::swap. I suggest the following changes to the function.
void Word::arrangeWords(char **&words, int*& pages, int size)
{
for (int i=0;i<size;i++)
{
int track = i;
char lowest[30]; // If you know the size of the array to be 30,
// just create an array. Why use new char[30]?
strcpy(lowest, words[i]);;
for(int j=i+1;j<size;j++)
{
int compResult=strcmp(lowest, words[j]);
if (compResult>0)
{
strcpy(lowest, words[j]);
track=j;
}
}
if ( track > i )
{
std::swap(words[track],words[i]);
std::swap(pages[track],pages[i]);
}
}
}