Why i am getting error not declared in this scope? - c++

#include<iostream>
#include<vector>
using namespace std;
int sol(int i,int j,vector<vector<char>>v,int h,int w,int dp[][w]){
if(i>h || j>w){
return 0;
}
if(i==h && j==w){
return 1;
}
if(v[i][j]=='#'){
return 0;
}
if(dp[i][j]!=-1){
return dp[i][j];
}
dp[i][j]=sol(i+1,j,v,h,w,dp) + sol(i,j+1,v,h,w,dp);
return dp[i][j];
}
int main(){
int h,w;
cin>>h>>w;
vector<vector<char>>v(h);
char c;
int dp[h][w];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>c;
v[i].push_back(c);
dp[i][j]=-1;
}
}
h--;
w--;
cout<<sol(0,0,v,h,w,dp)<<endl;
}
Why i am getting error that i,j,h,w,dp is not declared in this scope(Inside sol function).If i remove the dp[][] array from my code then it run without any error https://ideone.com/uqz3p3 )
Error Screenshot

In C++ array bounds must be compile time constants. In your code int dp[][w] w is a variable, not a constant.
Since you are already using vectors I suggest you use a vector for dp as well. In main
vector<vector<int>> dp(h, vector<int>(w));
and in sol
int sol(int i,int j,vector<vector<char>>v,int h,int w,vector<vector<int>>& dp) {

Related

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;
}

Finding LCS using DP

I have used Dynamic Programming to find longest common sub-sequence b/w two strings. What is wrong in the code. Why it always gives answer as 0?
#include<bits/stdc++.h>
using namespace std;
int dp[20][20];
void initialize()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
dp[i][j]=-1;
}
int lcs(string a,string b,int m,int n)
{
if(m==0||n==0)
return 0;
if(dp[m][n]!=-1)
return dp[m][n];
if(a[m-1]==b[n-1])
return dp[m-1][n-1] = 1+lcs(a,b,m-1,n-1);
if(a[m-1]!=b[n-1])
return dp[n-1][m-1]=max(dp[n][m-1]=lcs(a,b,n,m-1),dp[n-1][m]=lcs(a,b,n-1,m));
}
int main()
{
string a="AGGTAB",b="GXTXAYB";
cout<<lcs(a,b,a.length(),b.length());
}
You've forgotten to call initialize()
18th line, it should be dp[m][n], not dp[m-1][n-1]
Commented 19th line code, as it is no need & for make the code compatible for all compilers
i.e., some compiler may give warning: control reaches end of non-void function [-Wreturn-type]
Made some code change in 20th line, as it seems you confused with variables m & n.
Code:
#include<bits/stdc++.h>
using namespace std;
int dp[20][20];
void initialize()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
dp[i][j]=-1;
}
int lcs(string a,string b,int m,int n)
{
if(m==0||n==0)
return 0;
if(dp[m][n]!=-1)
return dp[m][n];
if(a[m-1]==b[n-1])
return dp[m][n] = 1+lcs(a,b,m-1,n-1);
//if(a[m-1]!=b[n-1])
return dp[m][n]=max(lcs(a,b,m-1,n),lcs(a,b,m,n-1));
}
int main()
{
string a="AGGTAB",b="GXTXAYB";
initialize();
cout<<lcs(a,b,a.length(),b.length());
}
Output:
4

Hello, This simple C++ script pops out an error at line 11. Does anybody know how to fix it?

Description
This code is intended to find a spanner index (i) when dealing with
Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!
code
# include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(n,p,u,U) /* an error in this line */
{
if (u==U[n+1]) return (n);
low=p; high=n+1;
mid=(low+high)/2
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2
}
return (mid);
}
You have forgotten some semicolons and types!
here is the correct code:
#include <iostream>
using namespace std;
int n=3;
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};
int FindSpan(int n, int p, int u, int U[])
{
if (u==U[n+1]) return (n);
int low=p, high=n+1;
int mid=(low+high)/2;
while(u<U[mid] || u>=U[mid+1])
{
if (u<U[mid]) high=mid;
else low=mid;
mid=(low+high)/2;
}
return (mid);
}
int main() {
cout << FindSpan(n, p, u, U);
return 0;
}

Binary Search with few modification

While I am trying to compile the code with few modification in binary search recursive function. The program is acting weird. Some time it gives the correct value and some time it goes to infinite loop. Please explain what went wrong with the code. I am using DEV C++ as an IDE.
CODE:
#include<iostream>
#include<sstream>
using namespace std;
//function to compare the two integers
int compare(int low, int high)
{
if (low==high)
return 0;
if (low<high)
return 1;
else
return -1;
}
//Function for binary search using recursion
int *BinarySearch(int *Arr,int Val,int start,int end)
{
int localstart=start;
int localend=end;
int mid=(start+end)/3;
cout<<"MID:"<<mid;
int comp= compare(Val,Arr[mid]);
if(comp==0)
return &(Arr[mid]);
else if (comp>0)
return BinarySearch(Arr,Val,localstart,mid-1);
else
return BinarySearch(Arr,Val,mid+1,localend);
return NULL;
}
main()
{
int *arr;
arr= new int [256];
string str;
getline(cin,str);
stringstream ss;
ss<<str;
int index=0;
while(ss>>arr[index])
{index++;}
//cout<<arr[index-1];
cout<<"Enter Value:";
int value;
cin>>value;
int *final;
final=BinarySearch(arr,value,0,index-1);
if(final!=NULL)
cout<<"Final:"<<*final;
else
cout<<"Not Found";
getchar();
getchar();
return 0;
}
Two ideas:
What should BinarySearch do if Val is not in the array? Trace out what your code does in this case.
(start+end)/3 probably isn't the middle of the current range.