Bizzare error in code::blocks - c++

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.

Related

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

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.

My c++ program is compiling fine, but not running . My program is running and stopping for a while. No error is showing

My c++ program is compiling with no error. I am running my program on vscode . In the same file, when i run this code.
My system:
Windows 10
visual studio code
c++ 14
#include<bits/stdc++.h>
using namespace std;
int main(){
count<<"Hello";
return 0;
}
It is running fine.
When in the same file, i put this code,
#include<bits/stdc++.h>
using namespace std;
int removeAlt(string s);
int removeTwo(string s,char a,char b);
int alternate(string s);
int main(){
cout<<alternate("abaacdabd");
return 0;
}
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
}
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
int removeTwo(string str,char a,char b){
for(int i=0;i<str.length();){
char ch=str[i];
if(str[i]==a||str[i]==b){
str.erase(i,1);
} else i++;
}
cout<<"removing "<<a<<" "<<b<<",, "<<str<<endl;
int res=removeAlt(str);
return res;
}
// Complete the alternate function below.
int alternate(string s) {
set<char>st;
// get different character of set
for(auto x:s)
st.insert(x);
int len=st.size(); // length of set
char setAr[len];
auto it=st.begin();
for(int i=0;i<len;i++){
setAr[i]=*it;
it++;
}
cout<<"Removing\n";
int up=len;
for(int i=0;i<up-1;i++){
for(int j=i+1;j<=up;j++){
cout<<"SetAr "<<i<<" "<<j<<endl;
len=max(0,removeTwo(s,setAr[i],setAr[j]));
}
}
return len;
}
It runs some statements , after that terminal pauses forever.
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
}
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
You never increment i and so this function will run forever unless it erases everything.
int removeAlt(string s){
// remove consicutive characters
for(int i=0;i<s.size();){
if(s[i]==s[i+1]){
s.erase(i,1);
} else i++;
}
cout<<"Remove consitutive "<<s<<endl;
return s.size();
}
Increment i if next element is not same as current.

Vector error,a very confusing segmentation error?

