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 know that nan means "not a number",but this code using the Gaussian elimination algorithm output "-nan" :
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=55;
const double eps=1e-7;
int n;
struct mat {
int l,c;
double p[N][N];
double* operator [] (int i) {return p[i];}
}a;
bool flo0(double &x) {return (fabs(x)<=eps)?1:0;}
void REF() {
for(int i=1;i<=n;i++) {
int flag=i;
for(int j=i;j<=n;j++) {
if(fabs(a[j][i])>fabs(a[flag][i])) flag=j;
}
for(int j=1;j<=n+1;j++) swap(a[flag][j],a[i][j]);
if(flo0(a[i][i])) continue;
for(int j=i+1;j<=n;j++) {
double t=a[j][i]/a[i][i];
for(int k=i;k<=n+1;k++) {
a[j][k]-=t*a[i][k];
}
}
}
}
void RREF() {
for(int i=n;i>=1;i--) {
if(flo0(a[i][i])) continue;
for(int j=n+1;j>=i;j--) a[i][j]/=a[i][i];
for(int j=i-1;j>=1;j--) {
double t=a[j][i];
for(int k=n+1;k>=1;k--) {
a[j][k]-=t*a[i][k];
}
}
}
bool no=false,many=false;
for(int i=1;i<=n;i++) {
int t=0;
for(int j=1;j<=n;j++) if(flo0(a[i][j])==false) ++t;
if(t==0 && flo0(a[i][n+1])==false) no=true;
if(t==0 && flo0(a[i][n+1])) many=true;
}
if(no) printf("-1");
else if(many) printf("0");
else for(int i=1;i<=n;i++) {if(flo0(a[i][n+1])) a[i][n+1]=0.0;printf("x%d=%.2lf\n",i,a[i][n+1]);}
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
for(int j=1;j<=n+1;j++) {
cin>>a[i][j];
}
}
REF();
RREF();
return 0;
}
I can't get the input,but the output is
x1=-nan
x2=-nan
x3=-nan
x4=-nan
x5=-nan
x6=-nan
x7=-nan
x8=-nan
x9=-nan
x10=-nan
x11=-nan
x12=-nan
x13=-nan
x14=-nan
x15=-nan
x1...
I initially thought the reason is that I didn't use "eps" when I determining if a double equals to 0,but after I added,the error still existed.
I can't get any information about "-nan" from google.
It's a not-a-number value with a representation with negative in the sign bit, that your runtime has decided to show as "negative nan".
When the program working, the value in p[i][j] overflows and it gets inf. And inf/inf causes nan.
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;
}
I know the logic how to merge two arrays but the problem is how to code.
This was my code n it is giving correct ans but my sir told me that do it again,please tell me what I have to add in this code,
#include<iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
mergeArrays(arrayA,size1,arrayB,size);
}
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i];
cout<<" "<<array3[k];
}
int j=0;
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++)
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j];
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++];
}
}
I had searched this in many places but could not corrected my code
I had written this code but it is not giving correct ans.
#include<iostream>
using namespace std;
int merge(int *a,int *b,int aSize,int bSize);
int main()
{
const int aSize={8};
const int bSize={12};
int arrayA[aSize]={10,25,37,49,50,51,55,60};
int arrayB[bSize]={2,5,26,27,29,32,40,45,70,80,90,95};
merge(arrayA,arrayB,aSize,bSize);
return 0;
}
int merge(int *a,int *b,int aSize ,int bSize)
{
int cSize=aSize+bSize;
int *c=new int[cSize];
int j=0,k=0;
int i=0;
while(i<=aSize&&j<=bSize )
{
if(a[aSize ]<=b[bSize])
{
c[k]=a[aSize];
k++;
i++;
}
else
{
c[k]=b[bSize];
k++;
j++;
}
}
for(int i=0;i<k;i++)
{
cout<<c[i]<<endl;
}
delete[]c;
return c[k++];
}
your sir request you do Merging two arrays in ascending order. so i think you should return a new array, fill with array1 and array2's element, and the elements should be ascending order. here is a implement.(suppose your input arraies is already in ascending order.)
#include <iostream>
using namespace std;
int mergeArrays(int array1[],int size1,int array2[],int size2, int outArray[]);
int main()
{
const int size1=8;
const int size=12;
int arrayA[size1]={10,25,37,49,50,51,55,60};
int arrayB[size]={2,5,26,27,29,32,40,45,70,80,90,95};
int outArray[size1+size];
int len = mergeArrays(arrayA,size1,arrayB,size, outArray);
cout <<" "<< len;
for (int i = 0; i< size1+size; ++i){
cout <<" " << outArray[i];
}
}
int mergeArrays(int array1[], int size1, int array2[], int size2, int outArray[])
{
int i=0, j=0, k=0;
int retSize = size1+size2;
while (k<retSize){
if (i==size1){// only left array2, copy it
for (; j<size2; ++j){
outArray[k++] = array2[j];
}
}else if (j == size2) { // only left array1, copy it
for (; i<size1; ++i){
outArray[k++] = array1[i];
}
}
else if (array1[i] > array2[j]){ // copy the min value to outArray
outArray[k++] = array2[j++];
}else{
outArray[k++] = array1[i++];
}
}
return k;
}
now, let's look at your first code:
int mergeArrays(int array1[],int size1,int array2[],int size2)
{
int size3=size1+size2;
int *array3=new int[size3];
int k=0;
for(int i=0;i<size1;i++)
{
array3[k]=array1[i]; // k is not changed, so you just assign array1's each value to array3[0]
cout<<" "<<array3[k];
}
int j=0;
// what's the purpose of this loop?
// and in loop, you don't use i, you just repeat set array3[0] = array2[0]!!
for(int i=size1;i<size2;i++)
{
array3[k]=array2[j];
}
for(int i=size1;i<size2;i++) // if array2's length bigger than array1's, will enter this loop.
{
for(int j=0;j<size2;j++)
{
array3[i]=array2[j]; // this just repeat assign array2's each value to array3[i]!!
cout<<" "<<array3[i];
}
cout<<endl;
delete[]array3;
return array3[k++]; // you delete array3, but at here you access it!! this will crash!
// also, in this for i loop, you have return, so it will only execute once.
}
// reach function end and no return if not enter for loop.
}
I haven't looked at your second code. I think you still need to do more study.
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++;
}
}
}