I wrote this code for printing the productions after eliminating left recursion.And also i stored these output productions in "all" array. when i print this array, only the last values are being printed with blank lines in the initial rows.How to resolve this problem?
#include<iostream>
#include<cstring>
using namespace std;
class production
{
private:
char lhs;
char rhs[10][10],nr[10][10],rs[10][10],all[30][30];
int noa,m;
public:
production()
{
noa=0;m=0;
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
all[i][j]='\0';
}
void makeprod(char *str)
{
noa=0;
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
rhs[i][j]='\0';
lhs=str[0];
char r[20];
strcpy(r,str+3);
int j=0;
for(int i=0;r[i]!='\0';i++)
{
if(r[i]!='/')
rhs[noa][j++]=r[i];
else
{
rhs[noa++][j]='\0';
j=0;
}
}
noa++;
}
void checks(string sr)
{
for(int i=0;i<30;i++)
for(int j=0;j<30;j++)
rs[i][j]=nr[i][j]='\0';
int ct=0,k=0,l=0;
for(int i=0;i<noa;i++)
{
if(lhs==rhs[i][0])
{
strcat(rhs[i],sr.c_str());
strcpy(rs[k],rhs[i]+1);
k++;
}
else
{
strcpy(nr[l],rhs[i]);
strcat(nr[l],sr.c_str());
l++;
}
}
if(l==noa)
{
cout<<lhs<<"->";
for(int i=0;i<noa;i++)
cout<<rhs[i]<<" ";
cout<<"\n";
all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<noa;i++)
{
strcat(all[m],rhs[i]);
strcat(all[m],"/");
}
m++;
}
else
{
cout<<lhs<<"->";
for(int i=0;i<l;i++)
cout<<nr[i]<<" ";
p.all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<l;i++)
{
strcat(all[m],nr[i]);
strcat(all[m],"/");
}
m++;
cout<<"\n"<<sr<<"->";
for(int i=0;i<k;i++)
cout<<rs[i]<<" ";
cout<<" e\n";
all[m][0]=lhs;
strcat(all[m],"->");
for(int i=0;i<k;i++)
{
strcat(all[m],rs[i]);
strcat(all[m],"/");
}
m++;
}
cout<<"m= "<<m<<"\n";
for(int i=0;i<m;i++)
{
for(int j=0;all[i][j]!='\0';j++)
cout<<all[i][j];
cout<<"\n";
}
}
void display()
{
cout<<"m= "<<m<<"\n";
for(int i=0;i<m;i++)
{
for(int j=0;all[i][j]!='\0';j++)
cout<<all[i][j];
cout<<"\n";
}
}
};
int main()
{
production p;
char str[20][20];
string op[5]={"X","Y","Z","U","V"};
cout<<"enter no of productions\n";
int n,l=0;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter a production\n";
cin>>str[i];
p.makeprod(str[i]);
p.checks(op[l++]);
}
p.display();
return 0;
}
Related
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 want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?
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++;
}
}
}
# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.
I am making a timetable generator as a project.
A part of the code seems to have a logical error.
void _tmain(int argc, _TCHAR* argv[])
{
int time=4;
int classes=2;
int teacher=4;
const int column=4;
const int rows=8;
int table[rows][column];
int final_table[rows][column];
int cell;
int temp=time;
int temp2=classes;
int temp3=teacher;
int cell_reset=111;
int cell_temp;
int k=0;
int selector_temp=0;
int selector_temp2=0;
cell=111;
//array initilization loop
for(int i=0;i<rows;i++)
{
for(int j=0;j<rows;j++)
{
table[i][j]=-1;
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<rows;j++)
{
final_table[i][j]=-1;
}
}
//Number generator loop
for(int i=0;i<rows;)
{
while(k<classes)
{
for(int j=0;j<column;j++)
{
table[i][j]=cell;
cell++;
}
cell=cell_reset+10;
k++;
i++;
}
k=0;
cell=cell_reset+100;
cell_reset=cell;
}
//selector loop
int counter=0;
for(int i=0;i<rows;i++)
{
counter=0;
for(int j=0;j<column&&counter<1;j++)
{
if(table[i][j]==selector_temp+10)
{
table[i][j]=-1;
}
if(table[i][j]==selector_temp-10)
{
table[i][j]=-1;
}
if(table[i][j]!=-1)
{
selector_temp=table[i][j];
final_table[i][j]=table[i][j];
for(int gg=(j+1);gg<column;gg++)
{
table[i][gg]=-1;
}
selector_temp2=selector_temp;
while(k<time)
{
selector_temp2+=100;
for(int ii=0;ii<rows;ii++)
{
for(int jj=0;jj<column;jj++)
{
if(table[ii][jj]==selector_temp2)
{
table[ii][jj]=-1;
}
}
}
k++;
}
k=0;
counter++;
}
}
}
//display loop
for(int i=0;i<rows;i++)
{
for(int j=0;j<column;j++)
{
cout<<final_table[i][j];
cout<<" ";
}
cout<<endl;
}
}//end of main bracket
This code generates exactly what I want.
But when I try to run this code the compiler gives me error that
Stack around the variable table is corrupt.
I choose to ignore this error and then the program gives me the correct result.
I have tried to find the source of this error but I cannot on the top of that I am getting correct results so if it cannot be found how can I disable the prompt that the compiler gives me.
The two initialisation loops are wrong - the inner loop should have j<column