So basically,I am doing a code which searches for an element of a vector inside a vector.While I thought of the approach , implementing it got me a segmentation error. I narrowed down the problem
In the code if I decomment the line in the for loop while commenting the above then all elements of B[i] are being displayed.Why then is a segmentation error being thrown. I think the binary_return is more or less correct and if I replace the line with
binary_return(A,0,A.size(),B[1])
then its working.
Here is the code:
#include<iostream>
#include<vector>
using namespace std;
int binary_return(vector<int> a,int start,int end,int seek)
{
int mid = (start+end)/2;
//cout<<start<<" "<<seek<<" "<<mid;
if(end!=start)
{
if(a[mid]==seek)
{
return mid;
}
else if(a[mid]>seek)
{
return binary_return(a,start,mid,seek);
}
else if(a[mid]<seek)
{
return binary_return(a,mid,end,seek);
}
}
else
return -1;
}
int main()
{
vector<int> A{1,3,6,9,23};
vector<int> B{1,4,23};
cout<<B[0]<<B[1]<<B[2];
for(int i=0;i<B.size();i++)
{
cout<<binary_return(A,0,A.size(),B[i]);
//cout<<binary_return(A,0,A.size(),B[0]);
}
return 1;
}
Your code is not handling the last case correctly and ends up in infinite recursion.
This unfortunately in C++ means that anything can happen (you're not guaranteed to get a meaningful error).
Add a debug print at the beginning of the function and you'll see in which cases you're entering infinite recursion.
You have infinite recursion in third if statment
The correct code if the following:
#include<iostream>
#include<vector>
using namespace std;
int binary_return(vector<int> a,int start,int end,int seek)
{
int mid = (start+end)/2;
//cout<<start<<" "<<seek<<" "<<mid;
if(end!=start)
{
if(a[mid]==seek)
{
return mid;
}
else if(a[mid]>seek)
{
return binary_return(a,start,mid,seek);
}
else if(a[mid]<seek)
{
// In your sample you forgot to add +1 (mid+1) for next start
return binary_return(a,mid+1,end,seek);
}
}
else
return -1;
}
int main()
{
vector<int> A{1,3,6,9,23};
vector<int> B{1,4,23};
for(int i=0;i<B.size();i++)
{
cout<<binary_return(A,0,A.size(),B[i]);
}
return 0;
}

Trying C++ on Xcode on Mac- ERROR-exc_bad_access (code=1 address=0x100500000)

I have been trying to write a program where I have to read a binary file which has around 30 million long values and we have to find out the modes I have successfully done it using a smaller number of values but when I try with the file it always gives me the error- exc_bad_access (code=1 address=0x100500000), I am trying it in Xcode on my MAC and implementing in C++. The code is as below, can anyone help me out?
#include <iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
void mode(long data[], long size, long &num_modes, long modeNums[], long &maxFrequency);
int main()
{
long *data,*modeNums, size;
ifstream binaryin("TestData.bin", ios::binary);
binaryin.read(reinterpret_cast<char *>(&size), 4);
data = new(nothrow)long[size];
if(!data)
{ cout<<"Memory allocation error for data array, program will terminate\n";
system("pause");
exit(0); }
binaryin.read(reinterpret_cast<char *>(data),size*sizeof(long));
//cout<<"SIZE "<<size<<endl;
long num_modes;long maxFrequency;
modeNums=new(nothrow)long[size];
size=3000000;
mode(data,size,num_modes,modeNums,maxFrequency);
/* for(int i=0;i<20;i++)
cout<<"data[i]"<<data[i]<<endl;*/
// cout<<"maxFrequency= "<<maxFrequency;
// cout<<"\nnum_modes= "<<num_modes<<endl;
// for(int i=0;i<num_modes;i++)
// cout<<"modeNums[]= "<<modeNums[i]<<endl;
}
void mode(long data[], long size, long &num_modes, long modeNums[], long &maxFrequency)
{
// size=300000;
// cout<<"SIZE "<<size<<endl;
int cnt=0,val=0;// long *arr,*arr1;
vector<long> arr;
vector<long> arr1;
//arr=new(nothrow)long[size/2];
//arr1=new(nothrow)long[size/2];
// cout<<"INSIDE THE FUNCTION\n";
//cout<<"SIZE "<<size<<endl;
long k;
//cout<<"SIZE "<<size<<endl;
cout<<"\nstarting for loop\n";
for(int i=0;i<size;i++)
{ cout<<"Inside for loop\n";
cnt=0; k=(size-1);
long num=data[i];
int flag=0;
int temp=val;
while(temp!=0)
{ cout<<"inside while loop\n";
if(arr[temp]==num)
{ flag=1;}
temp--;
}
if(flag==0){
int count=0;
for(int j=0;j<(size/2);j++)
{// cout<<"Inside for which is inside while\n";
cout<<"data[j] "<<data[j]<<" "<<"data[k] "<<data[k]<<endl;
cout<<"COUNT "<<count++<<endl;
if(num==data[j] && num==data[k])
{ cnt+=2; }
else if(num==data[j]||num==data[k])
{ cnt++; }
k--;
}
arr.push_back(num);arr1.push_back(cnt);
val++;
}
else
flag=2;
}
cout<<"\nend of for loop\n";
// for(int i=0;i<val;i++)
// cout<<"NUM "<<arr[val]<<"Mode "<<arr1[val]<<endl;
maxFrequency=0;
for(int i=0;i<val;i++)
{
if(arr1[i]>maxFrequency)
maxFrequency=arr1[i];
}
num_modes=0; int value=0;
for(int i=0;i<val;i++)
{
if(arr1[i]==maxFrequency)
{ num_modes++;
modeNums[value]=arr[i];
value++; }
}
/* int value=0;
for(int i=0;i<val;i++)
{
if(maxFrequency==arr1[i])
{ modeNums[value]=arr[i];
value++;}
} */
// for(int i=0;i<val;i++)
// cout<<"Value "<<arr[i]<<" Mode "<<arr1[i]<<endl;
// cout<<"maxFrequency= "<<maxFrequency<<endl;
// cout<<"\nnum_modes= "<<num_modes<<endl;
cout<<"\nend of function\n";
}
enter image description here
This line:
size=3000000;
means that you allocate your arrays of a certain size (whatever you read from the file), then tell your mode function that those arrays are actually 3000000 longs in size. This could lead to a buffer overrun.
I can't see any reason for this line to be in there.
Also if you use new(nothrow) you should check the return value.

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.