I am getting runtime error (SIGSEGV) error when this code is submitted on SPOJ. I cleared the vectors and initialized all variables but still getting the error. What is wrong with this code?
#include<iostream>
#include<vector>
using namespace std;
vector<int> even_greater_increment(vector<int> num,int mid1,int mid2){
int bigindex=0,smallindex=0;
if(num[mid1]==num[mid2]){
return num;
}
if(num[mid1]>num[mid2]){
bigindex=mid1;
smallindex=mid2;
}
else{
bigindex=mid2;
smallindex=mid1;
}
while(num[smallindex]!=num[bigindex]){
num[smallindex]++;
}
return num;
}
bool check9(vector<int> num,int vlen){
for(int i=0;i<vlen;i++){
if(num[i]==9){
continue;
}
else{
return false;
}
}
return true;
}
int nine(vector<int> num,int vlen){
vlen=vlen+1;
num[0]=1;
cout<<num[0];
for(int i=1;i<vlen-1;i++){
num[i]=0;
cout<<num[i];
}
num[vlen-1]=1;
cout<<num[vlen-1];
return 0;
}
vector<int> increment9(vector<int> num,int mid1,int mid2,int vlen){
int l=mid1,k=mid2;
do{
l--;k++;
}while((l>=0&&k<=(vlen-1))&&(num[l]==9&&num[k]==9));
num[l]++;
num[k]++;
for(int i=l+1;i<k;i++){
num[i]=0;
}
return num;
}
vector<int> incrementmiddle(vector<int> num,int mid1,int mid2,int vlen){
if(num[mid1]==9&&num[mid2]==9){
num=increment9(num,mid1,mid2,vlen);
return num;
}
if(mid1==mid2){//odd implementation
num[mid1]=num[mid1]+1;
}
else{//even implementation
num=even_greater_increment(num,mid1,mid2);
}
return num;
}
int println(vector<int> r,int vlen){
for(int p=0;p<vlen;p++){
cout<<r[p];
}
}
bool comp(const vector<int>& v1, const vector<int>& v2) {
if (v1.size() != v2.size())
return v1.size() < v2.size();
for (int i = 0; i < v1.size(); i++)
if (v1[i] != v2[i])
return v1[i] < v2[i];
return false;
}
int palindrome(){
int length=0;
char ch[7];
cin>>ch;
for(int i=0;ch[i]!='\0';i++){
length++;
}
vector<int> num;
vector<int> temp;
for(int j=0;j<length;j++){
int a= ch[j] - '0';
num.push_back(a);
}
int vlen=num.size();
int mid1=0,mid2=0;
if(vlen==1){
cout<<num[0]<<endl;
return 0;
}
if(vlen==2){
mid1=0;mid2=1;
int bigindex,smallindex;
if(num[mid1]>num[mid2]){
bigindex=mid1;
smallindex=mid2;
}
else{
bigindex=mid2;
smallindex=mid1;
}
num[smallindex]=num[bigindex];
println(num,2);
return 0;
}
if(vlen%2==0){
mid2=vlen/2;
mid1=mid2-1;
}
else{
mid1=mid2=vlen/2;
}
temp=num;
if(mid1==mid2){
int y=mid1;
//for odd implementation
for(int j=0;j<mid1;j++){
++y;
num[y]=num[j];
}
//num contains palindrome and temp contains original
if(comp(temp,num)){
println(num,vlen);
return 0;
}//printing the number if the palindrome is greater than the input number
else{
if(check9(num,vlen)){
nine(num,vlen);
return 0;
}
num=incrementmiddle(num,mid1,mid2,vlen);
println(num,vlen);
return 0;
}
}
else{//even implementation
int h=vlen-1;
for(int b=0;b<mid1;b++){
num[h]=num[b];
h--;
}
if(comp(temp,num)){
num=even_greater_increment(num,mid1,mid2);
println(num,vlen);
return 0;
}//printing the number if the palindrome is greater than the input number
else{
if(check9(num,vlen)){
nine(num,vlen);
return 0;
}
num=incrementmiddle(num,mid1,mid2,vlen);
println(num,vlen);
return 0;
}
}
num.clear();
temp.clear();
}
int main(){
int test=0;
cin>>test;
testcase:
for(int f=0;f<test;f++){
palindrome();
cout<<endl;
}
}
It actually takes a number and the print the next small palindrome.
The logic and everything is correct. It works fine in the compiler and ideone.
please help. I really want to get this problem accepted because i spent more than 5 hrs writing the logic.
Related
I have written the code to check if a cycle exists or not in a graph using breadth-first traversal.
If I declare the adjacency list as vectoradj[n]; then it's okay.
But the problem arises when I use vector<vector>adj(n)
Can someone tell me what is the error? and how can I use the latter option to run my code?
#include <bits/stdc++.h>
using namespace std;
bool checkForCycle(int s, int V, vector<vector<int>>adj, vector<int> &visited)
{
queue<pair<int, int>> q;
visited[s] = true;
q.push({s, -1});
while (!q.empty())
{
int node = q.front().first;
int par = q.front().second;
q.pop();
for (auto it : adj[node])
{
if (!visited[it])
{
visited[it] = true;
q.push({it, node});
}
else if (par != it)
return true;
}
}
return false;
}
bool isCycle(int V,vector<vector<int>>adj)
{
vector<int> vis(V - 1, 0);
for (int i = 1; i <= V; i++)
{
if (!vis[i])
{
if (checkForCycle(i, V, adj, vis))
return true;
}
}
}
int main() {
int n, m;
cout<<"Enter number of vertices :\n";
cin>>n;
cout<<"Enter number of edges :\n";
cin>>m;
vector<vector<int>>adj(n);
for(int i=0; i<m; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
cout<<endl;
if(isCycle(n,adj))
cout<<"cycle present int the graph";
else
cout<<"No cycle present";
return 0;
}
okay so the error resolves if I use
vector<vector<int>>adj(n+1);
instead of
vector<vector<int>>adj(n);
class Suduko
{
public:
int N=4;
int grid[N][]={{1,0,3,0}
,{0,0,2,1}
,{0,1,0,2}
,{2,4,0,0}};
bool solve()
{
int i,j;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
if(grid[i][j]==0)
break;
}
if(i==N && j==N)
return true;
for(int n=1;n<=N;n++)
{
if(issafe(i,j,n))
{
grid[i][j]=n;
if(solve())
return true;
grid[i][j]=0;
}
}
return false;
}
bool issafe(int i,int j,int n)
{
for(int k=0;k<N;k++)
if(grid[k][j]==n || grid[i][k]==n)
return false;
int s=sqrt(N);
int rs=i-i%s;
int cs=j-j%s;
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
if(grid[i+rs][j+cs]==n)
return false;
return true;
}
void print()
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
};
When I try to solve the sudoku problem I got an error in line number 8 " Invalid use of non-static data member 'Suduko::N' ".
I am new to C++, I don't figure out what is the cause of this error please help me to remove this error.
I am trying to implement quick sort to sort a sequence of integers.
I am getting a segmentation default with the following code:
I implemented partition and quick sort recursive calls.But for some c++ reason I am getting an access to memory or an infinite loop I cant understand why.
#include <fstream>
#include<vector>
#include<iostream>
using namespace std;
int pivotSelection(vector<int> A){
return 0;
}
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=0; j< A.size(); j++) {
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i;
}
vector<int> readArray(char* file){
ifstream inFile;
vector<int> A;
inFile.open(file);
int x;
while (inFile >>x ) {
A.push_back(x);
}
return A;
}
void quickSort(vector<int> &A, int l,int r){
if(r==1) {
return ;
}
if(r>l){
int p= partition(A,l,r);
quickSort(A,l,p-1);
quickSort(A,p+1,r);
}
}
int main(){
vector<int> A;//= readArray((char*)"/home/brunoeducsantos/AlgorithmFoundation/quicksort/data.txt");
A.push_back(3);
A.push_back(5);
A.push_back(7);
A.push_back(1);
int length = A.size();
quickSort(A,0,length-1);
for(int i=0;i<length;i++) cout<<A[i]<<endl;
return 0;
};
The expected result is: 1 3 5 7
Fixes noted in comments:
int partition(vector<int> &A,int l,int r){
int i=l+1;
int p = A[l];
for(int j=i; j<=r; j++) { // fix
if(A[j]<p){
swap(A[j], A[i] );
i=i+1;
}
}
swap(A[l], A[i-1]);
return i-1; // fix
}
I was writing the program for finding a path through an defined matrix.
#include<iostream>
#include<list>
using namespace std;
class maze{
int inst[10][10];
int m,n,initr,initc,finalr,finalc;
public:
maze(int c){
if(c==1) return;
cout<<"\n Enter rows and colums: ";
cin>>m>>n;
cout<<endl<<"\n ENter initial configuration (1 for block, 0 for open):";
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>inst[i][j];
}
}
cout<<endl<<"Enter initial state in row column:";
cin>>initr>>initc;
cout<<endl<<"Enter final state in row column:";
cin>>finalr>>finalc;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==1) inst[i][j]=(-1);
}
}
inst[initr][initc]=1;
}
bool up(int curr){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==0) return false;
if(inst[x-1][y]==0){
//cout<<"\n up "<<x-1<<y;
inst[x-1][y]=curr+1;
return true;
}else return false;
}
bool down(int curr){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==m-1) return false;
if(inst[x+1][y]==0){
//cout<<"\n down "<<x+1<<y;
inst[x+1][y]=curr+1;
return true;
}else return false;
}
bool left(int curr){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(y==0) return false;
if(inst[x][y-1]==0){
//cout<<"\n left "<<x<<y-1;
inst[x][y-1]=curr+1;
return true;
}else return false;
}
bool right(int curr){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(y==n-1) return false;
if(inst[x][y+1]==0){
//cout<<"\n right "<<x<<y+1;
inst[x][y+1]=curr+1;
return true;
}else return false;
}
bool check(int curr){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==finalr && y==finalc) return true;
else return false;
}
void print_maze(){
cout<<endl<<endl;
for(int i=0;i<m;i++){
cout<<endl;
for(int j=0;j<n;j++){
cout<<"\t"<<inst[i][j];
}
}
}
};
bool state_search(){
int curr=1;
maze start(0),temp(1);
list<maze> queue;
queue.push_back(start);
while(!queue.empty()){
start = queue.front();
queue.pop_front();
if(start.check(curr)){
start.print_maze();
return true;
}
//start.print_maze();
temp=start;
if(temp.up(curr)) queue.push_back(temp);
temp=start;
if(temp.down(curr)) queue.push_back(temp);
temp=start;
if(temp.left(curr)) queue.push_back(temp);
temp=start;
if(temp.right(curr)) queue.push_back(temp);
curr++;
}
cout<<"\n No answer found";
}
int main(){
state_search();
}
This program works fine for most of the inputs. But gives an address boundry error for the following input
Enter rows and colums: 4 4
ENter initial configuration (1 for block, 0 for open):0 0 0 0 0 1 0 1
0 1 1 1 0 0 0 0
Enter initial state in row column:1 0
Enter final state in row column:3 3 fish: “./a.out” terminated by
signal SIGSEGV (Address boundary error)
Please suggest the possible reason for this error and how to correct it
The problem is that you don't initialize x and y values in your up, down, left and right functions. And then if you don't find the indexes for curr, you access the inst array at random indexes.
In right function you should declare x and y like this:
int x = 0,y = 0;
In left:
int x = m - 1,y = 0;
Do the same in right and left.
You use uninitialized variables x and y in method maze::up:
bool up(int curr){
int x,y; // !!!
...
So when in following loops we didn't go inside if statement these variables still contain garbage from uninitialized allocated memory.
...
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
...
And it's very high possibility that x is not in range [0, 10).
...
if(x==0) return false;
if(inst[x-1][y]==0){
...
And we immediately try to get value somewhere (inst[x-1][y]) far away in memory, and terminated with segmentation error.
Fix: It's better to initialize all the variables with meaningful values. Suppose that for your code this is correct solution:
bool up(int curr){
int x = 0, y = 0;
...
You can tell to compiler to notify you when you keep somewhere uninitialized variables.
If you use GCC compiler you can use compilation flag:
g++ -O2 -Wuninitialized prog.cpp
And you will see a lot of similar places in your code.
Additionally you can use flag -Wall, and you will found that bool state_search() function reaches end without returning any value.
I realized where the mistake was. I did not initialize x and y in the functions maze::up(), maze::down(), maze::right() and maze::left() as it was always supposed to be initialized by the function while traversing the matrix. The Error was in the algorithm itself. The variable curr from state_search() was supposed to denote the depth of the BFS tree. But since it was incrementing for every node found, it denoted the depth wrongly causing error whenever there were 2 possible paths.
To solve this issue, I removed the curr variable from state_search() and added it to the class definition initializing it to 1 and incrementing it whenever the functions maze::up(), maze::down(), maze::right() and maze::left() are called.
here is the complete working code
#include<iostream>
#include<list>
using namespace std;
class maze{
int inst[10][10];
int m,n,initr,initc,finalr,finalc,curr;
public:
maze(int c){
if(c==1) return;
cout<<"\n Enter rows and colums: ";
cin>>m>>n;
cout<<endl<<"\n ENter initial configuration (1 for block, 0 for open):";
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
cin>>inst[i][j];
}
}
cout<<endl<<"Enter initial state in row column:";
cin>>initr>>initc;
cout<<endl<<"Enter final state in row column:";
cin>>finalr>>finalc;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==1) inst[i][j]=(-1);
}
}
inst[initr][initc]=1;
curr=1;
}
bool up(){
int x=0,y=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==0) return false;
if(inst[x-1][y]==0){
inst[x-1][y]=curr+1;
curr++;
return true;
}else return false;
}
bool down(){
int x=m-1,y=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==m-1) return false;
if(inst[x+1][y]==0){
inst[x+1][y]=curr+1;
curr++;
return true;
}else return false;
}
bool left(){
int x,y=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(y==0) return false;
if(inst[x][y-1]==0){
inst[x][y-1]=curr+1;
curr++;
return true;
}else return false;
}
bool right(){
int x,y=n-1;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(y==n-1) return false;
if(inst[x][y+1]==0){
inst[x][y+1]=curr+1;
curr++;
return true;
}else return false;
}
bool check(){
int x,y;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(inst[i][j]==curr) {
x=i;
y=j;
break;
}
}
}
if(x==finalr && y==finalc) return true;
else return false;
}
void print_maze(){
cout<<endl<<endl;
for(int i=0;i<m;i++){
cout<<endl;
for(int j=0;j<n;j++){
cout<<"\t"<<inst[i][j];
}
}
}
};
bool state_search(){
maze start(0),temp(1);
list<maze> queue;
queue.push_back(start);
while(!queue.empty()){
start = queue.front();
queue.pop_front();
if(start.check()){
start.print_maze();
return true;
}
temp=start;
if(temp.up()) queue.push_back(temp);
temp=start;
if(temp.down()) queue.push_back(temp);
temp=start;
if(temp.left()) queue.push_back(temp);
temp=start;
if(temp.right()) queue.push_back(temp);
}
cout<<"\n No answer found";
return false;
}
int main(){
state_search();
}
I have a vector of strings named "values". I want to merge sort them but I kept getting bus error or segmentation error. I know the errors are from the merge_sort function but I don't know how to fix it. Any ideas?
int main()
{
int p=1,q=values.size()/2,r=values.size();
merge_sort(values,p,r);
for (unsigned int i = 0; i < values.size(); i++)
{
cout << values[i] << endl;
}
}
}
void merge_sort(vector<string> a,int p,int r)
{
int q;
if(p<r)
{
q=r/2;
merge_sort(a,p,q);
merge_sort(a,q+1,r);
merge(a,p,q,r);
}
}
void merge(vector<string> a,int p,int q,int r)
{
int n1=q-p+1;
int n2=r-q;
string L[n1+1];
string R[n2+1];
for(int i=0;i<n1;i++)
{
L[i]=a[p+i-1];
}
for(int j=0;j<n2;j++)
{
R[j]=a[q+j];
}
L[n1]="ZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
R[n2]="ZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
int i=0, j=0;
for(int k=0;k<r;k++)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
}
}